Skip to content

Commit

Permalink
improve output file name conflict handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Last-Order committed Oct 14, 2021
1 parent bf53249 commit cbde8f3
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minyami",
"version": "4.4.4",
"version": "4.4.5",
"description": "",
"main": "dist/exports.js",
"types": "dist/exports.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/core/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class ArchiveDownloader extends Downloader {
this.emit("finished");
}
muxer(this.outputFileList, this.outputPath)
.then(async () => {
.then(async (outputPath) => {
logger.info("End of merging.");
logger.info("Starting cleaning temporary files.");
try {
Expand All @@ -437,7 +437,7 @@ class ArchiveDownloader extends Downloader {
logger.warning("Fail to parse previous tasks, ignored.");
logger.warning(error.message);
}
logger.info(`All finished. Check your file at [${path.resolve(this.outputPath)}] .`);
logger.info(`All finished. Check your file at [${path.resolve(outputPath)}] .`);
this.emit("finished");
})
.catch(async (e) => {
Expand Down
8 changes: 0 additions & 8 deletions src/core/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,6 @@ class Downloader extends EventEmitter {
this.outputPath = output;
}

if (fs.existsSync(this.outputPath)) {
// output filename conflict
const pathArr = this.outputPath.split(".");
const filePath = pathArr.slice(0, -1).join(".");
const ext = pathArr[pathArr.length - 1];
this.outputPath = `${filePath}_${Date.now()}.${ext}`;
}

if (key) {
this.key = key;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ export default class LiveDownloader extends Downloader {
logger.info(`${this.finishedChunkCount} chunks downloaded. Start merging chunks.`);
const muxer = this.format === "ts" ? mergeToTS : mergeToMKV;
muxer(this.outputFileList, this.outputPath)
.then(async () => {
.then(async (outputPath) => {
logger.info("End of merging.");
await this.clean();
logger.info(`All finished. Check your file at [${path.resolve(this.outputPath)}] .`);
logger.info(`All finished. Check your file at [${path.resolve(outputPath)}] .`);
this.emit("finished");
})
.catch((e) => {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { URL } from "url";
import * as fs from "fs";

class CommonUtils {
static buildFullUrl(host: string, path: string) {
Expand All @@ -7,6 +8,16 @@ class CommonUtils {
}
return new URL(path, host).href;
}
static getAvailableOutputPath(path: string) {
if (fs.existsSync(path)) {
// output filename conflict
const pathArr = path.split(".");
const filePath = pathArr.slice(0, -1).join(".");
const ext = pathArr[pathArr.length - 1];
return `${filePath}_${Date.now()}.${ext}`;
}
return path;
}
}

export default CommonUtils;
18 changes: 10 additions & 8 deletions src/utils/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,42 @@ import * as crypto from "crypto";
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import { exec } from "./system";
import ProxyAgentHelper from "../utils/agent";
import UA from "../constants/ua";
import CommonUtils from "./common";

/**
* 合并视频文件
* @param fileList 文件列表
* @param output 输出路径
*/
export function mergeToMKV(fileList = [], output = "./output.mkv") {
return new Promise<void>(async (resolve) => {
const outputPath = CommonUtils.getAvailableOutputPath(output);
return new Promise<string>(async (resolve) => {
if (fileList.length === 0) {
return;
}
fileList = fileList.map((file, index) => {
return index === 0 ? file : `+${file}`;
});

const parameters = fileList.concat(["-o", output, "-q"]);
const parameters = fileList.concat(["-o", outputPath, "-q"]);

const tempFilename = `temp_${new Date().valueOf()}.json`;
fs.writeFileSync(`./${tempFilename}`, JSON.stringify(parameters, null, 2));
await exec(`mkvmerge @${tempFilename}`);
fs.unlinkSync(`./${tempFilename}`);
resolve();
resolve(outputPath);
});
}

export function mergeToTS(fileList = [], output = "./output.ts") {
const cliProgress = require("cli-progress");
return new Promise<void>(async (resolve) => {
const outputPath = CommonUtils.getAvailableOutputPath(output);
return new Promise<string>(async (resolve) => {
if (fileList.length === 0) {
resolve();
resolve(outputPath);
}

const writeStream = fs.createWriteStream(output);
const writeStream = fs.createWriteStream(outputPath);
const lastIndex = fileList.length - 1;
const bar = new cliProgress.SingleBar(
{
Expand All @@ -57,7 +59,7 @@ export function mergeToTS(fileList = [], output = "./output.ts") {
bar.update(i);
bar.stop();
writeStream.end();
resolve();
resolve(outputPath);
}
});
bar.update(i);
Expand Down

0 comments on commit cbde8f3

Please sign in to comment.