From a048939cf1714f20ae00aea0d4f2595241bcbd13 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 24 Jul 2018 06:21:21 +0900 Subject: [PATCH] wip --- src/models/drive-file.ts | 2 ++ src/services/drive/add-file.ts | 6 ++-- src/services/drive/delete-file.ts | 52 ++++++++++++++++++------------- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/models/drive-file.ts b/src/models/drive-file.ts index 0b7212665..f197f86d4 100644 --- a/src/models/drive-file.ts +++ b/src/models/drive-file.ts @@ -34,6 +34,8 @@ export type IMetadata = { src?: string; deletedAt?: Date; withoutChunks?: boolean; + storage?: string; + storageProps?: any; isSensitive?: boolean; }; diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index 4f379a25b..ab9353c9f 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -35,9 +35,11 @@ async function save(readable: stream.Readable, name: string, type: string, hash: await minio.putObject(config.drive.bucket, obj, readable); Object.assign(metadata, { - storage: 'object-storage', withoutChunks: true, - obj: id, + storage: 'object-storage', + storageProps: { + id: id + }, url: `${ config.drive.config.secure ? 'https' : 'http' }://${ config.drive.config.endPoint }${ config.drive.config.port ? ':' + config.drive.config.port : '' }/${ config.drive.bucket }/${ obj }` }); diff --git a/src/services/drive/delete-file.ts b/src/services/drive/delete-file.ts index 4ac60439b..bf9c80f48 100644 --- a/src/services/drive/delete-file.ts +++ b/src/services/drive/delete-file.ts @@ -1,30 +1,40 @@ +import * as Minio from 'minio'; import DriveFile, { DriveFileChunk, IDriveFile } from '../../models/drive-file'; import DriveFileThumbnail, { DriveFileThumbnailChunk } from '../../models/drive-file-thumbnail'; +import config from '../../config'; export default async function(file: IDriveFile, isExpired = false) { - // チャンクをすべて削除 - await DriveFileChunk.remove({ - files_id: file._id - }); - - await DriveFile.update({ _id: file._id }, { - $set: { - 'metadata.deletedAt': new Date(), - 'metadata.isExpired': isExpired + 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); } - }); - - //#region サムネイルもあれば削除 - const thumbnail = await DriveFileThumbnail.findOne({ - 'metadata.originalId': file._id - }); - - if (thumbnail) { - await DriveFileThumbnailChunk.remove({ - files_id: thumbnail._id + } else { + // チャンクをすべて削除 + await DriveFileChunk.remove({ + files_id: file._id }); - await DriveFileThumbnail.remove({ _id: thumbnail._id }); + await DriveFile.update({ _id: file._id }, { + $set: { + 'metadata.deletedAt': new Date(), + 'metadata.isExpired': isExpired + } + }); + + //#region サムネイルもあれば削除 + const thumbnail = await DriveFileThumbnail.findOne({ + 'metadata.originalId': file._id + }); + + if (thumbnail) { + await DriveFileThumbnailChunk.remove({ + files_id: thumbnail._id + }); + + await DriveFileThumbnail.remove({ _id: thumbnail._id }); + } + //#endregion } - //#endregion }