fix: move file failed

This commit is contained in:
Methapon2001 2023-12-14 15:05:05 +07:00
parent bbeda11fc6
commit 73673f7ae3
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
4 changed files with 22 additions and 17 deletions

View file

@ -49,4 +49,4 @@ server.listen(PORT, "0.0.0.0", () =>
console.log(`[APP] Application is running on http://localhost:${PORT}`),
);
rabbitmq.init(amqHandler).catch((e) => console.error(e));
// rabbitmq.init(amqHandler).catch((e) => console.error(e));

View file

@ -467,7 +467,7 @@ export class StorageController extends Controller {
@Put("file")
@Tags("Storage File")
@Security("bearerAuth", ["management-role", "admin"])
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
public async moveFile(
@Request() request: { user: { preferred_username: string } },
@Body() body: PutFileBody,
@ -494,17 +494,16 @@ export class StorageController extends Controller {
});
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบไฟล์ดังกล่าว");
}
if (body.to && !(await checkPathExist(DEFAULT_BUCKET, body.to.path.join("/")))) {
throw new HttpError(HttpStatusCode.PRECONDITION_FAILED, "ไม่พบตำแหน่งที่ต้องการย้าย");
}
if (
body.to &&
(await checkFileExist(DEFAULT_BUCKET, body.to.path.join("/") + `/${body.to.file}`))
) {
throw new HttpError(
HttpStatusCode.PRECONDITION_FAILED,
"พบไฟล์ในต้ำแหน่งปลายทาง ไม่สามารถย้ายได้",
);
if (body.to && JSON.stringify(body.from) !== JSON.stringify(body.to)) {
if (!(await checkPathExist(DEFAULT_BUCKET, body.to.path.join("/")))) {
throw new HttpError(HttpStatusCode.PRECONDITION_FAILED, "ไม่พบตำแหน่งที่ต้องการย้าย");
}
if (await checkFileExist(DEFAULT_BUCKET, body.to.path.join("/") + `/${body.to.file}`)) {
throw new HttpError(
HttpStatusCode.PRECONDITION_FAILED,
"พบไฟล์ในต้ำแหน่งปลายทาง ไม่สามารถย้ายได้",
);
}
}
if (!search.hits.hits[0]._source) {
// This should not possible.
@ -526,7 +525,7 @@ export class StorageController extends Controller {
updatedBy: request.user.preferred_username,
};
if (from && to) {
if (from && to && JSON.stringify(from) !== JSON.stringify(to)) {
const src = [DEFAULT_BUCKET, ...from.path, ""].join("/") + from.file;
const dst = to.path.join("/") + `/${to.file}`;
@ -544,11 +543,15 @@ export class StorageController extends Controller {
...metadata,
path: to.path.join("/") + "/",
pathname: dst,
fileName: to.file,
...dateMeta,
},
refresh: "wait_for",
})
.then(async () => await minioClient.removeObject(DEFAULT_INDEX, src))
.then(
async () =>
await minioClient.removeObject(DEFAULT_BUCKET, from.path.join("/") + `/${from.file}`),
)
.catch((e) => console.error(`ElasticSearch Error: ${e}`));
io.getInstance()?.emit("FileMove", {
@ -558,6 +561,7 @@ export class StorageController extends Controller {
...metadata,
path: to.path.join("/") + "/",
pathname: dst,
fileName: to.file,
...dateMeta,
},
});
@ -566,6 +570,7 @@ export class StorageController extends Controller {
const presignedUrl = await minioClient.presignedPutObject(DEFAULT_BUCKET, dst);
return { uploadUrl: presignedUrl };
}
return this.setStatus(HttpStatusCode.NO_CONTENT);
}
}

View file

@ -891,7 +891,7 @@ export function RegisterRoutes(app: Router) {
const promise = controller.moveFile.apply(controller, validatedArgs as any);
promiseHandler(controller, promise, response, 204, next);
promiseHandler(controller, promise, response, 200, next);
} catch (err) {
return next(err);
}

View file

@ -2298,7 +2298,7 @@
"put": {
"operationId": "MoveFile",
"responses": {
"204": {
"200": {
"description": "สำเร็จ",
"content": {
"application/json": {