Persist software uploads outside deploy path
This commit is contained in:
+22
-7
@@ -9,8 +9,9 @@ import multer from "multer";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const projectRoot = path.resolve(__dirname, "..");
|
||||
const dataDir = path.join(__dirname, "data");
|
||||
const uploadDir = path.join(__dirname, "uploads");
|
||||
const storageRoot = process.env.SUMI_STORAGE_DIR ? path.resolve(process.env.SUMI_STORAGE_DIR) : "";
|
||||
const dataDir = path.resolve(process.env.SUMI_DATA_DIR || (storageRoot ? path.join(storageRoot, "data") : path.join(__dirname, "data")));
|
||||
const uploadDir = path.resolve(process.env.SUMI_UPLOAD_DIR || (storageRoot ? path.join(storageRoot, "uploads") : path.join(__dirname, "uploads")));
|
||||
const packageDir = path.join(uploadDir, "packages");
|
||||
const iconDir = path.join(uploadDir, "icons");
|
||||
const manifestDir = path.join(uploadDir, "manifests");
|
||||
@@ -534,12 +535,24 @@ function isPathInside(parent, child) {
|
||||
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
||||
}
|
||||
|
||||
function resolvePackagePath(item) {
|
||||
if (item.storagePath) return path.resolve(item.storagePath);
|
||||
if (item.downloadUrl?.startsWith("/uploads/packages/")) {
|
||||
return path.resolve(packageDir, path.basename(decodeURIComponent(item.downloadUrl)));
|
||||
function resolveUploadPathFromUrl(publicUrl, prefix, directory) {
|
||||
if (!publicUrl?.startsWith(prefix)) return "";
|
||||
try {
|
||||
return path.resolve(directory, path.basename(decodeURIComponent(publicUrl)));
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function resolvePackagePath(item) {
|
||||
const publicPackagePath = resolveUploadPathFromUrl(item.downloadUrl, "/uploads/packages/", packageDir);
|
||||
if (item.storagePath) {
|
||||
const storagePath = path.resolve(item.storagePath);
|
||||
if (isPathInside(packageDir, storagePath)) return storagePath;
|
||||
if (publicPackagePath) return publicPackagePath;
|
||||
return storagePath;
|
||||
}
|
||||
return publicPackagePath;
|
||||
}
|
||||
|
||||
async function cleanupUploadedFiles(req) {
|
||||
@@ -944,6 +957,8 @@ await ensureStorage();
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`sumi.work software center running at http://localhost:${port}`);
|
||||
console.log(`data directory: ${dataDir}`);
|
||||
console.log(`upload directory: ${uploadDir}`);
|
||||
if (!process.env.ADMIN_TOKEN) {
|
||||
console.log("ADMIN_TOKEN is not set. Using development token: sumi-admin");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user