Input-Text geht, CORS behoben
This commit is contained in:
21
backend/dist/routes/images.js
vendored
21
backend/dist/routes/images.js
vendored
@@ -11,13 +11,21 @@ const fs_1 = __importDefault(require("fs"));
|
||||
const config_1 = require("../config/config");
|
||||
const router = (0, express_1.Router)();
|
||||
const prisma = new client_1.PrismaClient();
|
||||
const getUploadsDir = (subPath) => {
|
||||
const localUploadsDir = path_1.default.join(process.cwd(), 'uploads');
|
||||
const legacyUploadsDir = path_1.default.join(process.cwd(), '../../uploads');
|
||||
const baseDir = fs_1.default.existsSync(localUploadsDir)
|
||||
? localUploadsDir
|
||||
: legacyUploadsDir;
|
||||
return subPath ? path_1.default.join(baseDir, subPath) : baseDir;
|
||||
};
|
||||
const storage = multer_1.default.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
const recipeNumber = req.body.recipeNumber || req.params.recipeNumber;
|
||||
if (!recipeNumber) {
|
||||
return cb(new Error('Recipe number is required'), '');
|
||||
}
|
||||
const uploadDir = path_1.default.join(process.cwd(), '../../uploads', recipeNumber);
|
||||
const uploadDir = getUploadsDir(recipeNumber);
|
||||
if (!fs_1.default.existsSync(uploadDir)) {
|
||||
fs_1.default.mkdirSync(uploadDir, { recursive: true });
|
||||
}
|
||||
@@ -28,7 +36,7 @@ const storage = multer_1.default.diskStorage({
|
||||
if (!recipeNumber) {
|
||||
return cb(new Error('Recipe number is required'), '');
|
||||
}
|
||||
const uploadDir = path_1.default.join(process.cwd(), '../../uploads', recipeNumber);
|
||||
const uploadDir = getUploadsDir(recipeNumber);
|
||||
const existingFiles = fs_1.default.existsSync(uploadDir)
|
||||
? fs_1.default.readdirSync(uploadDir).filter(f => f.match(new RegExp(`^${recipeNumber}_\\d+\\.jpg$`)))
|
||||
: [];
|
||||
@@ -171,7 +179,7 @@ router.get('/serve/:imagePath(*)', (req, res, next) => {
|
||||
});
|
||||
}
|
||||
const cleanPath = imagePath.replace(/^uploads\//, '');
|
||||
const fullPath = path_1.default.join(process.cwd(), '../../uploads', cleanPath);
|
||||
const fullPath = path_1.default.join(getUploadsDir(), cleanPath);
|
||||
console.log(`Serving image: ${imagePath} -> ${fullPath}`);
|
||||
if (!fs_1.default.existsSync(fullPath)) {
|
||||
console.log(`Image not found: ${fullPath}`);
|
||||
@@ -182,8 +190,13 @@ router.get('/serve/:imagePath(*)', (req, res, next) => {
|
||||
resolvedPath: fullPath
|
||||
});
|
||||
}
|
||||
const allowedOrigins = ['http://localhost:5173', 'http://localhost:3000'];
|
||||
const origin = req.headers.origin;
|
||||
const corsOrigin = process.env.CORS_ORIGIN === '*'
|
||||
? (origin || '*')
|
||||
: (origin && allowedOrigins.includes(origin)) ? origin : 'http://localhost:3000';
|
||||
res.set({
|
||||
'Access-Control-Allow-Origin': 'http://localhost:5173',
|
||||
'Access-Control-Allow-Origin': corsOrigin,
|
||||
'Access-Control-Allow-Credentials': 'true',
|
||||
'Cache-Control': 'public, max-age=31536000',
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user