refactor: drop supported presigned url return

Now use its endpoint to upload file instead.
The server still not loaded by the api as it redirect to presigned url.
This commit is contained in:
Methapon Metanipat 2024-09-05 09:55:45 +07:00
parent 2af4e750b0
commit 8750c2cf98
2 changed files with 74 additions and 57 deletions

View file

@ -20,6 +20,7 @@ import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user";
import minio, { presignedGetObjectIfExist } from "../services/minio";
import { isSystem } from "../utils/keycloak";
import { fileLocation } from "../utils/minio";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
@ -99,18 +100,6 @@ type BranchUpdate = {
}[];
};
function lineImageLoc(id: string) {
return `branch/line-qr-${id}`;
}
function branchImageLoc(id: string) {
return `branch/branch-img-${id}`;
}
function mapImageLoc(id: string) {
return `branch/map-img-${id}`;
}
@Route("api/v1/branch")
@Tags("Branch")
export class BranchController extends Controller {
@ -306,10 +295,7 @@ export class BranchController extends Controller {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
}
return Object.assign(record, {
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)),
qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, lineImageLoc(record.id)),
});
return record;
}
@Post()
@ -395,6 +381,7 @@ export class BranchController extends Controller {
province: true,
district: true,
subDistrict: true,
contact: true,
createdBy: true,
updatedBy: true,
},
@ -430,21 +417,7 @@ export class BranchController extends Controller {
this.setStatus(HttpStatus.CREATED);
return Object.assign(record, {
contact: await prisma.branchContact.findMany({ where: { branchId: record.id } }),
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)),
imageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
branchImageLoc(record.id),
12 * 60 * 60,
),
qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, lineImageLoc(record.id)),
qrCodeImageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
lineImageLoc(record.id),
12 * 60 * 60,
),
});
return record;
}
@Put("{branchId}")
@ -554,20 +527,7 @@ export class BranchController extends Controller {
where: { id: branchId },
});
return Object.assign(record, {
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, branchImageLoc(record.id)),
imageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
branchImageLoc(record.id),
12 * 60 * 60,
),
qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, lineImageLoc(record.id)),
qrCodeImageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
lineImageLoc(record.id),
12 * 60 * 60,
),
});
return record;
}
@Delete("{branchId}")
@ -626,12 +586,14 @@ export class BranchController extends Controller {
});
}
await minio.removeObject(MINIO_BUCKET, lineImageLoc(branchId), {
forceDelete: true,
});
await minio.removeObject(MINIO_BUCKET, branchImageLoc(branchId), {
forceDelete: true,
});
await Promise.all([
minio.removeObject(MINIO_BUCKET, fileLocation.branch.line(branchId), {
forceDelete: true,
}),
minio.removeObject(MINIO_BUCKET, fileLocation.branch.image(branchId), {
forceDelete: true,
}),
]);
return data;
});
@ -639,7 +601,11 @@ export class BranchController extends Controller {
@Get("{branchId}/line-image")
async getLineImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
const url = await presignedGetObjectIfExist(MINIO_BUCKET, lineImageLoc(branchId), 60 * 60);
const url = await presignedGetObjectIfExist(
MINIO_BUCKET,
fileLocation.branch.line(branchId),
60 * 60,
);
if (!url) {
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
@ -674,13 +640,21 @@ export class BranchController extends Controller {
}
return req.res?.redirect(
await minio.presignedPutObject(MINIO_BUCKET, lineImageLoc(record.id), 12 * 60 * 60),
await minio.presignedPutObject(
MINIO_BUCKET,
fileLocation.branch.line(record.id),
12 * 60 * 60,
),
);
}
@Get("{branchId}/branch-image")
async getBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
const url = await presignedGetObjectIfExist(MINIO_BUCKET, branchImageLoc(branchId), 60 * 60);
const url = await presignedGetObjectIfExist(
MINIO_BUCKET,
fileLocation.branch.image(branchId),
60 * 60,
);
if (!url) {
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
@ -715,13 +689,21 @@ export class BranchController extends Controller {
}
return req.res?.redirect(
await minio.presignedPutObject(MINIO_BUCKET, branchImageLoc(record.id), 12 * 60 * 60),
await minio.presignedPutObject(
MINIO_BUCKET,
fileLocation.branch.image(record.id),
12 * 60 * 60,
),
);
}
@Get("{branchId}/map-image")
async getMapImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
const url = await presignedGetObjectIfExist(MINIO_BUCKET, mapImageLoc(branchId), 60 * 60);
const url = await presignedGetObjectIfExist(
MINIO_BUCKET,
fileLocation.branch.image(branchId),
60 * 60,
);
if (!url) {
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
@ -756,7 +738,11 @@ export class BranchController extends Controller {
}
return req.res?.redirect(
await minio.presignedPutObject(MINIO_BUCKET, mapImageLoc(record.id), 12 * 60 * 60),
await minio.presignedPutObject(
MINIO_BUCKET,
fileLocation.branch.map(record.id),
12 * 60 * 60,
),
);
}
}

31
src/utils/minio.ts Normal file
View file

@ -0,0 +1,31 @@
import minio from "../services/minio";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
}
const MINIO_BUCKET = process.env.MINIO_BUCKET;
export async function deleteFolder(path: string) {
new Promise<string[]>((resolve, reject) => {
const item: string[] = [];
const stream = minio.listObjectsV2(MINIO_BUCKET, path, true);
stream.on("data", (v) => v && v.name && item.push(v.name));
stream.on("end", () => resolve(item));
stream.on("error", () => reject(new Error("MinIO error.")));
}).then((list) => {
list.map(async (v) => {
await minio.removeObject(MINIO_BUCKET, v, { forceDelete: true });
});
});
}
export const fileLocation = {
branch: {
line: (id: string) => `branch/line-qr-${id}`,
image: (id: string) => `branch/branch-img-${id}`,
map: (id: string) => `branch/map-img-${id}`,
},
};