Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
const fs = require("fs");
const path = require("path");
const { app } = require("electron");
const debugLogger = require("./debugLogger");
const { parseDbTimestamp } = require("./dbTimestamp");
class AudioStorageManager {
constructor() {
this.audioDir = path.join(app.getPath("userData"), "audio");
this.ensureAudioDir();
}
ensureAudioDir() {
try {
fs.mkdirSync(this.audioDir, { recursive: true });
} catch (error) {
debugLogger.error(
"Failed to create audio directory",
{ error: error.message },
"audio-storage"
);
}
}
_buildFilename(transcriptionId, timestamp) {
if (timestamp) {
// Named in the user's own wall clock, so the stored instant has to be
// resolved before it is read -- a bare SQLite timestamp is UTC, not local.
const d = parseDbTimestamp(timestamp);
if (d) {
const pad = (n) => String(n).padStart(2, "0");
const date = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
const time = `${pad(d.getHours())}-${pad(d.getMinutes())}-${pad(d.getSeconds())}`;
return `OpenWhispr-${date}-${time}-${transcriptionId}.webm`;
}
}
return `OpenWhispr-${transcriptionId}.webm`;
}
saveAudio(transcriptionId, audioBuffer, timestamp) {
try {
const filename = this._buildFilename(transcriptionId, timestamp);
const filePath = path.join(this.audioDir, filename);
fs.writeFileSync(filePath, audioBuffer);
debugLogger.debug(
"Audio saved",
{ transcriptionId, filename, size: audioBuffer.length },
"audio-storage"
);
return { success: true, path: filePath };
} catch (error) {
debugLogger.error(
"Failed to save audio",
{ transcriptionId, error: error.message },
"audio-storage"
);
return { success: false };
}
}
getAudioPath(transcriptionId) {
try {
const files = fs.readdirSync(this.audioDir);
const match = files.find(
(f) => f.endsWith(`-${transcriptionId}.webm`) || f === `${transcriptionId}.webm`
);
if (match) return path.join(this.audioDir, match);
} catch {}
return null;
}
getAudioBuffer(transcriptionId) {
const filePath = this.getAudioPath(transcriptionId);
if (!filePath) return null;
try {
return fs.readFileSync(filePath);
} catch (error) {
debugLogger.error(
"Failed to read audio",
{ transcriptionId, error: error.message },
"audio-storage"
);
return null;
}
}
deleteAudio(transcriptionId) {
try {
const filePath = this.getAudioPath(transcriptionId);
if (filePath) {
fs.unlinkSync(filePath);
debugLogger.debug("Audio deleted", { transcriptionId }, "audio-storage");
}
return { success: true };
} catch (error) {
debugLogger.error(
"Failed to delete audio",
{ transcriptionId, error: error.message },
"audio-storage"
);
return { success: false };
}
}
cleanupExpiredAudio(retentionDays, databaseManager) {
try {
const cutoffMs = Date.now() - retentionDays * 86400000;
const files = fs.readdirSync(this.audioDir).filter((f) => f.endsWith(".webm"));
const expiredIds = [];
let kept = 0;
for (const file of files) {
const filePath = path.join(this.audioDir, file);
try {
const stats = fs.statSync(filePath);
if (stats.mtimeMs < cutoffMs) {
fs.unlinkSync(filePath);
// Extract ID from "OpenWhispr-...-{id}.webm" or legacy "{id}.webm"
const basename = path.basename(file, ".webm");
const lastDash = basename.lastIndexOf("-");
const id = lastDash !== -1 ? basename.slice(lastDash + 1) : basename;
expiredIds.push(id);
} else {
kept++;
}
} catch (error) {
debugLogger.error(
"Failed to process audio file during cleanup",
{ file, error: error.message },
"audio-storage"
);
}
}
if (expiredIds.length > 0 && databaseManager) {
databaseManager.clearAudioFlags(expiredIds);
}
debugLogger.info(
"Audio cleanup complete",
{ deleted: expiredIds.length, kept, retentionDays },
"audio-storage"
);
return { deleted: expiredIds.length, kept };
} catch (error) {
debugLogger.error("Audio cleanup failed", { error: error.message }, "audio-storage");
return { deleted: 0, kept: 0 };
}
}
deleteAllAudio() {
try {
const files = fs.readdirSync(this.audioDir).filter((f) => f.endsWith(".webm"));
for (const file of files) {
try {
fs.unlinkSync(path.join(this.audioDir, file));
} catch (error) {
debugLogger.error(
"Failed to delete audio file",
{ file, error: error.message },
"audio-storage"
);
}
}
debugLogger.info("All audio deleted", { count: files.length }, "audio-storage");
return { deleted: files.length };
} catch (error) {
debugLogger.error("Failed to delete all audio", { error: error.message }, "audio-storage");
return { deleted: 0 };
}
}
getStorageUsage() {
try {
const files = fs.readdirSync(this.audioDir).filter((f) => f.endsWith(".webm"));
let totalBytes = 0;
for (const file of files) {
try {
const stats = fs.statSync(path.join(this.audioDir, file));
totalBytes += stats.size;
} catch {
// Skip files that can't be stat'd
}
}
return { fileCount: files.length, totalBytes };
} catch (error) {
debugLogger.error("Failed to get storage usage", { error: error.message }, "audio-storage");
return { fileCount: 0, totalBytes: 0 };
}
}
}
module.exports = AudioStorageManager;