Skip to content

Commit

Permalink
Implement use complete path #298
Browse files Browse the repository at this point in the history
  • Loading branch information
baptistecdr committed Oct 28, 2023
1 parent d648a59 commit 9d58ee8
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 61 deletions.
80 changes: 78 additions & 2 deletions package-lock.json

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

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "aria2-integration",
"private": true,
"type": "module",
"version": "4.5.4-SNAPSHOT.0",
"version": "4.6.0",
"scripts": {
"lint": "eslint 'src/**/*.{js,ts,jsx,tsx}'",
"lint:fix": "eslint 'src/**/*.{js,ts,jsx,tsx}' --fix",
Expand All @@ -14,6 +14,8 @@
"dependencies": {
"@stdlib/regexp-basename-posix": "0.1.1",
"@stdlib/regexp-basename-windows": "0.1.1",
"@stdlib/regexp-dirname-posix": "0.1.1",
"@stdlib/regexp-dirname-windows": "0.1.1",
"@testing-library/jest-dom": "6.1.4",
"@testing-library/react": "14.0.0",
"@testing-library/user-event": "14.5.1",
Expand All @@ -40,6 +42,8 @@
"@types/react": "18.2.33",
"@types/react-dom": "18.2.14",
"@types/uuid": "9.0.6",
"@types/bootstrap": "5.2.8",
"@types/react-bootstrap": "0.32.34",
"@types/webextension-polyfill": "0.10.5",
"@typescript-eslint/eslint-plugin": "6.9.0",
"@typescript-eslint/parser": "6.9.0",
Expand Down
15 changes: 12 additions & 3 deletions public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"extDescription": {
"message": "Download files with Aria2"
},
"optionsNoServers": {
"message": "There are no servers<br>Create a new server using the <b>+</b> button above."
},
"serverOptionsSuccess": {
"message": "The options have been saved."
},
Expand Down Expand Up @@ -83,6 +80,18 @@
"extensionOptionsExcludeFileTypesDescription": {
"message": "rar, zip"
},
"extensionOptionsUseCompleteFilePath": {
"message": "Use complete file path when using \"Save Link As...\""
},
"extensionOptionsUseCompleteFilePathHelpTitle": {
"message": "Help"
},
"extensionOptionsUseCompleteFilePathHelpContent": {
"message": "Aria2 Integration uses the file name when you are using \"Save Link As...\".\nEnabling this option, Aria2 Integration will also use the file path. It only works when Aria2 is running locally."
},
"extensionOptionsUseCompleteFilePathHelpButton": {
"message": "Close"
},
"extensionOptionsExcludeFileTypesInformation": {
"message": "Leave it blank to capture files of any type."
},
Expand Down
15 changes: 12 additions & 3 deletions public/_locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"extDescription": {
"message": "Télécharger des fichiers avec Aria2"
},
"optionsNoServers": {
"message": "Il n'y a aucun serveur.<br>Créez-en un en cliquant sur le bouton <b>+</b> au dessus."
},
"serverOptionsSuccess": {
"message": "Les options ont été sauvegardé."
},
Expand Down Expand Up @@ -86,6 +83,18 @@
"extensionOptionsExcludeFileTypesInformation": {
"message": "rar, zip"
},
"extensionOptionsUseCompleteFilePath": {
"message": "Utiliser le chemin d'accès complet au fichier lors de l'utilisation \"Enregistrer le lien sous...\""
},
"extensionOptionsUseCompleteFilePathHelpTitle": {
"message": "Aide"
},
"extensionOptionsUseCompleteFilePathHelpContent": {
"message": "Aria2 Integration utilise le nom du fichier lorsque vous utilisez \"Enregistrer le lien sous...\".\nEn activant cette option, Aria2 Integration utilisera également le chemin d'accès au fichier. Cela ne fonctionne que lorsque Aria2 est exécuté localement."
},
"extensionOptionsUseCompleteFilePathHelpButton": {
"message": "Fermer"
},
"extensionOptionsTheme": {
"message": "Thème"
},
Expand Down
15 changes: 12 additions & 3 deletions public/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"extDescription": {
"message": "用 Aria2 下载文件"
},
"optionsNoServers": {
"message": "无服务器<br>使用上面的 <b>+</b> 按钮创建新的服务器."
},
"serverOptionsSuccess": {
"message": "选项已保存."
},
Expand Down Expand Up @@ -83,6 +80,18 @@
"extensionOptionsExcludeFileTypesDescription": {
"message": "rar, zip"
},
"extensionOptionsUseCompleteFilePath": {
"message": "Use complete file path when using \"Save Link As...\""
},
"extensionOptionsUseCompleteFilePathHelpTitle": {
"message": "Help"
},
"extensionOptionsUseCompleteFilePathHelpContent": {
"message": "Aria2 Integration uses the file name when you are using \"Save Link As...\".\nEnabling this option, Aria2 Integration will also use the file path. It only works when Aria2 is running locally."
},
"extensionOptionsUseCompleteFilePathHelpButton": {
"message": "Close"
},
"extensionOptionsExcludeFileTypesInformation": {
"message": "留空则拦截任何类型的文件下载."
},
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "baptistecdr",
"homepage_url": "https://github.com/baptistecdr/aria2-extensions",
"description": "__MSG_extDescription__",
"version": "4.5.4-SNAPSHOT.0",
"version": "4.6.0",
"default_locale": "en",
"permissions": [
"contextMenus",
Expand Down
11 changes: 6 additions & 5 deletions src/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Cookies, Downloads, Menus, Tabs } from "webextension-polyfill";
import browser from "webextension-polyfill";
import { captureTorrentFromURL, captureURL, showNotification } from "../models/aria2-extension";
import ExtensionOptions from "../models/extension-options";
import basename from "../models/basename";
import { basename, dirname } from "../stdlib";
import Server from "../models/server";

const CONTEXT_MENUS_PARENT_ID = "aria2-integration";
Expand Down Expand Up @@ -144,15 +144,16 @@ function downloadItemMustBeCaptured(extensionOptions: ExtensionOptions, item: Do
return false;
}

async function captureDownloadItem(aria2: any, server: Server, item: Downloads.DownloadItem, referer: string, cookies: string) {
async function captureDownloadItem(aria2: any, server: Server, item: Downloads.DownloadItem, referer: string, cookies: string, useCompleteFilePath: boolean) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const url = item.finalUrl ?? item.url; // finalUrl (Chrome), url (Firefox)
const directory = useCompleteFilePath ? await dirname(item.filename) : undefined;
const filename = await basename(item.filename);
if (url.match(/\.torrent$|\.meta4$|\.metalink$/) || filename.match(/\.torrent$|\.meta4$|\.metalink$/)) {
return captureTorrentFromURL(aria2, server, url, filename);
return captureTorrentFromURL(aria2, server, url, directory, filename);
}
return captureURL(aria2, server, url, referer, cookies, filename);
return captureURL(aria2, server, url, referer, cookies, directory, filename);
}

browser.downloads.onCreated.addListener(async (downloadItem) => {
Expand All @@ -175,7 +176,7 @@ browser.downloads.onCreated.addListener(async (downloadItem) => {
await browser.downloads.erase({ id: downloadItem.id });
}
try {
await captureDownloadItem(connection, server, downloadItem, referrer, cookies);
await captureDownloadItem(connection, server, downloadItem, referrer, cookies, extensionOptions.useCompleteFilePath);
await showNotification(browser.i18n.getMessage("addFileSuccess", server.name));
} catch {
await showNotification(browser.i18n.getMessage("addFileError", server.name));
Expand Down
19 changes: 14 additions & 5 deletions src/models/aria2-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,32 @@ export async function captureTorrentFromFile(aria2: any, server: Server, file: F
return aria2.call("aria2.addMetalink", blobAsBase64, [], server.rpcParameters);
}

export async function captureTorrentFromURL(aria2: any, server: Server, url: string, filename?: string) {
export async function captureTorrentFromURL(aria2: any, server: Server, url: string, directory?: string, filename?: string) {
const blob = await download(url);
const blobAsBase64 = await encodeFileToBase64(blob);
const aria2Parameters: any = {
...server.rpcParameters,
};
if (directory) {
aria2Parameters.dir = directory;
}
if (url.endsWith(".torrent") || filename?.endsWith(".torrent")) {
return aria2.call("aria2.addTorrent", blobAsBase64, [], server.rpcParameters);
return aria2.call("aria2.addTorrent", blobAsBase64, [], aria2Parameters);
}
return aria2.call("aria2.addMetalink", blobAsBase64, [], server.rpcParameters);
return aria2.call("aria2.addMetalink", blobAsBase64, [], aria2Parameters);
}

export async function captureURL(aria2: any, server: Server, url: string, referer: string, cookies: string, filename?: string) {
export async function captureURL(aria2: any, server: Server, url: string, referer: string, cookies: string, directory?: string, filename?: string) {
if (url.match(/\.torrent$|\.meta4$|\.metalink$/)) {
return captureTorrentFromURL(aria2, server, url, filename);
return captureTorrentFromURL(aria2, server, url, directory, filename);
}
const aria2Parameters: any = {
header: [`Referer: ${referer}`, `Cookie: ${cookies}`],
...server.rpcParameters,
};
if (directory) {
aria2Parameters.dir = directory;
}
if (filename) {
aria2Parameters.out = filename;
}
Expand Down
12 changes: 0 additions & 12 deletions src/models/basename.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/models/extension-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ export default class ExtensionOptions {
public readonly excludedProtocols: string[] = [],
public readonly excludedSites: string[] = [],
public readonly excludedFileTypes: string[] = [],
public readonly useCompleteFilePath: boolean = false,
public readonly theme: Theme = Theme.Auto,
) {}

private serialize(): string {
public serialize(): string {
return JSON.stringify(this);
}

Expand Down
Loading

0 comments on commit 9d58ee8

Please sign in to comment.