strapi-community / strapi-provider-upload-google-cloud-storage

Google Cloud Storage Upload Provider for Strapi
MIT License
211 stars 94 forks source link

Support async generateUploadFileName method #164

Closed Frank3K closed 1 year ago

Frank3K commented 1 year ago

Which is required such that the getStream method on uploaded files (in Strapi 4) can be used.

Strapi 3

In Strapi 3 we could use the exposed file buffer to compute a (for example) md5 hash of the uploaded file:

      generateUploadFileName: (file) => {
        const extension = file.ext.toLowerCase();
        const uploadPath = ...;
        const hash = md5(file.buffer);
        const fileNameWithoutExtension = slugify(path.parse(file.name).name);
        return `${uploadPath}/${fileNameWithoutExtension}-${hash}${extension}`;
      },

Strapi 4

In Strapi 4 the buffer does not seem to be exposed anymore. Instead we find a getStream() method which can be used to get a ReadStream of the file (source). This can be used to computed a md5 hash, but this requires generateFileName to be async-compatible.

        generateUploadFileName: async (file: File) => {
          const extension = file.ext.toLowerCase();
          const uploadPath = ...;
          const hash = await md5(file.getStream());
          const fileNameWithoutExtension = slugify(path.parse(file.name).name);
          return `${uploadPath}/${fileNameWithoutExtension}-${hash}${extension}`;
        },

Sample md5 method for ReadStream:

function md5(rs: ReadStream): Promise<string> {
  return new Promise((resolve, reject) => {
    const hash = crypto.createHash('md5');
    rs.on('error', reject);
    rs.on('data', (chunk: string | Buffer) => hash.update(chunk));
    rs.on('end', () => resolve(hash.digest('hex')));
  });
}
codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 16.66% and project coverage change: -3.49 :warning:

Comparison is base (0d7e438) 78.70% compared to head (3ddb3cd) 75.22%.

:exclamation: Current head 3ddb3cd differs from pull request most recent head 8ede666. Consider uploading reports for the commit 8ede666 to get more accurate results

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #164 +/- ## ========================================== - Coverage 78.70% 75.22% -3.49% ========================================== Files 2 2 Lines 108 113 +5 ========================================== Hits 85 85 - Misses 23 28 +5 ``` | [Impacted Files](https://codecov.io/gh/strapi-community/strapi-provider-upload-google-cloud-storage/pull/164?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | | |---|---|---| | [lib/provider.js](https://codecov.io/gh/strapi-community/strapi-provider-upload-google-cloud-storage/pull/164?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bGliL3Byb3ZpZGVyLmpz) | `74.77% <16.66%> (-3.53%)` | :arrow_down: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

Lith commented 1 year ago

Hi @Frank3K

Could you please sign your commits ? This is blocking PR for me. It's required by Strapi Community.

Thanks !

Frank3K commented 1 year ago

@Lith Thanks for reviewing. I've just signed the commit and force-pushed it.

Lith commented 1 year ago

Hi @Frank3K ! Your PR has been release in https://github.com/strapi-community/strapi-provider-upload-google-cloud-storage/releases/tag/4.10.2 ! Thx

Frank3K commented 1 year ago

Thank you 🎉