Docker mit traefik und portainer
This commit is contained in:
89
nodejs-version/backend/Dockerfile
Normal file
89
nodejs-version/backend/Dockerfile
Normal file
@@ -0,0 +1,89 @@
|
||||
# Backend Dockerfile
|
||||
FROM node:18-alpine AS builder
|
||||
|
||||
# Install OpenSSL for Prisma compatibility
|
||||
RUN apk add --no-cache openssl openssl-dev
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy package files
|
||||
COPY package*.json ./
|
||||
|
||||
# Install dependencies
|
||||
RUN npm ci --only=production
|
||||
|
||||
# Copy source code
|
||||
COPY . .
|
||||
|
||||
# Build the application
|
||||
RUN npm run build
|
||||
|
||||
# Production stage
|
||||
FROM node:18-alpine AS production
|
||||
|
||||
# Install required system dependencies for Prisma and health checks
|
||||
RUN apk add --no-cache \
|
||||
curl \
|
||||
openssl \
|
||||
openssl-dev \
|
||||
libc6-compat \
|
||||
&& rm -rf /var/cache/apk/*
|
||||
|
||||
# Install curl for healthcheck
|
||||
RUN apk add --no-cache curl
|
||||
|
||||
# Create app user
|
||||
RUN addgroup -g 1001 -S nodejs
|
||||
RUN adduser -S backend -u 1001
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy package files
|
||||
COPY package*.json ./
|
||||
|
||||
# Install only production dependencies
|
||||
RUN npm ci --only=production && npm cache clean --force
|
||||
|
||||
# Copy built application from builder stage
|
||||
COPY --from=builder /app/dist ./dist
|
||||
|
||||
# Copy prisma schema for runtime
|
||||
COPY --from=builder /app/prisma ./prisma
|
||||
|
||||
# Create uploads directory
|
||||
RUN mkdir -p uploads legacy-uploads && chown -R backend:nodejs uploads legacy-uploads
|
||||
|
||||
# Create migration script for legacy uploads (via volumes)
|
||||
COPY <<EOF ./migrate-uploads.sh
|
||||
#!/bin/sh
|
||||
# This will be handled via volume mounts in docker-compose
|
||||
# The legacy upload/ directory will be mounted to /app/legacy-uploads
|
||||
if [ -d "/app/legacy-uploads" ] && [ "$(ls -A /app/legacy-uploads)" ]; then
|
||||
echo "Migrating legacy uploads from volume..."
|
||||
cp -r /app/legacy-uploads/* /app/uploads/ 2>/dev/null || true
|
||||
chown -R backend:nodejs /app/uploads
|
||||
echo "Upload migration completed."
|
||||
else
|
||||
echo "No legacy uploads found to migrate."
|
||||
fi
|
||||
EOF
|
||||
|
||||
RUN chmod +x ./migrate-uploads.sh
|
||||
|
||||
# Generate Prisma client
|
||||
RUN npx prisma generate
|
||||
|
||||
# Switch to non-root user
|
||||
USER backend
|
||||
|
||||
# Expose port
|
||||
EXPOSE 3001
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
||||
CMD curl -f http://localhost:3001/api/health || exit 1
|
||||
|
||||
# Start the application
|
||||
CMD ["sh", "-c", "./migrate-uploads.sh && node dist/app.js"]
|
||||
2
nodejs-version/backend/dist/app.d.ts.map
vendored
2
nodejs-version/backend/dist/app.d.ts.map
vendored
@@ -1 +1 @@
|
||||
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAgBA,QAAA,MAAM,GAAG,6CAAY,CAAC;AAkGtB,eAAe,GAAG,CAAC"}
|
||||
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAgBA,QAAA,MAAM,GAAG,6CAAY,CAAC;AA2GtB,eAAe,GAAG,CAAC"}
|
||||
12
nodejs-version/backend/dist/app.js
vendored
12
nodejs-version/backend/dist/app.js
vendored
@@ -27,12 +27,20 @@ const limiter = (0, express_rate_limit_1.default)({
|
||||
message: 'Too many requests from this IP, please try again later.',
|
||||
});
|
||||
app.use(limiter);
|
||||
const allowedOrigins = [
|
||||
'http://localhost:5173',
|
||||
'http://localhost:3000',
|
||||
config_1.config.cors.origin
|
||||
].filter(Boolean);
|
||||
app.use((0, cors_1.default)({
|
||||
origin: config_1.config.cors.origin,
|
||||
origin: allowedOrigins,
|
||||
credentials: true,
|
||||
}));
|
||||
app.use((req, res, next) => {
|
||||
res.header('Access-Control-Allow-Origin', 'http://localhost:5173');
|
||||
const origin = req.headers.origin;
|
||||
if (origin && allowedOrigins.includes(origin)) {
|
||||
res.header('Access-Control-Allow-Origin', origin);
|
||||
}
|
||||
res.header('Access-Control-Allow-Credentials', 'true');
|
||||
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
|
||||
|
||||
2
nodejs-version/backend/dist/app.js.map
vendored
2
nodejs-version/backend/dist/app.js.map
vendored
@@ -1 +1 @@
|
||||
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":";;;;;AAAA,sDAA8B;AAC9B,gDAAwB;AACxB,oDAA4B;AAC5B,8DAAsC;AACtC,4EAA2C;AAC3C,gDAAwB;AACxB,4CAAyC;AACzC,4DAAyD;AACzD,8DAA2D;AAG3D,+DAA4C;AAC5C,uEAAoD;AACpD,6DAA0C;AAC1C,6DAA2C;AAE3C,MAAM,GAAG,GAAG,IAAA,iBAAO,GAAE,CAAC;AAGtB,GAAG,CAAC,GAAG,CAAC,IAAA,gBAAM,EAAC;IACb,yBAAyB,EAAE,EAAE,MAAM,EAAE,cAAc,EAAE;CACtD,CAAC,CAAC,CAAC;AACJ,GAAG,CAAC,GAAG,CAAC,IAAA,qBAAW,GAAE,CAAC,CAAC;AAGvB,MAAM,OAAO,GAAG,IAAA,4BAAS,EAAC;IACxB,QAAQ,EAAE,EAAE,GAAG,EAAE,GAAG,IAAI;IACxB,GAAG,EAAE,GAAG;IACR,OAAO,EAAE,yDAAyD;CACnE,CAAC,CAAC;AACH,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AAGjB,GAAG,CAAC,GAAG,CAAC,IAAA,cAAI,EAAC;IACX,MAAM,EAAE,eAAM,CAAC,IAAI,CAAC,MAAM;IAC1B,WAAW,EAAE,IAAI;CAClB,CAA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,MAAM,IAAI,GAAG,eAAM,CAAC,IAAI,CAAC;AAEzB,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;IACpB,OAAO,CAAC,GAAG,CAAC,6BAA6B,IAAI,EAAE,CAAC,CAAC;IACjD,OAAO,CAAC,GAAG,CAAC,qCAAqC,IAAI,aAAa,CAAC,CAAC;IACpE,OAAO,CAAC,GAAG,CAAC,0CAA0C,IAAI,MAAM,CAAC,CAAC;AACpE,CAAC,CAAC,CAAC;AAEH,kBAAe,GAAG,CAAC"}
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"file":"images.d.ts","sourceRoot":"","sources":["../../src/routes/images.ts"],"names":[],"mappings":"AAKA,QAAA,MAAM,MAAM,4CAAW,CAAC;AAuGxB,eAAe,MAAM,CAAC"}
|
||||
{"version":3,"file":"images.d.ts","sourceRoot":"","sources":["../../src/routes/images.ts"],"names":[],"mappings":"AAOA,QAAA,MAAM,MAAM,4CAAW,CAAC;AAqQxB,eAAe,MAAM,CAAC"}
|
||||
130
nodejs-version/backend/dist/routes/images.js
vendored
130
nodejs-version/backend/dist/routes/images.js
vendored
@@ -5,10 +5,140 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const express_1 = require("express");
|
||||
const client_1 = require("@prisma/client");
|
||||
const multer_1 = __importDefault(require("multer"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
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 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);
|
||||
if (!fs_1.default.existsSync(uploadDir)) {
|
||||
fs_1.default.mkdirSync(uploadDir, { recursive: true });
|
||||
}
|
||||
cb(null, uploadDir);
|
||||
},
|
||||
filename: (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 existingFiles = fs_1.default.existsSync(uploadDir)
|
||||
? fs_1.default.readdirSync(uploadDir).filter(f => f.match(new RegExp(`^${recipeNumber}_\\d+\\.jpg$`)))
|
||||
: [];
|
||||
const nextIndex = existingFiles.length;
|
||||
const filename = `${recipeNumber}_${nextIndex}.jpg`;
|
||||
cb(null, filename);
|
||||
}
|
||||
});
|
||||
const upload = (0, multer_1.default)({
|
||||
storage,
|
||||
limits: {
|
||||
fileSize: config_1.config.upload.maxFileSize,
|
||||
},
|
||||
fileFilter: (req, file, cb) => {
|
||||
const allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
|
||||
if (allowedTypes.includes(file.mimetype)) {
|
||||
cb(null, true);
|
||||
}
|
||||
else {
|
||||
cb(new Error('Invalid file type. Only JPEG, PNG and WebP are allowed.'));
|
||||
}
|
||||
},
|
||||
});
|
||||
router.post('/upload/:recipeId', upload.array('images', 10), async (req, res, next) => {
|
||||
try {
|
||||
const { recipeId } = req.params;
|
||||
const files = req.files;
|
||||
if (!recipeId) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Recipe ID is required',
|
||||
});
|
||||
}
|
||||
if (!files || files.length === 0) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'No files uploaded',
|
||||
});
|
||||
}
|
||||
const recipe = await prisma.recipe.findUnique({
|
||||
where: { id: parseInt(recipeId) }
|
||||
});
|
||||
if (!recipe) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Recipe not found',
|
||||
});
|
||||
}
|
||||
const imagePromises = files.map(file => {
|
||||
const relativePath = `uploads/${recipe.recipeNumber}/${file.filename}`;
|
||||
return prisma.recipeImage.create({
|
||||
data: {
|
||||
recipeId: parseInt(recipeId),
|
||||
filePath: relativePath,
|
||||
}
|
||||
});
|
||||
});
|
||||
const images = await Promise.all(imagePromises);
|
||||
return res.status(201).json({
|
||||
success: true,
|
||||
data: images,
|
||||
message: `${files.length} images uploaded successfully`,
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
if (req.files) {
|
||||
const files = req.files;
|
||||
files.forEach(file => {
|
||||
if (fs_1.default.existsSync(file.path)) {
|
||||
fs_1.default.unlinkSync(file.path);
|
||||
}
|
||||
});
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
router.delete('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
if (!id) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Image ID is required',
|
||||
});
|
||||
}
|
||||
const image = await prisma.recipeImage.findUnique({
|
||||
where: { id: parseInt(id) }
|
||||
});
|
||||
if (!image) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Image not found',
|
||||
});
|
||||
}
|
||||
const fullPath = path_1.default.join(process.cwd(), '../..', image.filePath);
|
||||
if (fs_1.default.existsSync(fullPath)) {
|
||||
fs_1.default.unlinkSync(fullPath);
|
||||
}
|
||||
await prisma.recipeImage.delete({
|
||||
where: { id: parseInt(id) }
|
||||
});
|
||||
return res.json({
|
||||
success: true,
|
||||
message: 'Image deleted successfully',
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
router.get('/recipe/:recipeId', async (req, res, next) => {
|
||||
try {
|
||||
const { recipeId } = req.params;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +1 @@
|
||||
{"version":3,"file":"recipes.d.ts","sourceRoot":"","sources":["../../src/routes/recipes.ts"],"names":[],"mappings":"AAIA,QAAA,MAAM,MAAM,4CAAW,CAAC;AA4PxB,eAAe,MAAM,CAAC"}
|
||||
{"version":3,"file":"recipes.d.ts","sourceRoot":"","sources":["../../src/routes/recipes.ts"],"names":[],"mappings":"AAIA,QAAA,MAAM,MAAM,4CAAW,CAAC;AAoQxB,eAAe,MAAM,CAAC"}
|
||||
@@ -74,8 +74,15 @@ router.get('/:id', async (req, res, next) => {
|
||||
message: 'Recipe ID is required',
|
||||
});
|
||||
}
|
||||
const recipeId = parseInt(id);
|
||||
if (isNaN(recipeId)) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Invalid recipe ID format',
|
||||
});
|
||||
}
|
||||
const recipe = await prisma.recipe.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
where: { id: recipeId },
|
||||
include: {
|
||||
images: true,
|
||||
ingredientsList: true,
|
||||
|
||||
File diff suppressed because one or more lines are too long
2
nodejs-version/backend/package-lock.json
generated
2
nodejs-version/backend/package-lock.json
generated
@@ -28,7 +28,7 @@
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/jest": "^29.5.8",
|
||||
"@types/jsonwebtoken": "^9.0.5",
|
||||
"@types/multer": "^1.4.11",
|
||||
"@types/multer": "^1.4.13",
|
||||
"@types/node": "^20.8.10",
|
||||
"@typescript-eslint/eslint-plugin": "^6.9.1",
|
||||
"@typescript-eslint/parser": "^6.9.1",
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/jest": "^29.5.8",
|
||||
"@types/jsonwebtoken": "^9.0.5",
|
||||
"@types/multer": "^1.4.11",
|
||||
"@types/multer": "^1.4.13",
|
||||
"@types/node": "^20.8.10",
|
||||
"@typescript-eslint/eslint-plugin": "^6.9.1",
|
||||
"@typescript-eslint/parser": "^6.9.1",
|
||||
|
||||
@@ -30,15 +30,36 @@ const limiter = rateLimit({
|
||||
});
|
||||
app.use(limiter);
|
||||
|
||||
// CORS configuration
|
||||
app.use(cors({
|
||||
origin: config.cors.origin,
|
||||
credentials: true,
|
||||
}));
|
||||
// CORS configuration - Allow both development and production origins
|
||||
const allowedOrigins = [
|
||||
'http://localhost:5173', // Vite dev server
|
||||
'http://localhost:3000', // Docker frontend
|
||||
config.cors.origin // Environment configured origin
|
||||
].filter(Boolean);
|
||||
|
||||
// Add local network origins if CORS_ORIGIN is "*" (for local network access)
|
||||
const corsConfig = config.cors.origin === '*'
|
||||
? {
|
||||
origin: true, // Allow all origins for local network
|
||||
credentials: true,
|
||||
}
|
||||
: {
|
||||
origin: allowedOrigins,
|
||||
credentials: true,
|
||||
};
|
||||
|
||||
app.use(cors(corsConfig));
|
||||
|
||||
// Additional CORS headers for all requests
|
||||
app.use((req, res, next) => {
|
||||
res.header('Access-Control-Allow-Origin', 'http://localhost:5173');
|
||||
const origin = req.headers.origin;
|
||||
|
||||
if (config.cors.origin === '*') {
|
||||
// Allow all origins for local network access
|
||||
res.header('Access-Control-Allow-Origin', origin || '*');
|
||||
} else if (origin && allowedOrigins.includes(origin)) {
|
||||
res.header('Access-Control-Allow-Origin', origin);
|
||||
}
|
||||
res.header('Access-Control-Allow-Credentials', 'true');
|
||||
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
|
||||
@@ -56,6 +77,9 @@ app.use(express.urlencoded({ extended: true, limit: '10mb' }));
|
||||
// Request logging
|
||||
app.use(requestLogger);
|
||||
|
||||
// Static file serving for uploads
|
||||
app.use('/uploads', express.static(path.join(process.cwd(), 'uploads')));
|
||||
|
||||
// API routes
|
||||
app.use('/api/health', healthRoutes);
|
||||
app.use('/api/recipes', recipeRoutes);
|
||||
|
||||
@@ -1,11 +1,180 @@
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import multer from 'multer';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { config } from '../config/config';
|
||||
|
||||
const router = Router();
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// Utility function to get correct uploads directory path
|
||||
const getUploadsDir = (subPath?: string): string => {
|
||||
const baseDir = process.env.NODE_ENV === 'production'
|
||||
? path.join(process.cwd(), 'uploads')
|
||||
: path.join(process.cwd(), '../../uploads');
|
||||
|
||||
return subPath ? path.join(baseDir, subPath) : baseDir;
|
||||
};
|
||||
|
||||
// Configure multer for file uploads
|
||||
const storage = multer.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 = getUploadsDir(recipeNumber);
|
||||
|
||||
// Create directory if it doesn't exist
|
||||
if (!fs.existsSync(uploadDir)) {
|
||||
fs.mkdirSync(uploadDir, { recursive: true });
|
||||
}
|
||||
|
||||
cb(null, uploadDir);
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
const recipeNumber = req.body.recipeNumber || req.params.recipeNumber;
|
||||
if (!recipeNumber) {
|
||||
return cb(new Error('Recipe number is required'), '');
|
||||
}
|
||||
|
||||
// Get existing files count to determine next index
|
||||
const uploadDir = getUploadsDir(recipeNumber);
|
||||
const existingFiles = fs.existsSync(uploadDir)
|
||||
? fs.readdirSync(uploadDir).filter(f => f.match(new RegExp(`^${recipeNumber}_\\d+\\.jpg$`)))
|
||||
: [];
|
||||
|
||||
const nextIndex = existingFiles.length;
|
||||
const filename = `${recipeNumber}_${nextIndex}.jpg`;
|
||||
|
||||
cb(null, filename);
|
||||
}
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage,
|
||||
limits: {
|
||||
fileSize: config.upload.maxFileSize, // 5MB
|
||||
},
|
||||
fileFilter: (req, file, cb) => {
|
||||
const allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
|
||||
if (allowedTypes.includes(file.mimetype)) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error('Invalid file type. Only JPEG, PNG and WebP are allowed.'));
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// Upload images for a recipe
|
||||
router.post('/upload/:recipeId', upload.array('images', 10), async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const { recipeId } = req.params;
|
||||
const files = req.files as Express.Multer.File[];
|
||||
|
||||
if (!recipeId) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Recipe ID is required',
|
||||
});
|
||||
}
|
||||
|
||||
if (!files || files.length === 0) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'No files uploaded',
|
||||
});
|
||||
}
|
||||
|
||||
// Get recipe to validate it exists and get recipe number
|
||||
const recipe = await prisma.recipe.findUnique({
|
||||
where: { id: parseInt(recipeId) }
|
||||
});
|
||||
|
||||
if (!recipe) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Recipe not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Create database entries for uploaded images
|
||||
const imagePromises = files.map(file => {
|
||||
const relativePath = `uploads/${recipe.recipeNumber}/${file.filename}`;
|
||||
return prisma.recipeImage.create({
|
||||
data: {
|
||||
recipeId: parseInt(recipeId),
|
||||
filePath: relativePath,
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const images = await Promise.all(imagePromises);
|
||||
|
||||
return res.status(201).json({
|
||||
success: true,
|
||||
data: images,
|
||||
message: `${files.length} images uploaded successfully`,
|
||||
});
|
||||
} catch (error) {
|
||||
// Clean up uploaded files if database operation fails
|
||||
if (req.files) {
|
||||
const files = req.files as Express.Multer.File[];
|
||||
files.forEach(file => {
|
||||
if (fs.existsSync(file.path)) {
|
||||
fs.unlinkSync(file.path);
|
||||
}
|
||||
});
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
// Delete an image
|
||||
router.delete('/:id', async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
if (!id) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Image ID is required',
|
||||
});
|
||||
}
|
||||
|
||||
const image = await prisma.recipeImage.findUnique({
|
||||
where: { id: parseInt(id) }
|
||||
});
|
||||
|
||||
if (!image) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'Image not found',
|
||||
});
|
||||
}
|
||||
|
||||
// Delete file from filesystem
|
||||
const fullPath = path.join(process.cwd(), '../..', image.filePath);
|
||||
if (fs.existsSync(fullPath)) {
|
||||
fs.unlinkSync(fullPath);
|
||||
}
|
||||
|
||||
// Delete from database
|
||||
await prisma.recipeImage.delete({
|
||||
where: { id: parseInt(id) }
|
||||
});
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
message: 'Image deleted successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
// Get all images for a recipe by recipe ID
|
||||
router.get('/recipe/:recipeId', async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
@@ -46,7 +215,7 @@ router.get('/serve/:imagePath(*)', (req: Request, res: Response, next: NextFunct
|
||||
|
||||
// Remove leading 'uploads/' if present to avoid duplication
|
||||
const cleanPath = imagePath.replace(/^uploads\//, '');
|
||||
const fullPath = path.join(process.cwd(), '../../uploads', cleanPath);
|
||||
const fullPath = path.join(getUploadsDir(), cleanPath);
|
||||
|
||||
console.log(`Serving image: ${imagePath} -> ${fullPath}`);
|
||||
|
||||
@@ -60,9 +229,17 @@ router.get('/serve/:imagePath(*)', (req: Request, res: Response, next: NextFunct
|
||||
});
|
||||
}
|
||||
|
||||
// Set CORS headers for images
|
||||
// Set CORS headers for images - support multiple origins including local network
|
||||
const allowedOrigins = ['http://localhost:5173', 'http://localhost:3000'];
|
||||
const origin = req.headers.origin;
|
||||
|
||||
// Check if CORS_ORIGIN is set to "*" for local network access
|
||||
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', // Cache for 1 year
|
||||
});
|
||||
|
||||
@@ -91,9 +91,17 @@ router.get('/:id', async (req: Request, res: Response, next: NextFunction) => {
|
||||
message: 'Recipe ID is required',
|
||||
});
|
||||
}
|
||||
|
||||
const recipeId = parseInt(id);
|
||||
if (isNaN(recipeId)) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Invalid recipe ID format',
|
||||
});
|
||||
}
|
||||
|
||||
const recipe = await prisma.recipe.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
where: { id: recipeId },
|
||||
include: {
|
||||
images: true,
|
||||
ingredientsList: true,
|
||||
|
||||
Reference in New Issue
Block a user