Skip to content

Commit

Permalink
feat(new tool) multi-link-downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmotion committed Oct 24, 2024
1 parent f836666 commit 3406743
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 1 deletion.
1 change: 1 addition & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ declare module '@vue/runtime-core' {
MenuLayout: typeof import('./src/components/MenuLayout.vue')['default']
MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default']
MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default']
MultiLinkDownloader: typeof import('./src/tools/multi-link-downloader/multi-link-downloader.vue')['default']
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
NCheckbox: typeof import('naive-ui')['NCheckbox']
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
Expand Down
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,7 @@ tools:
text-to-binary:
title: Text to ASCII binary
description: Convert text to its ASCII binary representation and vice-versa.

multi-link-downloader:
title: Multi link downloader
description: Asynchronously downloads from multiple links into a zip file while a single link downloads directly.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"ibantools": "^4.3.3",
"js-base64": "^3.7.6",
"json5": "^2.2.3",
"jszip": "^3.10.1",
"jwt-decode": "^3.1.2",
"libphonenumber-js": "^1.10.28",
"lodash": "^4.17.21",
Expand Down
54 changes: 54 additions & 0 deletions pnpm-lock.yaml

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

7 changes: 6 additions & 1 deletion src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as multiLinkDownloader } from './multi-link-downloader';
import { tool as emailNormalizer } from './email-normalizer';

import { tool as asciiTextDrawer } from './ascii-text-drawer';
Expand Down Expand Up @@ -188,7 +189,11 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Data',
components: [phoneParserAndFormatter, ibanValidatorAndParser],
components: [
phoneParserAndFormatter,
ibanValidatorAndParser,
multiLinkDownloader,
],
},
];

Expand Down
12 changes: 12 additions & 0 deletions src/tools/multi-link-downloader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FileDownload } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'Multi link downloader',
path: '/multi-link-downloader',
description: '',
keywords: ['multi', 'link', 'downloader'],
component: () => import('./multi-link-downloader.vue'),
icon: FileDownload,
createdAt: new Date('2024-10-18'),
});
108 changes: 108 additions & 0 deletions src/tools/multi-link-downloader/multi-link-downloader.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import JSZip from 'jszip';

export async function downloadLinks(links: string): Promise<void> {
// Split links by newline and filter out empty ones
const linksArray: string[] = links.split('\n').filter(link => link.trim() !== '');

// Helper function to handle duplicate filenames
function getUniqueFileName(existingNames: Set<string>, originalName: string): string {
let fileName = originalName;
let fileExtension = '';

// Split filename and extension (if any)
const lastDotIndex = originalName.lastIndexOf('.');
if (lastDotIndex !== -1) {
fileName = originalName.substring(0, lastDotIndex);
fileExtension = originalName.substring(lastDotIndex);
}

let counter = 1;
let uniqueName = originalName;

// Append a counter to the filename if it already exists in the map
while (existingNames.has(uniqueName)) {
uniqueName = `${fileName} (${counter})${fileExtension}`;
counter++;
}

existingNames.add(uniqueName);
return uniqueName;
}

if (linksArray.length === 1) {
// Single link: download directly
const linkUrl: string = linksArray[0];
try {
const response: Response = await fetch(linkUrl);
if (!response.ok) {
throw new Error(`Failed to fetch ${linkUrl}`);
}

// Get file as blob
const blob: Blob = await response.blob();

// Extract filename from URL
const fileName: string = linkUrl.split('/').pop() || 'downloaded_file';

// Trigger download
const a: HTMLAnchorElement = document.createElement('a');
const downloadUrl: string = window.URL.createObjectURL(blob);
a.href = downloadUrl;
a.download = fileName;
document.body.appendChild(a);
a.click();

// Clean up
document.body.removeChild(a);
window.URL.revokeObjectURL(downloadUrl);
}
catch (error) {
console.error('Error downloading the file:', error);
}
}
else if (linksArray.length > 1) {
// Multiple links: create a zip file
const zip = new JSZip();
const fileNamesSet = new Set<string>(); // To track file names for duplicates

await Promise.all(
linksArray.map(async (linkUrl: string) => {
try {
const response: Response = await fetch(linkUrl);
if (!response.ok) {
throw new Error(`Failed to fetch ${linkUrl}`);
}
const blob: Blob = await response.blob();

// Extract filename from URL
let fileName: string = linkUrl.split('/').pop() || 'file';

// Get unique filename if duplicate exists
fileName = getUniqueFileName(fileNamesSet, fileName);

// Add file to the zip
zip.file(fileName, blob);
}
catch (error) {
console.error(`Error downloading file from ${linkUrl}:`, error);
}
}),
);

// Generate the zip file and trigger download
zip.generateAsync({ type: 'blob' }).then((zipBlob: Blob) => {
const downloadUrl: string = window.URL.createObjectURL(zipBlob);

// Trigger download of the zip file
const a: HTMLAnchorElement = document.createElement('a');
a.href = downloadUrl;
a.download = 'downloaded_files.zip';
document.body.appendChild(a);
a.click();

// Clean up
document.body.removeChild(a);
window.URL.revokeObjectURL(downloadUrl);
});
}
}
52 changes: 52 additions & 0 deletions src/tools/multi-link-downloader/multi-link-downloader.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<script lang="ts">
import { defineComponent, ref } from 'vue';
import { downloadLinks } from './multi-link-downloader.service';
export default defineComponent({
setup() {
const links = ref<string>('');
const downloadMultiLinks = () => {
if (links.value) {
downloadLinks(links.value);
}
};
const clearInput = () => {
links.value = '';
};
return {
links,
downloadMultiLinks,
clearInput,
};
},
});
</script>

<template>
<c-card>
<div class="mb-4 flex justify-between">
<c-button
class="mr-2"
:disabled="!links"
@click="downloadMultiLinks"
>
Start Download
</c-button>
<c-button
class="ml-2"
@click="clearInput"
>
Clear
</c-button>
</div>

<c-input-text
v-model:value="links"
placeholder="Add links separated by new lines..."
multiline
:rows="20"
/>
</c-card>
</template>

0 comments on commit 3406743

Please sign in to comment.