Skip to content

Commit

Permalink
feat(download zip): configuration to download media on a zip or one b…
Browse files Browse the repository at this point in the history
…ye one without zipping
  • Loading branch information
programador51 committed Mar 28, 2024
1 parent 59ae77b commit 790059e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 13 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -89,6 +89,7 @@
"dependencies": {
"bootswatch": "^5.3.3",
"byte-size": "^8.1.1",
"file-saver": "^2.0.5",
"jszip": "^3.10.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -113,6 +114,7 @@
"@semantic-release/github": "^10.0.2",
"@semantic-release/release-notes-generator": "^13.0.0",
"@types/byte-size": "^8.1.2",
"@types/file-saver": "^2.0.7",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@types/webextension-polyfill": "^0.10.7",
Expand Down
4 changes: 1 addition & 3 deletions src/App.tsx
Expand Up @@ -9,8 +9,6 @@ function App() {

const key = useRef(`${window.crypto.randomUUID()}`);



return (
<main className={ui.addOn}>
<Header />
Expand All @@ -35,7 +33,7 @@ function App() {

<button
className="btn btn-primary"
onClick={() => hook.handleDownloadAll()}
onClick={async () => await hook.handleDownloadAll()}
>
Download all
</button>
Expand Down
24 changes: 17 additions & 7 deletions src/customHooks/useAddOn/index.ts
Expand Up @@ -2,17 +2,17 @@ import browser from "webextension-polyfill";
import { CrawledImageDom } from "../../typesContentScript";
import { useContext, useEffect, useState } from "react";
import { MessageBrowserActions } from "../../helpers/content_script/types";
import { downloadBase64 } from "../../helpers/files";
import { downloadBase64, zipGallery } from "../../helpers/files";
import { ContextGlobal } from "../../structure/configuration/Global";
import { saveAs } from "file-saver";

export default function useAddOn() {
const [state, setState] = useState<CrawledImageDom[]>([]);

const global = useContext(ContextGlobal)
const global = useContext(ContextGlobal);

useEffect(() => {

if(global===undefined) return
if (global === undefined) return;

browser.runtime.onMessage.addListener(function (
message: MessageBrowserActions<"crawledContent">
Expand Down Expand Up @@ -44,13 +44,23 @@ export default function useAddOn() {
const crawledContent: CrawledImageDom[] =
typeof dto === "string" ? JSON.parse(dto) : dto;

const parsed = global?.showThumbnails ? crawledContent : crawledContent.filter(item=>item.height > 320 && item.width > 320)
const parsed = global?.showThumbnails
? crawledContent
: crawledContent.filter((item) => item.height > 320 && item.width > 320);

setState(parsed);
}

function handleDownloadAll() {
state.forEach((data) => downloadBase64(data.blob));
async function handleDownloadAll() {
if (global?.zipBulkDownloads) {
const toZip = state.map((item) => item.blob);

const zippedFiles = await zipGallery(toZip);

saveAs(zippedFiles, `${window.crypto.randomUUID()}.zip`);
} else {
state.forEach((data) => downloadBase64(data.blob));
}
}

return {
Expand Down
20 changes: 19 additions & 1 deletion src/helpers/files/index.ts
@@ -1,3 +1,21 @@
import JSZip from "jszip";

export async function zipGallery(gallery: string[]): Promise<Blob> {
const zip = new JSZip();
for (let i = 0; i < gallery.length; i++) {

const name = `${i+1}`.padStart(10,"0");

zip.file(`${name}.jpg`, gallery[i].split(",")[1], {
base64: true,
});
}

const data = await zip.generateAsync({ type: "blob" });

return data;
}

/**
*
* @param base64String - Bas64 file
Expand Down Expand Up @@ -87,5 +105,5 @@ export function downloadBase64(base64: string) {
document.body.removeChild(link);
URL.revokeObjectURL(url);

return blob
return blob;
}
23 changes: 21 additions & 2 deletions src/index.css
Expand Up @@ -2,6 +2,25 @@
--primaryBackground: rgb(247, 247, 247);
}

/* Default font size */
* {
font-size: 16px;
}

/* Media query for smaller screens */
@media screen and (max-width: 768px) {
* {
font-size: 14px;
}
}

/* Media query for even smaller screens */
@media screen and (max-width: 480px) {
* {
font-size: 12px;
}
}

*,
*::before,
*::after {
Expand Down Expand Up @@ -112,7 +131,7 @@ textarea {
background-color: transparent;
}

svg{
svg {
height: 20px;
}

Expand All @@ -124,7 +143,7 @@ svg{
--primaryBackground: rgb(50, 50, 50);
}

*:not(label, input,select,option) {
*:not(label, input, select, option) {
color: white !important;
}
}

0 comments on commit 790059e

Please sign in to comment.