From 7f9a35d7acf33c3c64ed451e10054729d25973cc Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 16 Aug 2018 21:23:31 +0900 Subject: [PATCH] Improve object storage key --- src/services/drive/add-file.ts | 16 ++++++++-------- src/services/drive/delete-file.ts | 8 ++++++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index 03bb1468bd..da0d3fd82f 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -35,20 +35,19 @@ async function save(path: string, name: string, type: string, hash: string, size 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}`; - const thumbnailObj = `${obj}-thumbnail`; + const key = `${config.drive.prefix}/${uuid.v4()}/${name}`; + const thumbnailKey = `${config.drive.prefix}/${uuid.v4()}/${name}.thumbnail.jpg`; const baseUrl = config.drive.baseUrl || `${ config.drive.config.secure ? 'https' : 'http' }://${ config.drive.config.endPoint }${ config.drive.config.port ? ':' + config.drive.config.port : '' }/${ config.drive.bucket }`; - await minio.putObject(config.drive.bucket, obj, fs.createReadStream(path), size, { + await minio.putObject(config.drive.bucket, key, fs.createReadStream(path), size, { 'Content-Type': type, 'Cache-Control': 'max-age=31536000, immutable' }); if (thumbnail) { - await minio.putObject(config.drive.bucket, thumbnailObj, thumbnail, size, { + await minio.putObject(config.drive.bucket, thumbnailKey, thumbnail, size, { 'Content-Type': 'image/jpeg', 'Cache-Control': 'max-age=31536000, immutable' }); @@ -58,10 +57,11 @@ async function save(path: string, name: string, type: string, hash: string, size withoutChunks: true, storage: 'minio', storageProps: { - id: id + key: key, + thumbnailKey: thumbnailKey }, - url: `${ baseUrl }/${ obj }`, - thumbnailUrl: thumbnail ? `${ baseUrl }/${ thumbnailObj }` : null + url: `${ baseUrl }/${ key }`, + thumbnailUrl: thumbnail ? `${ baseUrl }/${ thumbnailKey }` : null }); const file = await DriveFile.insert({ diff --git a/src/services/drive/delete-file.ts b/src/services/drive/delete-file.ts index a417d260fa..445d231d66 100644 --- a/src/services/drive/delete-file.ts +++ b/src/services/drive/delete-file.ts @@ -7,11 +7,15 @@ export default async function(file: IDriveFile, isExpired = false) { if (file.metadata.storage == 'minio') { const minio = new Minio.Client(config.drive.config); - const obj = `${config.drive.prefix}/${file.metadata.storageProps.id}`; + // 後方互換性のため、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}`; await minio.removeObject(config.drive.bucket, obj); if (file.metadata.thumbnailUrl) { - const thumbnailObj = `${config.drive.prefix}/${file.metadata.storageProps.id}-thumbnail`; + // 後方互換性のため、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`; await minio.removeObject(config.drive.bucket, thumbnailObj); } }