fixed ocr/tts... caching

This commit is contained in:
2026-02-25 00:16:52 +00:00
parent e95c5de70d
commit 6e8ee69311
4 changed files with 38 additions and 16 deletions
+25 -7
View File
@@ -10,8 +10,16 @@ import { readSttConfig } from '@@/api/server-settings/stt';
import { readOcrConfig } from '@@/api/server-settings/ocr';
const DEFAULT_HOME_DIRS = ['Downloads', 'Documents', 'Music', 'Video', 'Pictures', 'Onboarding'];
const OLD_CACHE_DIRS = ['ocr', 'tts', 'transcriptions', 'audio', 'video'];
const ONBOARDING_SEED = join(DATA_PATH, 'Onboarding');
async function cleanOldCacheDirs(userDataDir: string) {
for (const dir of OLD_CACHE_DIRS) {
const target = join(userDataDir, dir);
if (existsSync(target)) await rm(target, { recursive: true, force: true });
}
}
async function seedHomeDir(homeDir: string) {
for (const dir of DEFAULT_HOME_DIRS) {
const target = join(homeDir, dir);
@@ -60,6 +68,11 @@ router.get('/ls', async (ctx) => {
await mkdir(absPath, { recursive: true });
}
// Remove old top-level cache dirs (migrated to cache/ prefix)
if (ctx.req.query('root') === 'user-data') {
await cleanOldCacheDirs(rootDir);
}
let names: string[];
try {
names = await readdir(absPath);
@@ -234,7 +247,7 @@ router.get('/transcode', async (ctx) => {
const userDataDir = getUserDataDir(user.email);
const { dir, name } = parsePath(relPath);
const cacheRel = dir ? `video/${dir}/${name}.mp4` : `video/${name}.mp4`;
const cacheRel = dir ? `cache/video/${dir}/${name}.mp4` : `cache/video/${name}.mp4`;
const cacheAbs = resolve(userDataDir, cacheRel);
if (!existsSync(cacheAbs)) {
@@ -318,7 +331,7 @@ router.get('/transcode-audio', async (ctx) => {
});
// Save a cached result (ocr/tts/transcriptions/audio) next to the original file
const CACHE_PREFIXES = ['ocr/', 'tts/', 'transcriptions/', 'audio/'];
const CACHE_PREFIXES = ['cache/ocr/', 'cache/tts/', 'cache/transcriptions/', 'cache/audio/', 'cache/video/'];
router.post('/save-result', async (ctx) => {
const user = ctx.get('user');
@@ -328,7 +341,12 @@ router.post('/save-result', async (ctx) => {
const prefix = CACHE_PREFIXES.find((p) => cachedPath.startsWith(p));
if (!prefix) throw errors.BAD_REQUEST('Not a cached result path');
const relativePath = cachedPath.slice(prefix.length);
let relativePath = cachedPath.slice(prefix.length);
// Strip any nested cache prefixes (e.g. tts/ocr/photos/file.mp3 → photos/file.mp3)
let nested: string | undefined;
while ((nested = CACHE_PREFIXES.find((p) => relativePath.startsWith(p)))) {
relativePath = relativePath.slice(nested.length);
}
const userDataDir = getUserDataDir(user.email);
const srcAbs = resolve(userDataDir, cachedPath);
if (!existsSync(srcAbs)) throw errors.BAD_REQUEST('Cached file not found');
@@ -357,7 +375,7 @@ router.post('/tts', async (ctx) => {
const userDataDir = getUserDataDir(user.email);
const { dir, name } = parsePath(filePath.replace(/^\/+/, ''));
const cacheRel = dir ? `tts/${dir}/${name}.mp3` : `tts/${name}.mp3`;
const cacheRel = dir ? `cache/tts/${dir}/${name}.mp3` : `cache/tts/${name}.mp3`;
const cacheAbs = resolve(userDataDir, cacheRel);
if (existsSync(cacheAbs)) {
@@ -409,7 +427,7 @@ router.post('/ocr', async (ctx) => {
const userDataDir = getUserDataDir(user.email);
const { dir, name } = parsePath(filePath.replace(/^\/+/, ''));
const cacheRel = dir ? `ocr/${dir}/${name}.md` : `ocr/${name}.md`;
const cacheRel = dir ? `cache/ocr/${dir}/${name}.md` : `cache/ocr/${name}.md`;
const cacheAbs = resolve(userDataDir, cacheRel);
if (existsSync(cacheAbs)) {
@@ -486,7 +504,7 @@ router.post('/extract-audio', async (ctx) => {
const userDataDir = getUserDataDir(user.email);
const { dir, name } = parsePath(filePath.replace(/^\/+/, ''));
const cacheRel = dir ? `audio/${dir}/${name}.mp3` : `audio/${name}.mp3`;
const cacheRel = dir ? `cache/audio/${dir}/${name}.mp3` : `cache/audio/${name}.mp3`;
const cacheAbs = resolve(userDataDir, cacheRel);
if (existsSync(cacheAbs)) {
@@ -610,7 +628,7 @@ router.post('/transcribe', async (ctx) => {
const userDataDir = getUserDataDir(user.email);
const { dir, name } = parsePath(filePath.replace(/^\/+/, ''));
const cacheRel = dir ? `transcriptions/${dir}/${name}.md` : `transcriptions/${name}.md`;
const cacheRel = dir ? `cache/transcriptions/${dir}/${name}.md` : `cache/transcriptions/${name}.md`;
const cacheAbs = resolve(userDataDir, cacheRel);
if (existsSync(cacheAbs)) {