Skip to content

Commit

Permalink
feat: save files in <title>.<extension> format
Browse files Browse the repository at this point in the history
closes: #273

Signed-off-by: Jordan Shatford <jordanshatford@live.com>
  • Loading branch information
jordanshatford committed Jun 25, 2024
1 parent 484d133 commit 529c891
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/cold-sloths-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@yd/extension": minor
"@yd/web": minor
---

feat: save files in <title>.<extension> format
2 changes: 1 addition & 1 deletion apps/extension/src/lib/context-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class ContextService {

const blob = await DownloadsService.getDownloadFile({ downloadId: id });
const download = this.#downloads[id];
const filename = `${download.video.title} - ${download.video.channel.name}.${download.options.format}`;
const filename = `${download.video.title}.${download.options.format}`;
try {
await saveAs(blob, filename);
} catch (e) {
Expand Down
11 changes: 1 addition & 10 deletions apps/extension/src/lib/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function saveAs(blob: Blob, name: string): Promise<number> {
return await browser.downloads.download({
// Blob type and base64 data for download.
url: `data:${blob.type};base64,${base64}`,
filename: cleanFilename(name),
filename: name,
saveAs: false,
conflictAction: 'uniquify'
});
Expand All @@ -33,12 +33,3 @@ async function blobToBase64(blob: Blob): Promise<string> {
reader.readAsDataURL(blob);
});
}

/**
* Clean a string to prevent browser download throwing invalid naming.
* @param name - name to clean
* @returns string - cleaned filename
*/
function cleanFilename(name: string): string {
return name.replace(/[^a-z0-9]/gi, '_');
}
2 changes: 1 addition & 1 deletion apps/web/src/lib/stores/downloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function createDownloadsStore() {
try {
const blob = await DownloadsService.getDownloadFile({ downloadId: id });
const download = get(DOWNLOADS)[id];
const filename = `${download.video.title} - ${download.video.channel.name}.${download.options.format}`;
const filename = `${download.video.title}.${download.options.format}`;
saveAs(blob, filename);
} catch (err) {
handleError(id, 'Failed to get file for download.', err);
Expand Down

0 comments on commit 529c891

Please sign in to comment.