2018-07-23 15:21:21 -06:00
|
|
|
import * as Minio from 'minio';
|
|
|
|
import config from '../../config';
|
2019-04-07 06:50:36 -06:00
|
|
|
import { DriveFile } from '../../models/entities/drive-file';
|
|
|
|
import { InternalStorage } from './internal-storage';
|
2019-04-16 11:11:22 -06:00
|
|
|
import { DriveFiles, Instances, Notes } from '../../models';
|
2019-04-07 06:50:36 -06:00
|
|
|
import { driveChart, perUserDriveChart, instanceChart } from '../chart';
|
2018-06-14 18:53:30 -06:00
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
export default async function(file: DriveFile, isExpired = false) {
|
|
|
|
if (file.storedInternal) {
|
2019-04-12 10:43:22 -06:00
|
|
|
InternalStorage.del(file.accessKey!);
|
2018-08-15 16:17:04 -06:00
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
if (file.thumbnailUrl) {
|
2019-04-12 10:43:22 -06:00
|
|
|
InternalStorage.del(file.thumbnailAccessKey!);
|
2018-08-15 16:17:04 -06:00
|
|
|
}
|
2018-11-25 12:25:48 -07:00
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
if (file.webpublicUrl) {
|
2019-04-12 10:43:22 -06:00
|
|
|
InternalStorage.del(file.webpublicAccessKey!);
|
2018-11-25 12:25:48 -07:00
|
|
|
}
|
2019-04-09 08:07:08 -06:00
|
|
|
} else if (!file.isLink) {
|
2019-04-12 10:43:22 -06:00
|
|
|
const minio = new Minio.Client(config.drive!.config);
|
2018-07-24 17:01:12 -06:00
|
|
|
|
2019-04-12 10:43:22 -06:00
|
|
|
await minio.removeObject(config.drive!.bucket!, file.accessKey!);
|
2018-07-24 17:01:12 -06:00
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
if (file.thumbnailUrl) {
|
2019-04-12 10:43:22 -06:00
|
|
|
await minio.removeObject(config.drive!.bucket!, file.thumbnailAccessKey!);
|
2018-06-14 18:53:30 -06:00
|
|
|
}
|
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
if (file.webpublicUrl) {
|
2019-04-12 10:43:22 -06:00
|
|
|
await minio.removeObject(config.drive!.bucket!, file.webpublicAccessKey!);
|
2019-04-07 06:50:36 -06:00
|
|
|
}
|
2018-06-14 18:53:30 -06:00
|
|
|
}
|
2018-08-18 08:56:44 -06:00
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
// リモートファイル期限切れ削除後は直リンクにする
|
2019-04-12 10:43:22 -06:00
|
|
|
if (isExpired && file.userHost !== null && file.uri != null) {
|
2019-04-07 06:50:36 -06:00
|
|
|
DriveFiles.update(file.id, {
|
2019-04-09 08:07:08 -06:00
|
|
|
isLink: true,
|
2019-04-07 06:50:36 -06:00
|
|
|
url: file.uri,
|
|
|
|
thumbnailUrl: null,
|
|
|
|
webpublicUrl: null
|
2018-11-25 12:25:48 -07:00
|
|
|
});
|
2019-04-07 06:50:36 -06:00
|
|
|
} else {
|
|
|
|
DriveFiles.delete(file.id);
|
2019-04-16 11:11:22 -06:00
|
|
|
|
|
|
|
// TODO: トランザクション
|
|
|
|
Notes.createQueryBuilder('note').delete()
|
2019-04-16 11:19:49 -06:00
|
|
|
.where(':id = ANY(note.fileIds)', { id: file.id })
|
2019-04-16 11:11:22 -06:00
|
|
|
.execute();
|
2018-11-25 12:25:48 -07:00
|
|
|
}
|
|
|
|
|
2018-08-18 08:56:44 -06:00
|
|
|
// 統計を更新
|
2018-10-22 14:36:35 -06:00
|
|
|
driveChart.update(file, false);
|
|
|
|
perUserDriveChart.update(file, false);
|
2019-04-07 06:50:36 -06:00
|
|
|
if (file.userHost !== null) {
|
2019-02-08 00:58:57 -07:00
|
|
|
instanceChart.updateDrive(file, false);
|
2019-04-07 06:50:36 -06:00
|
|
|
Instances.decrement({ host: file.userHost }, 'driveUsage', file.size);
|
|
|
|
Instances.decrement({ host: file.userHost }, 'driveFiles', 1);
|
2019-02-08 00:58:57 -07:00
|
|
|
}
|
2018-06-14 18:53:30 -06:00
|
|
|
}
|