hippofish/src/services/drive/delete-file.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

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