Skip to content

Commit

Permalink
Fix(command cp'): key should be correct.
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonPrefect committed Dec 12, 2022
1 parent 8502b6d commit 79acb12
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
17 changes: 13 additions & 4 deletions commands/cp/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IFile } from "../../core/interfaces/IFile.ts";

const bars = new progress.MultiProgressBar({
title: "Downloading files",
display: "[:bar] :text :percent :time :completed/:total"
display: `[:bar] :text :percent :time :completed/:total${ansi.eraseLineEnd.toString()}`,
});
const {error, warn, info, success} = {error: colors.bold.red, warn: colors.bold.yellow, info: colors.bold.blue, success: colors.bold.green};

Expand Down Expand Up @@ -58,7 +58,13 @@ export default async function download(config: Config, paths: Array<string>, opt
}

if(options.sync){
tasks = await file.syncFilter(tasks, options.signUrl === true);
console.log(`Sync Hashing...${ansi.eraseLineEnd.toString()}`);
tty.cursorUp(1);
tasks = await file.syncFilter(tasks, options.signUrl === true, (file: IFile) => {
tty.eraseLine;
console.log(`Hashing ${file.key}...${ansi.eraseLineEnd.toString()}`);
tty.cursorUp(1);
});
}
if(options.signUrl){
console.log(warn("[WARN]"), "This url is CHARGED for CNY0.5/GB/DAY");
Expand All @@ -74,8 +80,8 @@ export default async function download(config: Config, paths: Array<string>, opt
if(options.sync){
size = 0;
}
downloading(`${task.key} => ${task.local}`, tasks.indexOf(task), tasks.length, task.size, size);
await file.downloadFile(task, options.signUrl);
downloading(`${task.key}=>${task.local}`, tasks.indexOf(task), tasks.length, task.size, size);
await file.downloadFile(task, options.signUrl); // qps limit!
size = fs.lstatSync(task.local!).size;
downloading(`${task.key} => ${task.local}`, tasks.indexOf(task) + 1, tasks.length, task.size, size);
}
Expand All @@ -86,6 +92,9 @@ function downloading(file: string, index: number, total: number, size: number,
complete++;
size++;
}
if(complete === size){ // For the progress don't `end'.
complete--;
}
bars.render([
{ completed: complete, total: size, text: file },
{ completed: index, total: total, text: "Total" }
Expand Down
18 changes: 15 additions & 3 deletions commands/cp/upload.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import * as path from "https://deno.land/[email protected]/path/mod.ts";
import * as fs from "https://deno.land/[email protected]/node/fs.ts";
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
import { tty } from "https://deno.land/x/[email protected]/ansi/tty.ts";
import { ansi } from "https://deno.land/x/[email protected]/ansi/ansi.ts";
import * as progress from "https://deno.land/x/[email protected]/mod.ts";
import { Config } from "../../core/main/Config.ts";
import { File } from "../../core/main/File.ts"
import { IFile } from "../../core/interfaces/IFile.ts";

const bars = new progress.MultiProgressBar({
title: "Uploading files",
display: "[:bar] :text :percent :time :completed/:total"
display: `[:bar] :text :percent :time :completed/:total${ansi.eraseLineEnd.toString()}`
});

export default async function upload(config: Config, paths: Array<string>, options: any){
Expand Down Expand Up @@ -41,23 +43,33 @@ export default async function upload(config: Config, paths: Array<string>, optio
for await (const entry of walk(fullpath, { maxDepth: options.recursive ? Infinity : 1 })){
if(entry.isFile){
files.push({
key: path.posix.join(dogePath, path.posix.normalize(entry.path).replace(path.posix.normalize(fullpath), "").replace("\\", "/")).replace("\\", "/"),
key: path.posix.normalize(path.join(dogePath, path.posix.normalize(entry.path).replace(path.posix.normalize(fullpath), ""))).replaceAll("\\", "/"),
local: entry.path
} as IFile);
}
}

files = file.filterFilesLocal(files, options.include, options.exclude);
}else{
throw new Error(`${fullpath} rather be a directory or a file to be upload.`);
}
if(options.sync){
files = await file.syncFilter(files, options.signUrl === true);
console.log(`Sync Hashing...${ansi.eraseLineEnd.toString()}`);
tty.cursorUp(1);
files = await file.syncFilter(files, options.signUrl === true, (file: IFile) => {
tty.eraseLine;
console.log(`Hashing ${file.local}...${ansi.eraseLineEnd.toString()}`);
tty.cursorUp(1);
});
}

return await file.uploadFiles(files, options.partSize, options.threadNum, uploading);
}

function uploading(file: string, index: number, total: number, complete: number){
if(complete === 100){
complete-=0.0001; // For the progress don't `end'.
}
bars.render([
{ completed: complete, total: 100, text: file },
{ completed: index, total: total, text: "Total" },
Expand Down
5 changes: 4 additions & 1 deletion core/main/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ export class File{
return FileService.calculateHash(file);
}

public async syncFilter(files: Array<IFile>, sign = false){
public async syncFilter(files: Array<IFile>, sign = false, callback: Function | undefined = undefined){
const result: Array<IFile> = [] as Array<IFile>;
for(const file of files){
if(callback){
callback(file);
}
let localHash = "";
try{
localHash = await File.getHashLocal(file);
Expand Down
4 changes: 2 additions & 2 deletions core/services/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export class FileService extends DogeService{
FilePath: file.local!,
onTaskStart: (taskInfo: COS.Task) => {
if(callback){
callback(`${file.local} => ${preprefix}${file.key}`, files.indexOf(file), files.length, 0);
callback(`${file.local}=>${preprefix}${file.key}`, files.indexOf(file), files.length, 0);
}
},
onProgress: (params: COS.ProgressInfo) => {
if(callback){
callback(`${file.local} => ${preprefix}${file.key}`, files.indexOf(file) + 1, files.length, Math.round(params.percent * 100));
callback(`${file.local}=>${preprefix}${file.key}`, files.indexOf(file) + 1, files.length, Math.round(params.percent * 100));
}
}
}, (err: COS.CosError, data: COS.SliceUploadFileResult) => {
Expand Down

0 comments on commit 79acb12

Please sign in to comment.