Skip to content

Commit

Permalink
It should generate an eTag which is just the MD5 of the file for a no…
Browse files Browse the repository at this point in the history
…n-multipart upload file when file size is smaller than the part size

Fixes #215
  • Loading branch information
gabe-af committed May 22, 2024
1 parent ae5f7e0 commit 7be954c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/s3Etag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ function md5(contents: string | BinaryLike): string {
}

export function generateETag(filePath: string, partSizeInBytes = 0): string {
if (partSizeInBytes === 0) {
const { size: fileSizeInBytes } = fs.statSync(filePath);

if (partSizeInBytes === 0 || fileSizeInBytes <= partSizeInBytes) {
return md5(fs.readFileSync(filePath));
}
const { size: fileSizeInBytes } = fs.statSync(filePath);
let parts = Math.floor(fileSizeInBytes / partSizeInBytes);
if (fileSizeInBytes % partSizeInBytes > 0) {
parts += 1;
Expand Down
5 changes: 5 additions & 0 deletions src/tests/s3ETag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ describe('s3ETag', () => {
const etag = generateETag(stylesFilePath, 10);
expect(etag).toBe('b8ffa274b982363604db211ee8ffb991-3');
});

it('should generate an eTag which is just the MD5 of the file for a non-multipart upload file when file size is smaller than the part size ', () => {
const etag = generateETag(stylesFilePath, 2048);
expect(etag).not.toContain('-');
});
});

0 comments on commit 7be954c

Please sign in to comment.