From b3b82e75957b8df70d481ce18f1826db4245c6a2 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 25 Jul 2018 08:01:12 +0900 Subject: [PATCH] Fix bug nado --- .config/example.yml | 3 +- src/config/types.ts | 5 +-- src/services/drive/add-file.ts | 49 +++++++++++------------- src/services/drive/delete-file.ts | 62 +++++++++++++++---------------- 4 files changed, 55 insertions(+), 64 deletions(-) diff --git a/.config/example.yml b/.config/example.yml index 59201fce0a..e787274fdc 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -58,8 +58,7 @@ drive: # OR - # storage: 'object-storage' - # service: 'minio' + # storage: 'minio' # bucket: # prefix: # config: diff --git a/src/config/types.ts b/src/config/types.ts index b2e28f7306..b554650820 100644 --- a/src/config/types.ts +++ b/src/config/types.ts @@ -51,9 +51,8 @@ export type Source = { drive?: { storage: string; - bucket: string; - prefix: string; - service?: string; + bucket?: string; + prefix?: string; config?: any; }; diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index 4d14325db2..91bf5b010c 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -21,36 +21,31 @@ import config from '../../config'; const log = debug('misskey:drive:add-file'); async function save(readable: stream.Readable, name: string, type: string, hash: string, size: number, metadata: any): Promise { - if (config.drive && config.drive.storage == 'object-storage') { - if (config.drive.service == 'minio') { + if (config.drive && config.drive.storage == 'minio') { + const minio = new Minio.Client(config.drive.config); + const id = uuid.v4(); + const obj = `${config.drive.prefix}/${id}`; + await minio.putObject(config.drive.bucket, obj, readable); - const minio = new Minio.Client(config.drive.config); - const id = uuid.v4(); - const obj = `${config.drive.prefix}/${id}`; - await minio.putObject(config.drive.bucket, obj, readable); + Object.assign(metadata, { + withoutChunks: true, + storage: 'minio', + 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 }` + }); - Object.assign(metadata, { - withoutChunks: true, - 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 }` - }); + const file = await DriveFile.insert({ + length: size, + uploadDate: new Date(), + md5: hash, + filename: name, + metadata: metadata, + contentType: type + }); - const file = await DriveFile.insert({ - length: size, - uploadDate: new Date(), - md5: hash, - filename: name, - metadata: metadata, - contentType: type - }); - - return file; - } else { - throw 'unknown storage type'; - } + return file; } else { // Get MongoDB GridFS bucket const bucket = await getDriveFileBucket(); diff --git a/src/services/drive/delete-file.ts b/src/services/drive/delete-file.ts index bf9c80f485..5494023f46 100644 --- a/src/services/drive/delete-file.ts +++ b/src/services/drive/delete-file.ts @@ -4,37 +4,35 @@ import DriveFileThumbnail, { DriveFileThumbnailChunk } from '../../models/drive- import config from '../../config'; export default async function(file: IDriveFile, isExpired = false) { - 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); - } - } else { - // チャンクをすべて削除 - await DriveFileChunk.remove({ - files_id: file._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 + if (file.metadata.storage == 'minio') { + const minio = new Minio.Client(config.drive.config); + const obj = `${config.drive.prefix}/${file.metadata.storageProps.id}`; + await minio.removeObject(config.drive.bucket, obj); } + + // チャンクをすべて削除 + await DriveFileChunk.remove({ + files_id: file._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 }