-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(new tool) multi-link-downloader
- Loading branch information
Showing
8 changed files
with
238 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IconFileDownload } from '@tabler/icons-vue'; | ||
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: IconFileDownload, | ||
createdAt: new Date('2024-10-18'), | ||
}); |
108 changes: 108 additions & 0 deletions
108
src/tools/multi-link-downloader/multi-link-downloader.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |