Skip to content

Commit

Permalink
Refactor upload and uploadStream to avoid duplicate code and miss…
Browse files Browse the repository at this point in the history
…ing code fix

Fix #169 issue with boolean, add an `eval` for boolean value read "like a string"
Improve README.md with missing example for `skipCheckBucket` option (not documented)
Upgrade package
Update to latest strapi release 4.10.2
  • Loading branch information
Lith committed May 10, 2023
1 parent 46f9874 commit b5fa17e
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 272 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,19 @@ Define base path to save each media document.

#### `publicFiles`:

Boolean to define a public attribute to file when it upload to storage.
Boolean to define a public attribute to file when upload file to storage.
- Default value : `true`
- Optional

#### `uniform`:

Boolean to define uniform access, when uniform bucket-level access is enabled.
Boolean to define `uniform` access, when uniform bucket-level access is enabled.
- Default value : `false`
- Optional

#### `skipCheckBucket`:

Boolean to define `skipCheckBucket`, when skipCheckBucket is enabled, we skip to check if the bucket exist. It's useful for private bucket.
- Default value : `false`
- Optional

Expand Down
1 change: 1 addition & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ For old version, please refer to older release.

| Version | Supported |
|---------|--------------------|
| 4.10.2 | :white_check_mark: |
| 4.10.1 | :white_check_mark: |
| 4.6.1 | :white_check_mark: |
| 4.5.6 | :white_check_mark: |
Expand Down
127 changes: 64 additions & 63 deletions lib/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,11 @@ const checkServiceAccount = (config = {}) => {
if (!config.basePath) {
config.basePath = '';
}
if (config.publicFiles === undefined) {
config.publicFiles = true;
}
if (!config.uniform) {
config.uniform = false;
}
if (!config.skipCheckBucket) {
config.skipCheckBucket = false;
}

/** Check or set default boolean optional variable */
config.publicFiles = eval(config.publicFiles) || true; // default value
config.uniform = eval(config.uniform) || false; // default value
config.skipCheckBucket = eval(config.skipCheckBucket) || false; // default value

let serviceAccount;
if (config.serviceAccount) {
Expand Down Expand Up @@ -132,6 +128,49 @@ const generateUploadFileName = (basePath, file) => {
return `${basePath}${filePath}${fileName}${extension}`;
};

/**
* Prepare file before upload
* @param file
* @param config
* @param basePath
* @param GCS
* @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>|*|string)}>}
*/
const prepareUploadFile = async (file, config, basePath, GCS) => {
let deleteFile = false;
const fullFileName =
typeof config.generateUploadFileName === 'function'
? await config.generateUploadFileName(file)
: generateUploadFileName(basePath, file);
if (!config.skipCheckBucket) {
await checkBucket(GCS, config.bucketName);
}
const bucket = GCS.bucket(config.bucketName);
const bucketFile = bucket.file(fullFileName);
const [fileExists] = await bucketFile.exists();
if (fileExists) {
deleteFile = true;
}
const asciiFileName = file.name.normalize('NFKD').replace(/[\u0300-\u036f]/g, '');
const fileAttributes = {
contentType:
typeof config.getContentType === 'function' ? config.getContentType(file) : file.mime,
gzip: typeof config.gzip === 'boolean' ? config.gzip : 'auto',
metadata:
typeof config.metadata === 'function'
? config.metadata(file)
: {
contentDisposition: `inline; filename="${asciiFileName}"`,
cacheControl: `public, max-age=${config.cacheMaxAge || 3600}`,
},
};
if (!config.uniform) {
fileAttributes.public = config.publicFiles;
}

return { fileAttributes, bucketFile, fullFileName, deleteFile };
};

/**
*
* @param providerConfig
Expand Down Expand Up @@ -161,36 +200,17 @@ const init = (providerConfig) => {
return {
async upload(file) {
try {
const fullFileName =
typeof config.generateUploadFileName === 'function'
? await config.generateUploadFileName(file)
: generateUploadFileName(basePath, file);
if (!config.skipCheckBucket) {
await checkBucket(GCS, config.bucketName);
}
const bucket = GCS.bucket(config.bucketName);
const bucketFile = bucket.file(fullFileName);
const [fileExists] = await bucketFile.exists();
if (fileExists) {
const { fileAttributes, bucketFile, fullFileName, deleteFile } = await prepareUploadFile(
file,
config,
basePath,
GCS
);
if (deleteFile) {
console.info('File already exists. Try to remove it.');
await this.delete(file);
}
const asciiFileName = file.name.normalize('NFKD').replace(/[\u0300-\u036f]/g, '');
const fileAttributes = {
contentType:
typeof config.getContentType === 'function' ? config.getContentType(file) : file.mime,
gzip: typeof config.gzip === 'boolean' ? config.gzip : 'auto',
metadata:
typeof config.metadata === 'function'
? config.metadata(file)
: {
contentDisposition: `inline; filename="${asciiFileName}"`,
cacheControl: `public, max-age=${config.cacheMaxAge || 3600}`,
},
};
if (!config.uniform) {
fileAttributes.public = config.publicFiles;
}

await bucketFile.save(file.buffer, fileAttributes);
file.url = `${baseUrl}/${fullFileName}`;
console.debug(`File successfully uploaded to ${file.url}`);
Expand All @@ -203,36 +223,16 @@ const init = (providerConfig) => {
},
async uploadStream(file) {
try {
const fullFileName =
typeof config.generateUploadFileName === 'function'
? await config.generateUploadFileName(file)
: generateUploadFileName(basePath, file);
if (!config.skipCheckBucket) {
await checkBucket(GCS, config.bucketName);
}
const bucket = GCS.bucket(config.bucketName);
const bucketFile = bucket.file(fullFileName);
const [fileExists] = await bucketFile.exists();
if (fileExists) {
const { fileAttributes, bucketFile, fullFileName, deleteFile } = await prepareUploadFile(
file,
config,
basePath,
GCS
);
if (deleteFile) {
console.info('File already exists. Try to remove it.');
await this.delete(file);
}
const fileAttributes = {
contentType:
typeof config.getContentType === 'function' ? config.getContentType(file) : file.mime,
gzip: typeof config.gzip === 'boolean' ? config.gzip : 'auto',
metadata:
typeof config.metadata === 'function'
? config.metadata(file)
: {
contentDisposition: `inline; filename="${file.name}"`,
cacheControl: `public, max-age=${config.cacheMaxAge || 3600}`,
},
};
if (!config.uniform) {
fileAttributes.public = config.publicFiles;
}

await pipeline(file.stream, bucketFile.createWriteStream(fileAttributes));
console.debug(`File successfully uploaded to ${file.url}`);
file.url = `${baseUrl}/${fullFileName}`;
Expand Down Expand Up @@ -284,5 +284,6 @@ module.exports = {
checkBucket,
mergeConfigs,
generateUploadFileName,
prepareUploadFile,
init,
};
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@strapi-community/strapi-provider-upload-google-cloud-storage",
"version": "4.10.1",
"version": "4.10.2",
"description": "(Non-official) Google Cloud Storage Provider for Strapi Upload",
"keywords": [
"strapi",
Expand Down Expand Up @@ -39,20 +39,20 @@
"url": "git+https://github.com/strapi-community/strapi-provider-upload-google-cloud-storage.git"
},
"dependencies": {
"@google-cloud/storage": "^5.20.5",
"slugify": "^1.6.5"
"@google-cloud/storage": "^6.10.0",
"slugify": "^1.6.6"
},
"devDependencies": {
"codecov": "^3.8.3",
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.4",
"eslint": "^8.40.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"mocha": "^10.2.0",
"mock-require": "^3.0.3",
"nyc": "^15.1.0",
"prettier": "^2.5.0"
"prettier": "^2.8.8"
},
"engines": {
"node": "^16 || ^18",
Expand Down
2 changes: 1 addition & 1 deletion test/lib/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ describe('/lib/provider.js', () => {
assert.equal(typeof result.getSignedUrl, 'function');
});

it('must instanciate google cloud storage with right configurations', () => {
it('must instance google cloud storage with right configurations', () => {
let assertionsCount = 0;
mockRequire('@google-cloud/storage', {
Storage: class {
Expand Down

0 comments on commit b5fa17e

Please sign in to comment.