space limit

This commit is contained in:
Charmunks 2025-11-06 14:20:04 -05:00
parent 5067e21d92
commit ace70841b0
5 changed files with 50 additions and 4 deletions

View file

@ -48,7 +48,11 @@
newSpacePassword = '';
await loadSpaces();
} else {
error = data.error || ERROR_MESSAGES.CREATE_FAILED;
if (response.status === 403 && data.error?.includes('Maximum space limit')) {
error = data.error;
} else {
error = data.error || ERROR_MESSAGES.CREATE_FAILED;
}
}
} catch (err) {
error = ERROR_MESSAGES.NETWORK_ERROR;

View file

@ -25,7 +25,7 @@ router.post("/create", async (req, res) => {
});
}
const statusCode = err.message.includes("Missing") || err.message.includes("Invalid authorization") ? 400 : 500;
const statusCode = err.statusCode || (err.message.includes("Missing") || err.message.includes("Invalid authorization") ? 400 : 500);
res.status(statusCode).json({ error: err.message });
}
});

View file

@ -108,7 +108,8 @@ router.post('/signup', async (req, res) => {
.insert({
email,
username,
authorization: authToken
authorization: authToken,
max_spaces: 3
})
.returning(['id', 'email', 'username', 'authorization']);

View file

@ -1,7 +1,7 @@
import Docker from "dockerode";
import getPort from "get-port";
import pg from "./db.js";
import { getUser } from "./user.js";
import { getUser, checkUserSpaceLimit } from "./user.js";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
@ -53,6 +53,8 @@ export const createContainer = async (password, type, authorization) => {
throw new Error("Invalid authorization token");
}
await checkUserSpaceLimit(user.id);
const config = containerConfigs[type.toLowerCase()];
if (!config) {
const error = new Error("Invalid container type");

View file

@ -13,4 +13,43 @@ export const getUser = async (authorization) => {
console.error('Error fetching user:', error);
throw error;
}
};
export const checkUserSpaceLimit = async (userId) => {
if (!userId) {
throw new Error("User ID is required");
}
try {
const user = await pg('users')
.where('id', userId)
.first();
if (!user) {
throw new Error("User not found");
}
const userSpaces = await pg('spaces')
.where('user_id', userId)
.count('id as count')
.first();
const currentSpaceCount = parseInt(userSpaces.count) || 0;
const maxSpaces = user.max_spaces ?? 3;
if (currentSpaceCount >= maxSpaces) {
const error = new Error(`Maximum space limit reached (${maxSpaces} spaces)`);
error.statusCode = 403;
throw error;
}
return {
currentSpaceCount,
maxSpaces,
canCreateSpace: currentSpaceCount < maxSpaces
};
} catch (error) {
console.error('Error checking space limit:', error);
throw error;
}
};