el.xwx.moe/lib/api/storage/moveFile.ts

38 lines
965 B
TypeScript
Raw Normal View History

2023-08-05 23:58:18 -05:00
import fs from "fs";
import path from "path";
import s3Client from "./s3Client";
import removeFile from "./removeFile";
export default async function moveFile(from: string, to: string) {
if (s3Client) {
const Bucket = process.env.SPACES_BUCKET_NAME;
2023-08-05 23:58:18 -05:00
const copyParams = {
Bucket: Bucket,
CopySource: `/${Bucket}/${from}`,
Key: to,
};
try {
2024-07-27 16:17:38 -05:00
s3Client.copyObject(copyParams, async (err: unknown) => {
2023-08-05 23:58:18 -05:00
if (err) {
console.error("Error copying the object:", err);
} else {
await removeFile({ filePath: from });
}
});
} catch (err) {
console.log("Error:", err);
}
} else {
const storagePath = process.env.STORAGE_FOLDER || "data";
const directory = (file: string) =>
path.join(process.cwd(), storagePath + "/" + file);
fs.rename(directory(from), directory(to), (err) => {
if (err) console.log("Error copying file:", err);
});
}
}