feat: download file

This commit is contained in:
Methapon2001 2023-12-14 09:34:27 +07:00
parent e020c0d6c9
commit 0808a27965
No known key found for this signature in database
GPG key ID: 849924FEF46BD132

View file

@ -104,6 +104,13 @@ interface DeleteFileBody {
file: string;
}
interface DownloadFileBody {
/** @example ["แฟ้ม 1", "แฟ้ม 2", "แฟ้ม 3"] */
path: string[];
/** @example "ไฟล์ 1 แก้ไข.xlsx" */
file: string;
}
async function listFolder(path: string[]) {
const list = await new Promise<{ pathname: string; name: string }[]>((resolve, reject) => {
const item: { pathname: string; name: string }[] = [];
@ -601,7 +608,7 @@ export class StorageController extends Controller {
@Security("bearerAuth", ["management-role", "admin"])
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
public async deleteFile(@Body() body: DeleteFileBody) {
const pathname = body.path.join("/") + body.file;
const pathname = body.path.join("/") + `/${body.file}`;
await minioClient
.removeObject(DEFAULT_BUCKET, pathname)
@ -617,4 +624,30 @@ export class StorageController extends Controller {
return this.setStatus(HttpStatusCode.NO_CONTENT);
}
@Post("file/download")
@Tags("Download")
@Security("bearerAuth", ["management-role", "admin"])
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
public async downloadFile(@Body() body: DownloadFileBody) {
const pathname = body.path.join("/") + `/${body.file}`;
const search = await esClient.search<StorageFile & { attachment: Record<string, string> }>({
index: DEFAULT_INDEX!,
query: {
match: { pathname },
},
});
if (search && search.hits.hits.length === 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบไฟล์");
}
const { attachment, ...rest } = search.hits.hits[0]._source!;
return {
...rest,
downloadUrl: await minioClient.presignedGetObject(DEFAULT_BUCKET, pathname),
};
}
}