yumechi-no-kuni/src/services/drive/delete-file.ts
2019-05-27 16:54:47 +09:00

55 lines
1.6 KiB
TypeScript

import { DriveFile } from '../../models/entities/drive-file';
import { InternalStorage } from './internal-storage';
import { DriveFiles, Instances, Notes } from '../../models';
import { driveChart, perUserDriveChart, instanceChart } from '../chart';
import { createDeleteObjectStorageFileJob } from '../../queue';
export async function deleteFile(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) {
createDeleteObjectStorageFileJob(file.accessKey!);
if (file.thumbnailUrl) {
createDeleteObjectStorageFileJob(file.thumbnailAccessKey!);
}
if (file.webpublicUrl) {
createDeleteObjectStorageFileJob(file.webpublicAccessKey!);
}
}
// リモートファイル期限切れ削除後は直リンクにする
if (isExpired && file.userHost !== null && file.uri != null) {
DriveFiles.update(file.id, {
isLink: true,
url: file.uri,
thumbnailUrl: file.uri,
webpublicUrl: file.uri
});
} else {
DriveFiles.delete(file.id);
// TODO: トランザクション
Notes.createQueryBuilder().delete()
.where(':id = ANY(fileIds)', { id: file.id })
.execute();
}
// 統計を更新
driveChart.update(file, false);
perUserDriveChart.update(file, false);
if (file.userHost !== null) {
instanceChart.updateDrive(file, false);
Instances.decrement({ host: file.userHost }, 'driveUsage', file.size);
Instances.decrement({ host: file.userHost }, 'driveFiles', 1);
}
}