2019-04-07 15:50:36 +03:00
|
|
|
import { DriveFile } from '../../models/entities/drive-file';
|
|
|
|
import { InternalStorage } from './internal-storage';
|
2019-04-16 20:11:22 +03:00
|
|
|
import { DriveFiles, Instances, Notes } from '../../models';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { driveChart, perUserDriveChart, instanceChart } from '../chart';
|
2019-05-27 10:54:47 +03:00
|
|
|
import { createDeleteObjectStorageFileJob } from '../../queue';
|
2019-07-01 15:12:14 +03:00
|
|
|
import { fetchMeta } from '../../misc/fetch-meta';
|
2019-07-28 03:49:02 +03:00
|
|
|
import { getS3 } from './s3';
|
2018-06-15 03:53:30 +03:00
|
|
|
|
2019-05-27 10:54:47 +03:00
|
|
|
export async function deleteFile(file: DriveFile, isExpired = false) {
|
2019-04-07 15:50:36 +03:00
|
|
|
if (file.storedInternal) {
|
2019-04-12 19:43:22 +03:00
|
|
|
InternalStorage.del(file.accessKey!);
|
2018-08-16 01:17:04 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (file.thumbnailUrl) {
|
2019-04-12 19:43:22 +03:00
|
|
|
InternalStorage.del(file.thumbnailAccessKey!);
|
2018-08-16 01:17:04 +03:00
|
|
|
}
|
2018-11-25 21:25:48 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (file.webpublicUrl) {
|
2019-04-12 19:43:22 +03:00
|
|
|
InternalStorage.del(file.webpublicAccessKey!);
|
2018-11-25 21:25:48 +02:00
|
|
|
}
|
2019-04-09 17:07:08 +03:00
|
|
|
} else if (!file.isLink) {
|
2019-05-27 10:54:47 +03:00
|
|
|
createDeleteObjectStorageFileJob(file.accessKey!);
|
2018-07-25 02:01:12 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (file.thumbnailUrl) {
|
2019-05-27 10:54:47 +03:00
|
|
|
createDeleteObjectStorageFileJob(file.thumbnailAccessKey!);
|
2018-06-15 03:53:30 +03:00
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (file.webpublicUrl) {
|
2019-05-27 10:54:47 +03:00
|
|
|
createDeleteObjectStorageFileJob(file.webpublicAccessKey!);
|
2019-04-07 15:50:36 +03:00
|
|
|
}
|
2018-06-15 03:53:30 +03:00
|
|
|
}
|
2018-08-18 17:56:44 +03:00
|
|
|
|
2019-07-01 15:12:14 +03: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function postProcess(file: DriveFile, isExpired = false) {
|
2019-04-07 15:50:36 +03:00
|
|
|
// リモートファイル期限切れ削除後は直リンクにする
|
2019-04-12 19:43:22 +03:00
|
|
|
if (isExpired && file.userHost !== null && file.uri != null) {
|
2019-04-07 15:50:36 +03:00
|
|
|
DriveFiles.update(file.id, {
|
2019-04-09 17:07:08 +03:00
|
|
|
isLink: true,
|
2019-04-07 15:50:36 +03:00
|
|
|
url: file.uri,
|
2019-05-27 10:54:47 +03:00
|
|
|
thumbnailUrl: file.uri,
|
|
|
|
webpublicUrl: file.uri
|
2018-11-25 21:25:48 +02:00
|
|
|
});
|
2019-04-07 15:50:36 +03:00
|
|
|
} else {
|
|
|
|
DriveFiles.delete(file.id);
|
2019-04-16 20:11:22 +03:00
|
|
|
|
|
|
|
// TODO: トランザクション
|
2019-04-16 20:37:37 +03:00
|
|
|
Notes.createQueryBuilder().delete()
|
|
|
|
.where(':id = ANY(fileIds)', { id: file.id })
|
2019-04-16 20:11:22 +03:00
|
|
|
.execute();
|
2018-11-25 21:25:48 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 17:56:44 +03:00
|
|
|
// 統計を更新
|
2018-10-22 23:36:35 +03:00
|
|
|
driveChart.update(file, false);
|
|
|
|
perUserDriveChart.update(file, false);
|
2019-04-07 15:50:36 +03:00
|
|
|
if (file.userHost !== null) {
|
2019-02-08 09:58:57 +02:00
|
|
|
instanceChart.updateDrive(file, false);
|
2019-04-07 15:50:36 +03:00
|
|
|
Instances.decrement({ host: file.userHost }, 'driveUsage', file.size);
|
|
|
|
Instances.decrement({ host: file.userHost }, 'driveFiles', 1);
|
2019-02-08 09:58:57 +02:00
|
|
|
}
|
2018-06-15 03:53:30 +03:00
|
|
|
}
|
2019-07-01 15:12:14 +03:00
|
|
|
|
|
|
|
export async function deleteObjectStorageFile(key: string) {
|
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2019-07-28 03:49:02 +03:00
|
|
|
const s3 = getS3(meta);
|
2019-07-01 15:12:14 +03:00
|
|
|
|
2019-07-28 03:49:02 +03:00
|
|
|
await s3.deleteObject({
|
|
|
|
Bucket: meta.objectStorageBucket!,
|
|
|
|
Key: key
|
|
|
|
}).promise();
|
2019-07-01 15:12:14 +03:00
|
|
|
}
|