Skip to content

Commit

Permalink
refactor subtitle download
Browse files Browse the repository at this point in the history
  • Loading branch information
an-lee committed Jun 16, 2023
1 parent 009f505 commit baaead6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
51 changes: 30 additions & 21 deletions src/main/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "fs";
import https from "https";
import path from "path";

export const generatePortNumber = () => {
const min = 1024;
Expand All @@ -12,41 +13,49 @@ export const generatePortNumber = () => {
export const toUnixPath = (path: string) =>
path.replace(/[\\/]+/g, "/").replace(/^([a-zA-Z]+:|\.\/)/, "");

export const download = (url: string, dest: string) => {
export const download = (
url: string,
dir: string,
filename?: string
): Promise<string> => {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest, { flags: "wx" });
const regexp = /filename=\"(.*)\"/gi;

const request = https.get(url, (response) => {
if (response.statusCode === 200) {
filename =
filename ||
(regexp.exec(response.headers["content-disposition"]) || [])[1];
filename = filename || new URL(url).pathname.split("/").pop();
const dest = path.join(dir, filename);
const file = fs.createWriteStream(dest, {
flags: "wx",
});
response.pipe(file);

file.on("finish", () => {
resolve(dest);
});

file.on("error", (err: any) => {
file.close();

if (err.code === "EEXIST") {
resolve(dest);
} else {
fs.unlink(dest, () => {}); // Delete temp file
reject(err.message);
}
});
} else {
file.close();
fs.unlink(dest, () => {}); // Delete temp file
reject(
`Server responded with ${response.statusCode}: ${response.statusMessage}`
);
}
});

request.on("error", (err) => {
file.close();
fs.unlink(dest, () => {}); // Delete temp file
reject(err.message);
});

file.on("finish", () => {
resolve(dest);
});

file.on("error", (err: any) => {
file.close();

if (err.code === "EEXIST") {
resolve(dest);
} else {
fs.unlink(dest, () => {}); // Delete temp file
reject(err.message);
}
});
});
};
19 changes: 13 additions & 6 deletions src/main/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,19 @@ const ipcHandlers = () => {
return subtitlesServer.serve(path);
});

ipcMain.handle("subtitles-server-download", async (_event, url, fileName) => {
log.debug("subtitles-server-download", url);
const dest = path.join(SUBTITLE_CACHE_DIR, fileName);
await download(url, dest);
return subtitlesServer.serve(dest);
});
ipcMain.handle(
"subtitles-server-download",
async (_event, url, options: { fileName?: string; format?: string }) => {
log.debug("subtitles-server-download", url);
try {
const dest = await download(url, SUBTITLE_CACHE_DIR, options.fileName);
return subtitlesServer.serve(dest);
} catch (err) {
log.error("subtitles-server-download", err);
return null;
}
}
);

ipcMain.handle("settings-refresh", () => {
return {
Expand Down
18 changes: 12 additions & 6 deletions src/main/subtitles-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ export default class SubtitlesServer {
this.init();
}

const fileName = src.split("/").pop();
try {
fs.accessSync(src);
const fileName = path.basename(src);

const dest = path.join(SUBTITLE_CACHE_DIR, fileName);
if (src != dest) {
fs.copyFileSync(src, dest);
}
const dest = path.join(SUBTITLE_CACHE_DIR, fileName);
if (src != dest) {
fs.copyFileSync(src, dest);
}

return `http://localhost:${this.server.address().port}/${fileName}`;
return `http://localhost:${this.server.address().port}/${fileName}`;
} catch (err) {
log.error(err);
return null;
}
}
}

0 comments on commit baaead6

Please sign in to comment.