Skip to content

Commit

Permalink
Add beginMultipartUpload/commitMultipartUpload to WebResourceHandler
Browse files Browse the repository at this point in the history
Change-type: major
  • Loading branch information
otaviojacobi committed Apr 19, 2024
1 parent 4b8f0fa commit a23172f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"devDependencies": {
"@aws-sdk/client-s3": "^3.556.0",
"@balena/lint": "^8.0.0",
"@balena/pinejs-webresource-s3": "^0.1.0",
"@balena/pinejs-webresource-s3": "^0.2.0",
"@faker-js/faker": "^8.3.1",
"@types/busboy": "^1.5.3",
"@types/chai": "^4.3.11",
Expand Down
15 changes: 14 additions & 1 deletion src/webresource-handler/handlers/NoopHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
import type { IncomingFile, UploadResponse, WebResourceHandler } from '..';
import type {
BeginMultipartUploadHandlerResponse,
IncomingFile,
UploadResponse,
WebResourceHandler,
} from '..';

export class NoopHandler implements WebResourceHandler {
public async handleFile(resource: IncomingFile): Promise<UploadResponse> {
Expand All @@ -18,4 +23,12 @@ export class NoopHandler implements WebResourceHandler {
public async onPreRespond(webResource: WebResource): Promise<WebResource> {
return webResource;
}

public async beginMultipartUpload(): Promise<BeginMultipartUploadHandlerResponse> {
return { fileKey: 'noop', uploadId: 'noop', uploadParts: [] };
}

public async commitMultipartUpload(): Promise<WebResource> {
return { filename: 'noop', href: 'noop' };
}
}
35 changes: 35 additions & 0 deletions src/webresource-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@balena/odata-to-abstract-sql';
import { errors, permissions } from '../server-glue/module';
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
import type { AnyObject } from 'pinejs-client-core';

export * from './handlers';

Expand All @@ -29,10 +30,44 @@ export interface UploadResponse {
filename: string;
}

export interface BeginMultipartUploadPayload {
filename: string;
content_type: string;
size: number;
chunk_size: number;
}

export interface UploadPart {
url: string;
chunkSize: number;
partNumber: number;
}

export interface BeginMultipartUploadHandlerResponse {
uploadParts: UploadPart[];
fileKey: string;
uploadId: string;
}

export interface CommitMultipartUploadPayload {
fileKey: string;
uploadId: string;
filename: string;
providerCommitData?: AnyObject;
}

export interface WebResourceHandler {
handleFile: (resource: IncomingFile) => Promise<UploadResponse>;
removeFile: (fileReference: string) => Promise<void>;
onPreRespond: (webResource: WebResource) => Promise<WebResource>;

beginMultipartUpload: (
fieldName: string,
payload: BeginMultipartUploadPayload,
) => Promise<BeginMultipartUploadHandlerResponse>;
commitMultipartUpload: (
commitInfo: CommitMultipartUploadPayload,
) => Promise<WebResource>;
}

type WebResourcesDbResponse = {
Expand Down

0 comments on commit a23172f

Please sign in to comment.