Skip to content

Commit

Permalink
improve: allow two same name downloads;
Browse files Browse the repository at this point in the history
code: may contain bug.
  • Loading branch information
awaLiny2333 committed Dec 31, 2024
1 parent aa27ce9 commit d0916b7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Liny 的浏览器为 HarmonyOS NEXT 而构建,旨在为各种性能水平的
- [x] 缓存管理与清理
- [x] 广告过滤
- [ ] 隐私模式
- [ ] 下载选项自定义:每次弹窗询问、直接下载到系统下载目录
- [x] 页面内搜索
- [x] 禁用 JavaScript
- [ ] 网页链接右键/长按菜单(预览、操作)
Expand Down
1 change: 1 addition & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ or some of them will even lie on the table for ever... (ง •_•)ง
- [x] Cache management and clear
- [x] Ads blocking
- [ ] Private mode
- [ ] Customizable download options: ask each time & directly download to system download directory
- [x] In-page text search
- [x] Disable JavaScript
- [ ] Right click or long press menu on web links. (For preview and operations)
Expand Down
1 change: 0 additions & 1 deletion home/src/main/ets/blocks/meowWebView.ets
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ struct meowWebView {
.height("100%")
.backgroundColor('transparent')
.databaseAccess(true)
.onlineImageAccess(true)
.javaScriptAccess(this.my_js(Label.index_key))
.imageAccess(this.my_image(Label.index_key))
.onlineImageAccess(this.my_image(Label.index_key))
Expand Down
55 changes: 34 additions & 21 deletions home/src/main/ets/hosts/bunch_of_downloads.ets
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { fileUri, fileIo as fs, picker } from '@kit.CoreFileKit';
export class bunch_of_downloads {
list_of_on_going_tasks: request.DownloadTask[] = [];
list_of_downloaded_size: number[] = [];
list_of_target_folders: string[] = [];
list_of_full_size: number[] = [];
list_of_file_names: string[] = [];
list_of_paused: boolean[] = [];
list_of_urls: string[] = [];
list_of_additional_info: string[][] = [];
last_action: number = 0;
context = getContext();

/**
* A class to handle download tasks.
Expand All @@ -23,7 +23,7 @@ export class bunch_of_downloads {
return;
}

let filesDir = this.context.filesDir;
let filesDir = getContext().filesDir;
try {
fs.mkdirSync(filesDir + '/downloads');
} catch (e) {
Expand Down Expand Up @@ -58,16 +58,26 @@ export class bunch_of_downloads {
AppStorage.set('universal_new_download_filename_gateway', '');
}

// Produce task directory
let file_dir = getContext().filesDir + '/downloads/' + file_name + '_' + Date.now().toString() + '/';

// Create task folder
try {
fs.mkdirSync(file_dir);
} catch (e) {
console.log('[Meow][bunch_of_downloads] Task folder already created')
}

// connect path
let download_target_path = this.context.filesDir + '/downloads/' + file_name;
let download_target_path = file_dir + file_name;

let config: request.DownloadConfig = {
url: url,
filePath: download_target_path,
};

try {
request.downloadFile(this.context, config).then((downloadTask: request.DownloadTask) => {
request.downloadFile(getContext(), config).then((downloadTask: request.DownloadTask) => {
this.last_action = Date.now();

downloadTask.on('complete', () => {
Expand Down Expand Up @@ -113,18 +123,17 @@ export class bunch_of_downloads {
this.list_of_downloaded_size.push(0);
this.list_of_paused.push(false);
this.list_of_additional_info.push(additional_info);
this.list_of_target_folders.push(file_dir);

downloadTask.getTaskInfo().then((info) => {
this.list_of_full_size.push(info.downloadTotalBytes);
this.list_of_file_names.push(file_name_overwrite ? file_name : info.fileName);
this.list_of_urls.push(info.targetURI);
})

})
.catch((err: BusinessError) => {
console.error(`[bunch_of_downloads][ERROR] Meow Invoke downloadTask failed, code is ${err.code}, message is ${err.message}`);
});

}).catch((err: BusinessError) => {
console.error(`[bunch_of_downloads][ERROR] Meow Invoke downloadTask failed, code is ${err.code}, message is ${err.message}`);
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`[bunch_of_downloads][ERROR] Invoke downloadFile failed, code is ${err.code}, message is ${err.message}`);
Expand Down Expand Up @@ -153,20 +162,19 @@ export class bunch_of_downloads {
* Delete a download task in this bunch_of_downloads.
* @param index The index of target task in the tasks list to be deleted.
* */
async delete_task(index: number) {
delete_task(index: number) {

let file_path = (await this.list_of_on_going_tasks[index].getTaskInfo()).filePath
this.list_of_on_going_tasks[index].delete();

try {
fs.unlink(file_path)
fs.rmdir(this.list_of_target_folders[index]);
} catch (e) {
console.error("[ERROR][bunch_of_downloads][delete_task] Unlink file failed. " + e)
}

this.list_of_on_going_tasks[index].delete();

this.list_of_on_going_tasks.splice(index, 1);
this.list_of_downloaded_size.splice(index, 1);
this.list_of_target_folders.splice(index, 1);
this.list_of_full_size.splice(index, 1);
this.list_of_file_names.splice(index, 1);
this.list_of_paused.splice(index, 1);
Expand Down Expand Up @@ -197,7 +205,7 @@ export class bunch_of_downloads {
try {
let documentSaveOptions = new picker.DocumentSaveOptions();
documentSaveOptions.newFileNames = [file_name];
let documentPicker = new picker.DocumentViewPicker(this.context);
let documentPicker = new picker.DocumentViewPicker(getContext());

await documentPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
target_uri = documentSaveResult
Expand All @@ -210,8 +218,10 @@ export class bunch_of_downloads {

let writeLen = file_size;
fs.copy(fileUri.getUriFromPath(file_path), target_uri[0]).then(() => {
fs.unlink(file_path);
// fs.unlink(this.list_of_target_folders[index]);
fs.rmdir(this.list_of_target_folders[index])
});

console.info("[bunch_of_downloads] write data to file succeed and size is:" + writeLen + " @ " +
target_uri[0]);

Expand Down Expand Up @@ -239,11 +249,14 @@ export class bunch_of_downloads {
delete_all_downloaded_files() {
try {
let filesDir = getContext().filesDir;
let list = fs.listFileSync(filesDir + "/downloads");

for (let index = 0; index < list.length; index++) {
fs.unlink(filesDir + "/downloads/" + list[index]);
}
fs.rmdirSync(filesDir + "/downloads");
fs.mkdirSync(filesDir + "/downloads")

// let list = fs.listFileSync(filesDir + "/downloads");
//
// for (let index = 0; index < list.length; index++) {
// fs.rmdir(filesDir + "/downloads/" + list[index]);
// }
// Delete all downloaded files
} catch (e) {
console.error(e)
Expand Down
4 changes: 3 additions & 1 deletion home/src/main/ets/hosts/bunch_of_tabs.ets
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ export class bunch_of_tabs {
for (let index = start; index < this.Tabs.length; index++) {
let web_state = this.Tabs[index].web_state_array;
let identifier = "continue/continue_tabs_web_state_array_" + index.toString();
sandbox_save(identifier, web_state.buffer);
if (web_state != null) {
sandbox_save(identifier, web_state.buffer);
}
}
}
}
2 changes: 1 addition & 1 deletion home/src/main/resources/base/element/string.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
},
{
"name": "Settings_experience_history_date_format",
"value": "Month/Date/Year, Hour:Minute:Second"
"value": "Month/Date/Year, H:M:S"
},
{
"name": "Settings_edit_selecting",
Expand Down

0 comments on commit d0916b7

Please sign in to comment.