Skip to content

Commit

Permalink
[Reputation Oracle] Separate streams for hashing and uploading a file (
Browse files Browse the repository at this point in the history
…#1388)

* Using two separate streams for calculating file hash and uploading to s3 bucket

* `copyFileFromURLToBucket` testcase updated to match `copyFileFromURLToBucket` method
  • Loading branch information
Dzeranov authored Dec 22, 2023
1 parent 2b9372e commit 269fd31
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,15 @@ describe('StorageService', () => {

describe('copyFileFromURLToBucket', () => {
it('should copy a file from a valid URL to a bucket', async () => {
const streamResponseData = new stream.Readable();
streamResponseData.push(JSON.stringify(MOCK_MANIFEST));
streamResponseData.push(null);
(axios.get as any).mockResolvedValueOnce({ data: streamResponseData });
const hashStreamData = new stream.Readable();
hashStreamData.push(JSON.stringify(MOCK_MANIFEST));
hashStreamData.push(null);
const uploadStreamData = new stream.Readable();
uploadStreamData.push(JSON.stringify(MOCK_MANIFEST));
uploadStreamData.push(null);
(axios.get as any)
.mockResolvedValueOnce({ data: hashStreamData })
.mockResolvedValueOnce({ data: uploadStreamData });

const uploadedFile =
await storageService.copyFileFromURLToBucket(MOCK_FILE_URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,18 @@ export class StorageService {
*/
public async copyFileFromURLToBucket(url: string): Promise<UploadedFile> {
try {
const { data } = await axios.get(url, { responseType: 'stream' });
const stream = new PassThrough();
data.pipe(stream);
const { data: hStream } = await axios.get(url, {
responseType: 'stream',
});
const hash = await hashStream(hStream);

const hash = await hashStream(stream);
const key = `s3${hash}.zip`;

await this.minioClient.putObject(this.s3Config.bucket, key, stream, {
// Creating a second readable stream for uploading a file to the bucket
const { data: uStream } = await axios.get(url, {
responseType: 'stream',
});
await this.minioClient.putObject(this.s3Config.bucket, key, uStream, {
'Cache-Control': 'no-store',
});

Expand Down

1 comment on commit 269fd31

@vercel
Copy link

@vercel vercel bot commented on 269fd31 Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.