Local (esprimo) funktioniert, remote noch nicht

This commit is contained in:
2025-09-27 18:28:30 +00:00
parent db431553b9
commit f812921ff5
10 changed files with 277 additions and 72 deletions

View File

@@ -49,7 +49,7 @@ allowedOrigins = Array.from(new Set(allowedOrigins.map(o => o.replace(/\/$/, '')
// Auto-add common localhost dev origins if not prod and not wildcard
if (!isProd && !allowedOrigins.includes('*')) {
['http://localhost:5173','http://localhost:3000'].forEach(def => {
['http://localhost:5173','http://localhost:3000','http://esprimo:3000','http://esprimo:5173'].forEach(def => {
if (!allowedOrigins.includes(def)) allowedOrigins.push(def);
});
}

View File

@@ -333,7 +333,23 @@ router.post('/reorder/:recipeId', async (req: Request, res: Response, next: Next
}
const recipeNumber = recipe.recipeNumber;
const baseDir = getUploadsDir(recipeNumber);
// Determine the actual directory from the first image path
// Images might be in uploads/R005/ format instead of uploads/5/
let actualDir = recipeNumber;
if (images.length > 0 && images[0]) {
const firstImagePath = images[0].filePath;
// Extract directory from path like "uploads/R005/R005_0.jpg" -> "R005"
const pathParts = firstImagePath.split('/');
if (pathParts.length >= 2) {
const dirName = pathParts[pathParts.length - 2];
if (dirName) {
actualDir = dirName; // Get the directory name
}
}
}
const baseDir = getUploadsDir(actualDir);
if (!fs.existsSync(baseDir)) {
return res.status(500).json({ success: false, message: 'Upload directory missing on server' });
}
@@ -348,19 +364,19 @@ router.post('/reorder/:recipeId', async (req: Request, res: Response, next: Next
const fileName = image.filePath.split('/').pop() || '';
const extMatch = fileName.match(/\.(jpg|jpeg|png|webp)$/i);
const ext = extMatch && extMatch[1] ? extMatch[1].toLowerCase() : 'jpg';
const finalName = `${recipeNumber}_${idx}.${ext === 'jpeg' ? 'jpg' : ext}`;
const finalName = `${actualDir}_${idx}.${ext === 'jpeg' ? 'jpg' : ext}`;
// Previous implementation incorrectly traversed ../../ resulting in /uploads/... (missing /app prefix in container)
// image.filePath is like 'uploads/R100/R100_0.png' and baseDir points to '/app/uploads/R100'
// image.filePath is like 'uploads/R005/R005_0.png' and baseDir points to '/app/uploads/R005'
// So we join baseDir with just the filename to locate the current file.
const oldFull = path.join(baseDir, path.basename(fileName));
const tempName = `${recipeNumber}__reorder_${idx}_${Date.now()}_${Math.random().toString(36).slice(2)}.${ext}`;
const tempName = `${actualDir}__reorder_${idx}_${Date.now()}_${Math.random().toString(36).slice(2)}.${ext}`;
const tempFull = path.join(baseDir, tempName);
const finalFull = path.join(baseDir, finalName);
if (!fs.existsSync(oldFull)) {
return res.status(500).json({ success: false, message: `File missing on disk: ${fileName}` });
}
fs.renameSync(oldFull, tempFull);
tempRenames.push({ from: tempFull, to: finalFull, final: `uploads/${recipeNumber}/${finalName}`, idx, ext });
tempRenames.push({ from: tempFull, to: finalFull, final: `uploads/${actualDir}/${finalName}`, idx, ext });
}
// Phase 2: rename temp -> final