Files
everyone-can-use-english/enjoy/src/main/utils.ts
an-lee 265429a24e Feat: denoise recording & clean code (#473)
* denoise recording before saved

* Refactor audio processing and recording logic

* Remove unused code

* use echogarden to transcode

* remove ffmpeg-wasm

* add echogarden decode

* remove deprecated code

* ensure use posix path

* refactor echogarden transcode

* refactor recording denoise

* clean code

* expose align error in toast

* remove unused code
2024-04-02 11:10:19 +08:00

91 lines
2.2 KiB
TypeScript

import { createHash } from "crypto";
import { createReadStream } from "fs";
import settings from "./settings";
import path from "path";
export function hashFile(
path: string,
options: { algo: string }
): Promise<string> {
const algo = options.algo || "md5";
return new Promise((resolve, reject) => {
const hash = createHash(algo);
const stream = createReadStream(path);
stream.on("error", reject);
stream.on("data", (chunk) => hash.update(chunk));
stream.on("end", () => resolve(hash.digest("hex")));
});
}
export function hashBlob(
blob: Blob,
options: { algo: string }
): Promise<string> {
const algo = options.algo || "md5";
return new Promise((resolve, reject) => {
const hash = createHash(algo);
const reader = new FileReader();
reader.onload = () => {
if (reader.result instanceof ArrayBuffer) {
const buffer = Buffer.from(reader.result);
hash.update(buffer);
resolve(hash.digest("hex"));
} else {
reject(new Error("Unexpected result from FileReader"));
}
};
reader.onerror = reject;
reader.readAsArrayBuffer(blob);
});
}
/*
* Convert enjoy url to file path
*
* @param {string} enjoyUrl - enjoy url
* @returns {string} file path
*/
export function enjoyUrlToPath(enjoyUrl: string): string {
let filePath = enjoyUrl;
if (
enjoyUrl.match(/enjoy:\/\/library\/(audios|videos|recordings|speeches)/g)
) {
filePath = path.posix.join(
settings.userDataPath(),
enjoyUrl.replace("enjoy://library/", "")
);
} else if (enjoyUrl.startsWith("enjoy://library/")) {
filePath = path.posix.join(
settings.libraryPath(),
filePath.replace("enjoy://library/", "")
);
}
return filePath;
}
/*
* Convert file path to enjoy url
*
* @param {string} filePath - file path
* @returns {string} enjoy url
*/
export function pathToEnjoyUrl(filePath: string): string {
let enjoyUrl = filePath;
if (filePath.startsWith(settings.userDataPath())) {
enjoyUrl = `enjoy://library/${filePath.replace(
settings.userDataPath(),
""
)}`;
} else if (filePath.startsWith(settings.libraryPath())) {
enjoyUrl = `enjoy://library/${filePath.replace(
settings.libraryPath(),
""
)}`;
}
return enjoyUrl;
}