Skip to content

Commit

Permalink
chore: add multipartid event
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jun 24, 2024
1 parent 59ad0cf commit 21a43e4
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,9 @@ upload events:
* `start`: file upload started. Event has `detail` property with object that
contains uploaded file as `file`.
* `multipartid`: multipart upload initialized. Event has `detail` property with
object that contains uploaded file as `file` and ID of multipart upload as
`id`.
* `progress`: another chunk of file was transferred to server. Event has
`detail` property with object that contains uploaded file as `file`, number
of loaded bytes as `loaded` and total number of bytes that must be
Expand Down
9 changes: 7 additions & 2 deletions ckanext/files/assets/scripts/files--shared.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion ckanext/files/assets/ts/files--shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ namespace ckan {
new CustomEvent("start", { detail: { file } }),
);
}
dispatchMultipartId(file: File, id: string) {
this.dispatchEvent(
new CustomEvent("multipartid", { detail: { file, id } }),
);
}
dispatchProgress(file: File, loaded: number, total: number) {
this.dispatchEvent(
new CustomEvent("progress", {
Expand Down Expand Up @@ -229,9 +234,11 @@ namespace ckan {
return;
}

this.dispatchMultipartId(file, info.id);

this.dispatchStart(file);

this._doUpload(file, info);
return this._doUpload(file, info);
}

async resume(file: File, id: string) {
Expand Down Expand Up @@ -288,6 +295,7 @@ namespace ckan {
return;
}
this.dispatchFinish(file, info);
return info
}

_initializeUpload(file: File, params: {[key: string]: any}): Promise<UploadInfo> {
Expand Down
96 changes: 92 additions & 4 deletions cypress/e2e/sandbox.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const sandbox = () => ckan().invoke({ log: false }, "sandbox");

const intercept = (
action: string,
success: boolean = true,
result: any = {},
alias: string = "request",
result: any = {},
success: boolean = true,
) =>
cy
.intercept("/api/action/" + action, (req) =>
Expand Down Expand Up @@ -173,7 +173,7 @@ describe("Standard uploader", () => {
.as("adapter"),
);

it("uploads files", () => {
it("sends expected data to server", () => {
intercept("files_file_create");
cy.get("@adapter").then((adapter: any) =>
new adapter().upload(new File(["test"], "test.txt"), {}),
Expand All @@ -187,7 +187,7 @@ describe("Standard uploader", () => {
});
});

it.only("accepts params and even can override storage", () => {
it("accepts params and even can override storage", () => {
intercept("files_file_create");
cy.get("@adapter").then((adapter: any) =>
new adapter().upload(new File(["test"], "test.txt"), {
Expand All @@ -205,3 +205,91 @@ describe("Standard uploader", () => {
});
});
});

describe("Multipart uploader", () => {
beforeEach(() =>
ckan()
.then(
({
CKANEXT_FILES: {
adapters: { Multipart },
},
}) => Multipart,
)
.as("adapter"),
);

it("sends expected data to server", () => {
const content = "hello,world";
const chunkSize = 6;
let sizes = [content.length, chunkSize];

intercept("files_multipart_start", "start", {
id: "1",
storage_data: { uploaded: 0 },
});

cy.intercept("/api/action/files_multipart_update", (req) => {
return req.reply({
success: true,
result: {
id: "1",
storage_data: {
uploaded: sizes.pop(),
},
},
});
}).as("update");

intercept("files_multipart_complete", "complete");

cy.get("@adapter").then((adapter: any) =>
new adapter({ chunkSize: chunkSize }).upload(
new File([content], "test.txt", { type: "text/plain" }),
{},
),
);

cy.wait("@start").then(({ request: { body } }) => {
expect(body).deep.equal({
size: content.length,
content_type: "text/plain",
storage: "default",
name: "test.txt",
});
});

cy.wait("@update").interceptFormData(
(data) => {
expect(data).includes({
id: "1",
position: "0",
});

cy.wrap(data.upload.slice().text()).should(
"be.equal",
content.slice(0, chunkSize),
);
},
{ loadFileContent: true },
);

cy.wait("@update").interceptFormData(
(data) => {
expect(data).includes({
id: "1",
position: String(chunkSize),
});

cy.wrap(data.upload.slice().text()).should(
"be.equal",
content.slice(chunkSize, content.length),
);
},
{ loadFileContent: true },
);
cy.wait("@complete").then(({ request: { body } }) => {
expect(body).deep.equal({ id: "1" });
});
});
});

0 comments on commit 21a43e4

Please sign in to comment.