feat: download file
This commit is contained in:
parent
e020c0d6c9
commit
0808a27965
1 changed files with 34 additions and 1 deletions
|
|
@ -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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue