2023-01-13 05:40:33 +01:00
|
|
|
import type { DriveFile } from "@/models/entities/drive-file.js";
|
|
|
|
import { InternalStorage } from "./internal-storage.js";
|
|
|
|
import { DriveFiles, Instances } from "@/models/index.js";
|
|
|
|
import {
|
|
|
|
driveChart,
|
|
|
|
perUserDriveChart,
|
|
|
|
instanceChart,
|
|
|
|
} from "@/services/chart/index.js";
|
|
|
|
import { createDeleteObjectStorageFileJob } from "@/queue/index.js";
|
|
|
|
import { fetchMeta } from "@/misc/fetch-meta.js";
|
|
|
|
import { getS3 } from "./s3.js";
|
|
|
|
import { v4 as uuid } from "uuid";
|
2018-06-15 02:53:30 +02:00
|
|
|
|
2019-05-27 09:54:47 +02:00
|
|
|
export async function deleteFile(file: DriveFile, isExpired = false) {
|
2019-04-07 14:50:36 +02:00
|
|
|
if (file.storedInternal) {
|
2019-04-12 18:43:22 +02:00
|
|
|
InternalStorage.del(file.accessKey!);
|
2018-08-16 00:17:04 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (file.thumbnailUrl) {
|
2019-04-12 18:43:22 +02:00
|
|
|
InternalStorage.del(file.thumbnailAccessKey!);
|
2018-08-16 00:17:04 +02:00
|
|
|
}
|
2018-11-25 20:25:48 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (file.webpublicUrl) {
|
2019-04-12 18:43:22 +02:00
|
|
|
InternalStorage.del(file.webpublicAccessKey!);
|
2018-11-25 20:25:48 +01:00
|
|
|
}
|
2019-04-09 16:07:08 +02:00
|
|
|
} else if (!file.isLink) {
|
2019-05-27 09:54:47 +02:00
|
|
|
createDeleteObjectStorageFileJob(file.accessKey!);
|
2018-07-25 01:01:12 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (file.thumbnailUrl) {
|
2019-05-27 09:54:47 +02:00
|
|
|
createDeleteObjectStorageFileJob(file.thumbnailAccessKey!);
|
2018-06-15 02:53:30 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (file.webpublicUrl) {
|
2019-05-27 09:54:47 +02:00
|
|
|
createDeleteObjectStorageFileJob(file.webpublicAccessKey!);
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
2018-06-15 02:53:30 +02:00
|
|
|
}
|
2018-08-18 16:56:44 +02:00
|
|
|
|
2019-07-01 14:12:14 +02:00
|
|
|
postProcess(file, isExpired);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteFileSync(file: DriveFile, isExpired = false) {
|
|
|
|
if (file.storedInternal) {
|
|
|
|
InternalStorage.del(file.accessKey!);
|
|
|
|
|
|
|
|
if (file.thumbnailUrl) {
|
|
|
|
InternalStorage.del(file.thumbnailAccessKey!);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.webpublicUrl) {
|
|
|
|
InternalStorage.del(file.webpublicAccessKey!);
|
|
|
|
}
|
|
|
|
} else if (!file.isLink) {
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
promises.push(deleteObjectStorageFile(file.accessKey!));
|
|
|
|
|
|
|
|
if (file.thumbnailUrl) {
|
|
|
|
promises.push(deleteObjectStorageFile(file.thumbnailAccessKey!));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.webpublicUrl) {
|
|
|
|
promises.push(deleteObjectStorageFile(file.webpublicAccessKey!));
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
postProcess(file, isExpired);
|
|
|
|
}
|
|
|
|
|
2020-02-13 15:08:33 +01:00
|
|
|
async function postProcess(file: DriveFile, isExpired = false) {
|
2019-04-07 14:50:36 +02:00
|
|
|
// リモートファイル期限切れ削除後は直リンクにする
|
2019-04-12 18:43:22 +02:00
|
|
|
if (isExpired && file.userHost !== null && file.uri != null) {
|
2019-04-07 14:50:36 +02:00
|
|
|
DriveFiles.update(file.id, {
|
2019-04-09 16:07:08 +02:00
|
|
|
isLink: true,
|
2019-04-07 14:50:36 +02:00
|
|
|
url: file.uri,
|
2020-01-01 18:45:05 +01:00
|
|
|
thumbnailUrl: null,
|
|
|
|
webpublicUrl: null,
|
2021-05-30 06:48:23 +02:00
|
|
|
storedInternal: false,
|
2019-12-31 09:23:47 +01:00
|
|
|
// ローカルプロキシ用
|
|
|
|
accessKey: uuid(),
|
2023-01-13 05:40:33 +01:00
|
|
|
thumbnailAccessKey: `thumbnail-${uuid()}`,
|
|
|
|
webpublicAccessKey: `webpublic-${uuid()}`,
|
2018-11-25 20:25:48 +01:00
|
|
|
});
|
2019-04-07 14:50:36 +02:00
|
|
|
} else {
|
|
|
|
DriveFiles.delete(file.id);
|
2018-11-25 20:25:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-18 16:56:44 +02:00
|
|
|
// 統計を更新
|
2018-10-22 22:36:35 +02:00
|
|
|
driveChart.update(file, false);
|
|
|
|
perUserDriveChart.update(file, false);
|
2019-04-07 14:50:36 +02:00
|
|
|
if (file.userHost !== null) {
|
2019-02-08 08:58:57 +01:00
|
|
|
instanceChart.updateDrive(file, false);
|
|
|
|
}
|
2018-06-15 02:53:30 +02:00
|
|
|
}
|
2019-07-01 14:12:14 +02:00
|
|
|
|
|
|
|
export async function deleteObjectStorageFile(key: string) {
|
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2019-07-28 02:49:02 +02:00
|
|
|
const s3 = getS3(meta);
|
2019-07-01 14:12:14 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
await s3
|
|
|
|
.deleteObject({
|
|
|
|
Bucket: meta.objectStorageBucket!,
|
|
|
|
Key: key,
|
|
|
|
})
|
|
|
|
.promise();
|
2019-07-01 14:12:14 +02:00
|
|
|
}
|