mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
fix: remove-bg tmp directory create in dockerfile and set permissions
This commit is contained in:
parent
0505620145
commit
f8ad566e61
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
|
USER fastify
|
||||||
|
|
||||||
|
RUN mkdir -p /app/tmp && chown -R fastify:fastify /app/tmp
|
||||||
|
|
||||||
EXPOSE 4000
|
EXPOSE 4000
|
||||||
|
|
||||||
ARG CORS_ALLOWED_ORIGIN
|
ARG CORS_ALLOWED_ORIGIN
|
||||||
|
|
|
||||||
|
|
@ -8,14 +8,19 @@ export async function removeBG(app: FastifyInstance) {
|
||||||
app.post(
|
app.post(
|
||||||
"/api/remove-bg",
|
"/api/remove-bg",
|
||||||
async (request: FastifyRequest, reply: FastifyReply) => {
|
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 sessionId = randomUUID();
|
||||||
const inputPath = path.join(tmpDir, `input-${sessionId}.png`);
|
const inputPath = path.join(tmpDir, `input-${sessionId}.png`);
|
||||||
const outputPath = path.join(tmpDir, `output-${sessionId}.png`);
|
const outputPath = path.join(tmpDir, `output-${sessionId}.png`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parts = request.parts();
|
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;
|
let fileBuffer: Buffer | null = null;
|
||||||
|
|
||||||
|
|
@ -53,7 +58,10 @@ export async function removeBG(app: FastifyInstance) {
|
||||||
});
|
});
|
||||||
|
|
||||||
const outputImageBuffer = await fs.readFile(outputPath);
|
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
|
reply
|
||||||
.header("Content-Type", "image/png")
|
.header("Content-Type", "image/png")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue