Skip to content

Commit 94a6014

Browse files
feat(image-split): implement zip download and set filename
Adds a task to trigger the browser download of the generated ZIP file and replaces the placeholder filename with the original image's name.
1 parent 7f76afa commit 94a6014

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

src/routes/tools/image-split/index.lazy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function ImageSplitRoute(): React.ReactNode {
9191
DownloadSplitCanvasTasks(
9292
{
9393
...data,
94-
filename: "tralala",
94+
filename: imageFile.file.name,
9595
},
9696
imageFile
9797
)

src/services/image-split/tasks/download-split-canvas/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ export default function DownloadSplitCanvasTasks(config: DownloadSplitCanvasConf
1818
Tasks.CreateCanvas(),
1919
Tasks.CreateAndZip({ config, imageFile }),
2020
Tasks.PackageZip(),
21+
Tasks.DownloadZip({ config }),
2122
];
2223
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { createTask } from "@lilbunnyrabbit/task-manager";
2+
import { DownloadSplitCanvasConfig, DownloadSplitCanvasTask } from "..";
3+
4+
export default createTask<{ config: DownloadSplitCanvasConfig }, string>({
5+
name: "Download ZIP",
6+
7+
parse() {
8+
switch (this.status) {
9+
default:
10+
case "idle": {
11+
return {
12+
status: "Download ZIP",
13+
};
14+
}
15+
case "in-progress": {
16+
return {
17+
status: "Downloading...",
18+
};
19+
}
20+
case "error": {
21+
return {
22+
status: "Failed to download...",
23+
};
24+
}
25+
case "success": {
26+
if (this.result.isPresent()) {
27+
return {
28+
status: "Downloaded",
29+
result: this.result.get(),
30+
};
31+
}
32+
33+
return {
34+
status: "No result.",
35+
};
36+
}
37+
}
38+
},
39+
40+
async execute({ config }) {
41+
const blob = this.manager.getTaskResult(DownloadSplitCanvasTask.PackageZip);
42+
43+
const url = URL.createObjectURL(blob);
44+
const a = document.createElement("a");
45+
a.href = url;
46+
a.download = `${config.filename}.zip`;
47+
document.body.appendChild(a);
48+
a.click();
49+
document.body.removeChild(a);
50+
51+
return url;
52+
},
53+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default as CreateAndZip } from "./createAndZip.task";
22
export { default as CreateCanvas } from "./createCanvas.task";
33
export { default as CreateZip } from "./createZip.task";
4+
export { default as DownloadZip } from "./downloadZip.task";
45
export { default as GenerateInfo } from "./generateInfo.task";
56
export { default as PackageZip } from "./packageZip.task";

0 commit comments

Comments
 (0)