Skip to content

Commit

Permalink
fix filename and directory
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslawPokropinski committed Sep 30, 2021
1 parent 8ebeac3 commit c3d61f1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2018
"ecmaVersion": 2020
},
"rules": {
"linebreak-style": ["error", "unix"]
}
}
}
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"semi": true
}
37 changes: 29 additions & 8 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Aria2 from 'aria2';
import { filter, Observable, take } from 'rxjs';

function validateUrl(value) {
return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(
Expand All @@ -7,7 +8,14 @@ function validateUrl(value) {
}

function downloadAgent() {
browser.downloads.onCreated.addListener(function (downloadItem) {
const subscribers = [];
const observable = new Observable((s) => subscribers.push(s));

chrome.downloads.onChanged.addListener((delta) => {
subscribers.forEach((s) => s.next(delta));
});

chrome.downloads.onCreated.addListener(function (downloadItem) {
if (downloadItem.state !== 'in_progress') {
return;
}
Expand Down Expand Up @@ -49,7 +57,7 @@ function downloadAgent() {
if (downloadItem.filename) {
let directory, filename;
filename = downloadItem.filename.replace(/^.*[\\/]/, '');
directory = downloadItem.filename.match(/(.*)[/\\]/)[1] || '';
directory = downloadItem.filename.match(/(.*)[/\\]/)?.[1] ?? '';
// Appends path to the options
params = {
dir: directory,
Expand All @@ -66,18 +74,18 @@ function downloadAgent() {
.call('addUri', [downloadUrl], params)
.then(async () => {
// Added successfully: Cancels and removes the download from browser download manager
function onFinished() {}
function pass() {}

function onError(error) {
console.error(`Error: ${error}`);
}

const removing = browser.downloads.removeFile(downloadItem.id);
removing.then(onFinished).catch(onError);
removing.then(pass).catch(pass);
const canceling = browser.downloads.cancel(downloadItem.id);
canceling.then(onFinished).catch(onError);
canceling.then(pass).catch(onError);
const erasing = browser.downloads.erase({ id: downloadItem.id });
erasing.then(onFinished).catch(onError);
erasing.then(pass).catch(onError);

// Shows notification
if (result.enablenotifications) {
Expand All @@ -97,7 +105,6 @@ function downloadAgent() {
}
});
}
setTimeout(() => browser.runtime.reload(), 1000);
})
.catch((err) => {
console.error(err);
Expand All @@ -120,7 +127,21 @@ function downloadAgent() {
'extensionstatus',
'enablenotifications',
]);
getResult.then(onGot, onError);

getResult.then((result) => {
// wait for filename to be set
if (downloadItem.filename == null || downloadItem.filename === '') {
observable
.pipe(
filter((delta) => delta.id === downloadItem.id && delta.filename),
take(1)
)
.subscribe((delta) => {
downloadItem.filename = delta.filename.current;
onGot(result);
});
}
}, onError);
});
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"webextension-toolbox": "4.0.3"
},
"dependencies": {
"aria2": "^4.1.2"
"aria2": "^4.1.2",
"rxjs": "^7.3.0"
}
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,13 @@ run-parallel@^1.1.9:
dependencies:
queue-microtask "^1.2.2"

rxjs@^7.3.0:
version "7.3.0"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.3.0.tgz#39fe4f3461dc1e50be1475b2b85a0a88c1e938c6"
integrity sha512-p2yuGIg9S1epc3vrjKf6iVb3RCaAYjYskkO+jHIaV0IjOPlJop4UnodOoFb2xeNwlguqLYvGw1b1McillYb5Gw==
dependencies:
tslib "~2.1.0"

safe-buffer@^5.1.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
Expand Down Expand Up @@ -2919,6 +2926,11 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"

tslib@~2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a"
integrity sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A==

type-check@^0.4.0, type-check@~0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"
Expand Down

0 comments on commit c3d61f1

Please sign in to comment.