Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(s3): send magicbytes to start request #428

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added manual_tests/test_image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion manual_tests/upload_new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const createFile = (size = 10 * 1024 * 1024) => Buffer.alloc(size).fill('a');
// }

(async () => {
const file = await getFile(createFile());
const file = await getFile('./test_image.jpeg');
// const file = await getFile(createFile());
// const file1 = await getFile(createFile());
file.name = 'test.txt';
console.log('Uploadinf file', file.size);
Expand Down
89 changes: 4 additions & 85 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"dependencies": {
"@babel/runtime": "^7.8.4",
"@filestack/loader": "^1.0.4",
"@sentry/minimal": "^6.2.1",
"abab": "^2.0.3",
"debug": "^4.1.1",
"eventemitter3": "^4.0.0",
Expand All @@ -52,9 +53,7 @@
"jsonschema": "^1.2.5",
"lodash.clonedeep": "^4.5.0",
"p-queue": "^4.0.0",
"spark-md5": "^3.0.0",
"ts-node": "^8.10.2",
"@sentry/minimal": "^6.2.1"
"spark-md5": "^3.0.0"
},
"devDependencies": {
"@babel/core": "^7.8.4",
Expand Down
9 changes: 9 additions & 0 deletions src/lib/api/upload/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
import { md5, sanitizeName, SanitizeOptions } from './../../utils';

const MAGIC_BYTES_LEN = 300;

export interface UploadTags {
[key: string]: string;
}
Expand Down Expand Up @@ -216,6 +218,13 @@ export class File {
});
}

/**
* Returns magic bytes for file
*/
public async getMagicBytes() {
return String.fromCharCode.apply(null, new Uint16Array(await this._file.slice(0, MAGIC_BYTES_LEN)));
}

/**
* Returns part chunk
*
Expand Down
12 changes: 10 additions & 2 deletions src/lib/api/upload/uploaders/s3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,13 @@ describe('Api/Upload/Uploaders/S3', () => {
expect(res[0].handle).toEqual('test_handle');

const testFile = getSmallTestFile();
const bytes = await testFile.getMagicBytes();

expect(mockStart).toHaveBeenCalledWith({
filename: testFile.name,
mimetype: testFile.mimetype,
size: testFile.size,
bytes,
store: {
location: DEFAULT_STORE_LOCATION,
},
Expand Down Expand Up @@ -857,20 +860,23 @@ describe('Api/Upload/Uploaders/S3', () => {
it('should upload file', async () => {
const partSize = 1024 * 1024 * 7;

const testFile = getTestFile();

const u = new S3Uploader({});
u.setUrl(testHost);
u.setApikey(testApikey);
u.setPartSize(partSize);
u.addFile(getTestFile());
u.addFile(testFile);

const res = await u.execute();

const testFile = getTestFile();
const bytes = await testFile.getMagicBytes();

expect(mockStart).toHaveBeenCalledWith({
filename: testFile.name,
mimetype: testFile.mimetype,
size: testFile.size,
bytes,
store: {
location: DEFAULT_STORE_LOCATION,
},
Expand Down Expand Up @@ -941,11 +947,13 @@ describe('Api/Upload/Uploaders/S3', () => {
const res = await u.execute();

const testFile = getTestFile();
const bytes = await testFile.getMagicBytes();

expect(mockStart).toHaveBeenCalledWith({
filename: testFile.name,
mimetype: testFile.mimetype,
size: testFile.size,
bytes,
store: {
location: DEFAULT_STORE_LOCATION,
},
Expand Down
5 changes: 4 additions & 1 deletion src/lib/api/upload/uploaders/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export class S3Uploader extends UploaderAbstract {
* @returns {Promise<any>}
* @memberof S3Uploader
*/
private startRequest(id: string): Promise<any> {
private async startRequest(id: string): Promise<any> {
const payload = this.getPayloadById(id);

if (payload.file.size === 0) {
Expand All @@ -294,12 +294,15 @@ export class S3Uploader extends UploaderAbstract {
}

debug(`[${id}] Make start request`);
const bytes = await payload.file.getMagicBytes();

return FsRequest.post(
`${this.getUrl()}/multipart/start`,
{
filename: payload.file.name,
mimetype: payload.file.type,
size: payload.file.size,
bytes: bytes.toString(),
...this.getDefaultFields(id, ['apikey', 'policy', 'signature', 'fii'], true),
},
{
Expand Down