mirror of
https://github.com/System-End/spaces.git
synced 2026-04-19 18:45:22 +00:00
space limit
This commit is contained in:
parent
5067e21d92
commit
ace70841b0
5 changed files with 50 additions and 4 deletions
|
|
@ -48,7 +48,11 @@
|
||||||
newSpacePassword = '';
|
newSpacePassword = '';
|
||||||
await loadSpaces();
|
await loadSpaces();
|
||||||
} else {
|
} 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) {
|
} catch (err) {
|
||||||
error = ERROR_MESSAGES.NETWORK_ERROR;
|
error = ERROR_MESSAGES.NETWORK_ERROR;
|
||||||
|
|
|
||||||
|
|
@ -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 });
|
res.status(statusCode).json({ error: err.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,8 @@ router.post('/signup', async (req, res) => {
|
||||||
.insert({
|
.insert({
|
||||||
email,
|
email,
|
||||||
username,
|
username,
|
||||||
authorization: authToken
|
authorization: authToken,
|
||||||
|
max_spaces: 3
|
||||||
})
|
})
|
||||||
.returning(['id', 'email', 'username', 'authorization']);
|
.returning(['id', 'email', 'username', 'authorization']);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import Docker from "dockerode";
|
import Docker from "dockerode";
|
||||||
import getPort from "get-port";
|
import getPort from "get-port";
|
||||||
import pg from "./db.js";
|
import pg from "./db.js";
|
||||||
import { getUser } from "./user.js";
|
import { getUser, checkUserSpaceLimit } from "./user.js";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
|
|
@ -53,6 +53,8 @@ export const createContainer = async (password, type, authorization) => {
|
||||||
throw new Error("Invalid authorization token");
|
throw new Error("Invalid authorization token");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await checkUserSpaceLimit(user.id);
|
||||||
|
|
||||||
const config = containerConfigs[type.toLowerCase()];
|
const config = containerConfigs[type.toLowerCase()];
|
||||||
if (!config) {
|
if (!config) {
|
||||||
const error = new Error("Invalid container type");
|
const error = new Error("Invalid container type");
|
||||||
|
|
|
||||||
|
|
@ -13,4 +13,43 @@ export const getUser = async (authorization) => {
|
||||||
console.error('Error fetching user:', error);
|
console.error('Error fetching user:', error);
|
||||||
throw 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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
Loading…
Add table
Reference in a new issue