2018-07-23 21:21:21 +00:00
|
|
|
import * as Minio from 'minio';
|
2018-06-18 05:28:43 +00:00
|
|
|
import DriveFile, { DriveFileChunk, IDriveFile } from '../../models/drive-file';
|
2018-06-15 00:53:30 +00:00
|
|
|
import DriveFileThumbnail, { DriveFileThumbnailChunk } from '../../models/drive-file-thumbnail';
|
2018-07-23 21:21:21 +00:00
|
|
|
import config from '../../config';
|
2019-02-07 19:31:33 +00:00
|
|
|
import driveChart from '../../services/chart/drive';
|
|
|
|
import perUserDriveChart from '../../services/chart/per-user-drive';
|
2019-02-08 07:58:57 +00:00
|
|
|
import instanceChart from '../../services/chart/instance';
|
2018-11-25 19:25:48 +00:00
|
|
|
import DriveFileWebpublic, { DriveFileWebpublicChunk } from '../../models/drive-file-webpublic';
|
2019-02-08 07:58:57 +00:00
|
|
|
import Instance from '../../models/instance';
|
|
|
|
import { isRemoteUser } from '../../models/user';
|
2018-06-15 00:53:30 +00:00
|
|
|
|
|
|
|
export default async function(file: IDriveFile, isExpired = false) {
|
2018-07-24 23:01:12 +00:00
|
|
|
if (file.metadata.storage == 'minio') {
|
|
|
|
const minio = new Minio.Client(config.drive.config);
|
2018-08-15 22:17:04 +00:00
|
|
|
|
2018-08-16 12:23:31 +00:00
|
|
|
// 後方互換性のため、file.metadata.storageProps.key があるかどうかチェックしています。
|
|
|
|
// 将来的には const obj = file.metadata.storageProps.key; とします。
|
|
|
|
const obj = file.metadata.storageProps.key ? file.metadata.storageProps.key : `${config.drive.prefix}/${file.metadata.storageProps.id}`;
|
2018-07-24 23:01:12 +00:00
|
|
|
await minio.removeObject(config.drive.bucket, obj);
|
2018-08-15 22:17:04 +00:00
|
|
|
|
|
|
|
if (file.metadata.thumbnailUrl) {
|
2018-08-16 12:23:31 +00:00
|
|
|
// 後方互換性のため、file.metadata.storageProps.thumbnailKey があるかどうかチェックしています。
|
|
|
|
// 将来的には const thumbnailObj = file.metadata.storageProps.thumbnailKey; とします。
|
|
|
|
const thumbnailObj = file.metadata.storageProps.thumbnailKey ? file.metadata.storageProps.thumbnailKey : `${config.drive.prefix}/${file.metadata.storageProps.id}-thumbnail`;
|
2018-08-15 22:17:04 +00:00
|
|
|
await minio.removeObject(config.drive.bucket, thumbnailObj);
|
|
|
|
}
|
2018-11-25 19:25:48 +00:00
|
|
|
|
|
|
|
if (file.metadata.webpublicUrl) {
|
|
|
|
const webpublicObj = file.metadata.storageProps.webpublicKey ? file.metadata.storageProps.webpublicKey : `${config.drive.prefix}/${file.metadata.storageProps.id}-original`;
|
|
|
|
await minio.removeObject(config.drive.bucket, webpublicObj);
|
|
|
|
}
|
2018-07-24 23:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// チャンクをすべて削除
|
|
|
|
await DriveFileChunk.remove({
|
|
|
|
files_id: file._id
|
|
|
|
});
|
|
|
|
|
2018-12-12 02:46:35 +00:00
|
|
|
const set = {
|
|
|
|
metadata: {
|
|
|
|
deletedAt: new Date(),
|
|
|
|
isExpired: isExpired
|
2018-06-15 00:53:30 +00:00
|
|
|
}
|
2018-12-12 02:46:35 +00:00
|
|
|
} as any;
|
|
|
|
|
|
|
|
// リモートファイル期限切れ削除後は直リンクにする
|
|
|
|
if (isExpired && file.metadata && file.metadata._user && file.metadata._user.host != null) {
|
|
|
|
set.metadata.withoutChunks = true;
|
|
|
|
set.metadata.isRemote = true;
|
|
|
|
set.metadata.url = file.metadata.uri;
|
|
|
|
set.metadata.thumbnailUrl = undefined;
|
|
|
|
set.metadata.webpublicUrl = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
await DriveFile.update({ _id: file._id }, {
|
|
|
|
$set: set
|
2018-07-24 23:01:12 +00:00
|
|
|
});
|
2018-06-15 00:53:30 +00:00
|
|
|
|
2018-07-24 23:01:12 +00:00
|
|
|
//#region サムネイルもあれば削除
|
|
|
|
const thumbnail = await DriveFileThumbnail.findOne({
|
|
|
|
'metadata.originalId': file._id
|
|
|
|
});
|
2018-06-15 00:53:30 +00:00
|
|
|
|
2018-07-24 23:01:12 +00:00
|
|
|
if (thumbnail) {
|
|
|
|
await DriveFileThumbnailChunk.remove({
|
|
|
|
files_id: thumbnail._id
|
2018-06-15 00:53:30 +00:00
|
|
|
});
|
|
|
|
|
2018-07-24 23:01:12 +00:00
|
|
|
await DriveFileThumbnail.remove({ _id: thumbnail._id });
|
2018-06-15 00:53:30 +00:00
|
|
|
}
|
2018-07-24 23:01:12 +00:00
|
|
|
//#endregion
|
2018-08-18 14:56:44 +00:00
|
|
|
|
2018-11-25 19:25:48 +00:00
|
|
|
//#region Web公開用もあれば削除
|
|
|
|
const webpublic = await DriveFileWebpublic.findOne({
|
|
|
|
'metadata.originalId': file._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (webpublic) {
|
|
|
|
await DriveFileWebpublicChunk.remove({
|
|
|
|
files_id: webpublic._id
|
|
|
|
});
|
|
|
|
|
|
|
|
await DriveFileWebpublic.remove({ _id: webpublic._id });
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-08-18 14:56:44 +00:00
|
|
|
// 統計を更新
|
2018-10-22 20:36:35 +00:00
|
|
|
driveChart.update(file, false);
|
|
|
|
perUserDriveChart.update(file, false);
|
2019-02-08 07:58:57 +00:00
|
|
|
if (isRemoteUser(file.metadata._user)) {
|
|
|
|
instanceChart.updateDrive(file, false);
|
|
|
|
Instance.update({ host: file.metadata._user.host }, {
|
|
|
|
$inc: {
|
|
|
|
driveUsage: -file.length,
|
|
|
|
driveFiles: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-06-15 00:53:30 +00:00
|
|
|
}
|