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