diff --git a/backend/Dockerfile b/backend/Dockerfile index 03e81f9..fa2258d 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -60,6 +60,8 @@ COPY --from=builder --chown=fastify:nodejs /app/package.json ./package.json USER fastify +RUN mkdir -p /app/tmp && chown -R fastify:fastify /app/tmp + EXPOSE 4000 ARG CORS_ALLOWED_ORIGIN diff --git a/backend/src/routes/removebg.route.ts b/backend/src/routes/removebg.route.ts index ed55bf9..6a1e3dc 100644 --- a/backend/src/routes/removebg.route.ts +++ b/backend/src/routes/removebg.route.ts @@ -8,14 +8,19 @@ export async function removeBG(app: FastifyInstance) { app.post( "/api/remove-bg", async (request: FastifyRequest, reply: FastifyReply) => { - const tmpDir = path.join(process.cwd(), "tmp"); + const tmpDir = + process.env.NODE_ENV === "production" + ? "/app/tmp" + : path.join(process.cwd(), "tmp"); const sessionId = randomUUID(); const inputPath = path.join(tmpDir, `input-${sessionId}.png`); const outputPath = path.join(tmpDir, `output-${sessionId}.png`); try { const parts = request.parts(); - await fs.mkdir(tmpDir, { recursive: true }); + if (process.env.NODE_ENV === "development") { + await fs.mkdir(tmpDir, { recursive: true }); + } let fileBuffer: Buffer | null = null; @@ -53,7 +58,10 @@ export async function removeBG(app: FastifyInstance) { }); const outputImageBuffer = await fs.readFile(outputPath); - await Promise.all([fs.unlink(inputPath), fs.unlink(outputPath)]); + await Promise.all([ + fs.unlink(inputPath).catch(() => {}), + fs.unlink(outputPath).catch(() => {}), + ]); reply .header("Content-Type", "image/png")