diff --git a/README.md b/README.md index 98ce089..d665fa8 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ module.exports = { serviceAccount: {}, // replace `{}` with your serviceAccount JSON object baseUrl: 'https://storage.googleapis.com/{bucket-name}', basePath: '', + enableMediaReplace: true // instead of deleting, media will be replaced }, }, }, diff --git a/lib/provider.js b/lib/provider.js index 62ddb31..d200158 100644 --- a/lib/provider.js +++ b/lib/provider.js @@ -159,7 +159,7 @@ const generateUploadFileName = (basePath, file) => { * @returns {Promise<{fileAttributes: {metadata: (*|{contentDisposition: string, cacheControl: string}), gzip: (string|boolean|((buf: InputType, callback: CompressCallback) => void)|((buf: InputType, options: ZlibOptions, callback: CompressCallback) => void)|gzip|*), contentType: (string|string|*)}, fullFileName: (string|Promise|*|string)}>} */ const prepareUploadFile = async (file, config, basePath, GCS) => { - let deleteFile = false; + let fileExist = false; const fullFileName = typeof config.generateUploadFileName === 'function' ? await config.generateUploadFileName(file) @@ -171,7 +171,7 @@ const prepareUploadFile = async (file, config, basePath, GCS) => { const bucketFile = bucket.file(fullFileName); const [fileExists] = await bucketFile.exists(); if (fileExists) { - deleteFile = true; + fileExist = true; } const asciiFileName = file.name.normalize('NFKD').replace(/[\u0300-\u036f]/g, ''); const fileAttributes = { @@ -190,7 +190,7 @@ const prepareUploadFile = async (file, config, basePath, GCS) => { fileAttributes.public = config.publicFiles; } - return { fileAttributes, bucketFile, fullFileName, deleteFile }; + return { fileAttributes, bucketFile, fullFileName, fileExist }; }; /** @@ -222,13 +222,13 @@ const init = (providerConfig) => { return { async upload(file) { try { - const { fileAttributes, bucketFile, fullFileName, deleteFile } = await prepareUploadFile( + const { fileAttributes, bucketFile, fullFileName, fileExist } = await prepareUploadFile( file, config, basePath, GCS ); - if (deleteFile) { + if (fileExist && !config.enableMediaReplace) { console.info('File already exists. Try to remove it.'); await this.delete(file); } @@ -245,13 +245,13 @@ const init = (providerConfig) => { }, async uploadStream(file) { try { - const { fileAttributes, bucketFile, fullFileName, deleteFile } = await prepareUploadFile( + const { fileAttributes, bucketFile, fullFileName, fileExist } = await prepareUploadFile( file, config, basePath, GCS ); - if (deleteFile) { + if (fileExist && !config.enableMediaReplace) { console.info('File already exists. Try to remove it.'); await this.delete(file); } diff --git a/test/lib/provider.js b/test/lib/provider.js index b8d5635..8fc741c 100644 --- a/test/lib/provider.js +++ b/test/lib/provider.js @@ -785,6 +785,67 @@ describe('/lib/provider.js', () => { assert.equal(assertionsCount, 9); mockRequire.stop('@google-cloud/storage'); }); + + it('should replace replace media', async () => { + const baseUrl = 'https://storage.googleapis.com'; + const url = `${baseUrl}/random-bucket/l0ngH45h/l0ngH45h.jpeg`; + + const fileData = { + ext: '.JPEG', + buffer: 'file buffer information', + mime: 'image/jpeg', + name: 'people coding.JPEG', + related: [], + hash: 'l0ngH45h', + url, + }; + + const saveExpectedArgs = [ + 'file buffer information', + { + gzip: 'auto', + contentType: 'image/jpeg', + metadata: { + cacheControl: 'public, max-age=604800', + contentDisposition: 'inline; filename="people coding.JPEG"', + }, + public: true, + }, + ]; + + const fileMock = createFileMock({ saveExpectedArgs }); + const expectedFileNames = [ + 'l0ngH45h/l0ngH45h.jpeg', + 'l0ngH45h/l0ngH45h.jpeg', + 'l0ngH45h/l0ngH45h.jpeg', + ]; + const bucketMock = createBucketMock({ fileMock, expectedFileNames }); + const Storage = class { + bucket(bucketName) { + assertionsCount += 1; + assert.equal(bucketName, 'random-bucket'); + console.count(); + return bucketMock; + } + }; + + mockRequire('@google-cloud/storage', { Storage }); + const provider = mockRequire.reRequire('../../lib/provider'); + const config = { + serviceAccount: { + project_id: '123', + client_email: 'my@email.org', + private_key: 'a random key', + }, + enableMediaReplace: true, + bucketName: 'random-bucket', + cacheMaxAge: 60 * 60 * 24 * 7, // one week + }; + const providerInstance = provider.init(config); + await providerInstance.upload(fileData); + assert.equal(assertionsCount, 6); + mockRequire.stop('@google-cloud/storage'); + }); }); }); });