mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
Merge pull request #31 from theoleuthardt/fix/tools-backend-error
fix: remove-bg tmp directory create in dockerfile and set permissions
This commit is contained in:
commit
dda913df08
2 changed files with 13 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue