Skip to content

Commit

Permalink
Change download to ky
Browse files Browse the repository at this point in the history
  • Loading branch information
ta-Hirose committed Aug 5, 2022
1 parent 35615ce commit 239e53f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 0 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { download } from "https://deno.land/x/download@v1.0.1/mod.ts";
export type { DownlodedFile } from "https://deno.land/x/download@v1.0.1/mod.ts";
export { Command } from "https://deno.land/x/cliffy@v0.24.2/command/mod.ts";
export {
DenoLandProvider,
Expand Down
6 changes: 3 additions & 3 deletions libs/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ const installFromDimFile = async (isUpdate = false) => {
content.headers,
).then(async (result) => {
consoleAnimation.stop();
await executePostprocess(content.postProcesses, result.fullPath);
await executePostprocess(content.postProcesses, fullPath);
console.log(
Colors.green(`Installed to ${result.fullPath}`),
Colors.green(`Installed to ${fullPath}`),
);
console.log();
resolve({
name: content.name,
url: content.url,
path: result.fullPath,
path: fullPath,
catalogUrl: null,
catalogResourceId: null,
lastModified: null,
Expand Down
24 changes: 14 additions & 10 deletions libs/downloader.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import { download, DownlodedFile, ensureDirSync } from "../deps.ts";
import { ensureDirSync, ky } from "../deps.ts";
import { DEFAULT_DATAFILES_PATH } from "./consts.ts";

export class Downloader {
async download(
url: URL,
name: string,
headers?: Record<string, string>,
): Promise<DownlodedFile> {
): Promise<string> {
const splitedURLPath = url.pathname.split("/");
<<<<<<< HEAD
const dir = `${DEFAULT_DATAFILES_PATH}/${name}`;
const file = splitedURLPath[splitedURLPath.length - 1];
=======
const joinedDirPath = splitedURLPath
.slice(0, splitedURLPath.length - 1)
.join("/");
const dir = `${DEFAULT_DATAFILES_PATH}/${url.hostname}${joinedDirPath}`;
const fileName = splitedURLPath[splitedURLPath.length - 1];
>>>>>>> Change download to ky
ensureDirSync(dir);
const reqInit: RequestInit = {
method: "GET",
headers: headers,
};
return await download(url, {
file,
dir,
}, reqInit);
const response = await ky.get(url, { headers: headers });
const path = dir + "/" + fileName;
Deno.writeFileSync(path, new Uint8Array(await response.arrayBuffer()));
return path;
}
}

0 comments on commit 239e53f

Please sign in to comment.