2018-07-24 06:21:21 +09:00
|
|
|
import * as Minio from 'minio';
|
2018-06-18 14:28:43 +09:00
|
|
|
import DriveFile, { DriveFileChunk, IDriveFile } from '../../models/drive-file';
|
2018-06-15 09:53:30 +09:00
|
|
|
import DriveFileThumbnail, { DriveFileThumbnailChunk } from '../../models/drive-file-thumbnail';
|
2018-07-24 06:21:21 +09:00
|
|
|
import config from '../../config';
|
2018-06-15 09:53:30 +09:00
|
|
|
|
|
|
|
export default async function(file: IDriveFile, isExpired = false) {
|
2018-07-25 08:01:12 +09:00
|
|
|
if (file.metadata.storage == 'minio') {
|
|
|
|
const minio = new Minio.Client(config.drive.config);
|
2018-08-16 07:17:04 +09:00
|
|
|
|
2018-07-25 08:01:12 +09:00
|
|
|
const obj = `${config.drive.prefix}/${file.metadata.storageProps.id}`;
|
|
|
|
await minio.removeObject(config.drive.bucket, obj);
|
2018-08-16 07:17:04 +09:00
|
|
|
|
|
|
|
if (file.metadata.thumbnailUrl) {
|
|
|
|
const thumbnailObj = `${config.drive.prefix}/${file.metadata.storageProps.id}-thumbnail`;
|
|
|
|
await minio.removeObject(config.drive.bucket, thumbnailObj);
|
|
|
|
}
|
2018-07-25 08:01:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// チャンクをすべて削除
|
|
|
|
await DriveFileChunk.remove({
|
|
|
|
files_id: file._id
|
|
|
|
});
|
|
|
|
|
|
|
|
await DriveFile.update({ _id: file._id }, {
|
|
|
|
$set: {
|
|
|
|
'metadata.deletedAt': new Date(),
|
|
|
|
'metadata.isExpired': isExpired
|
2018-06-15 09:53:30 +09:00
|
|
|
}
|
2018-07-25 08:01:12 +09:00
|
|
|
});
|
2018-06-15 09:53:30 +09:00
|
|
|
|
2018-07-25 08:01:12 +09:00
|
|
|
//#region サムネイルもあれば削除
|
|
|
|
const thumbnail = await DriveFileThumbnail.findOne({
|
|
|
|
'metadata.originalId': file._id
|
|
|
|
});
|
2018-06-15 09:53:30 +09:00
|
|
|
|
2018-07-25 08:01:12 +09:00
|
|
|
if (thumbnail) {
|
|
|
|
await DriveFileThumbnailChunk.remove({
|
|
|
|
files_id: thumbnail._id
|
2018-06-15 09:53:30 +09:00
|
|
|
});
|
|
|
|
|
2018-07-25 08:01:12 +09:00
|
|
|
await DriveFileThumbnail.remove({ _id: thumbnail._id });
|
2018-06-15 09:53:30 +09:00
|
|
|
}
|
2018-07-25 08:01:12 +09:00
|
|
|
//#endregion
|
2018-06-15 09:53:30 +09:00
|
|
|
}
|