Enhance: Support flexible hash prefix length and organized caching
- Updated organized path logic to accept variable prefix length. - Refactored IPC handlers to pass hash_prefix_length from frontend. - Standardized parameter structures for check-cache and download-to-cache.
This commit is contained in:
@@ -7,23 +7,28 @@ import { expandPath } from './file_utils';
|
|||||||
let endpoints_in_progress: string[] = [];
|
let endpoints_in_progress: string[] = [];
|
||||||
|
|
||||||
export function registerFileHandlers() {
|
export function registerFileHandlers() {
|
||||||
function get_legacy_hashed_path(root: string, hash: string) {
|
// Flexible organization: [root]/[prefix_len-char-prefix]/[hash].file
|
||||||
return path.join(expandPath(root), `${hash}.file`);
|
function get_organized_hashed_path(root: string, hash: string, prefix_len: number = 2) {
|
||||||
|
const expanded_root = expandPath(root);
|
||||||
|
const prefix = hash.substring(0, Math.max(1, Math.min(prefix_len, 8)));
|
||||||
|
const dir = path.join(expanded_root, prefix);
|
||||||
|
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
||||||
|
return path.join(dir, `${hash}.file`);
|
||||||
}
|
}
|
||||||
|
|
||||||
ipcMain.handle('native:check-cache', async (event, { cache_root, hash }) => {
|
ipcMain.handle('native:check-cache', async (event, { cache_root, hash, hash_prefix_length = 2 }) => {
|
||||||
const full_path = get_legacy_hashed_path(cache_root, hash);
|
const full_path = get_organized_hashed_path(cache_root, hash, hash_prefix_length);
|
||||||
return fs.existsSync(full_path);
|
return fs.existsSync(full_path);
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMain.handle('native:download-to-cache', async (event, { url, cache_root, hash, api_key, account_id }) => {
|
ipcMain.handle('native:download-to-cache', async (event, { url, cache_root, hash, api_key, account_id, hash_prefix_length = 2 }) => {
|
||||||
const full_path = get_legacy_hashed_path(cache_root, hash);
|
const full_path = get_organized_hashed_path(cache_root, hash, hash_prefix_length);
|
||||||
const tmp_path = `${full_path}.tmp`;
|
const tmp_path = `${full_path}.tmp`;
|
||||||
|
|
||||||
if (endpoints_in_progress.includes(url)) return { success: true, status: 'in_progress' };
|
if (endpoints_in_progress.includes(url)) return { success: true, status: 'in_progress' };
|
||||||
if (fs.existsSync(full_path)) return { success: true, path: full_path, status: 'exists' };
|
if (fs.existsSync(full_path)) return { success: true, path: full_path, status: 'exists' };
|
||||||
|
|
||||||
console.log(`Native: Downloading ${hash} -> ${full_path}`);
|
console.log(`Native: Organized Download (${hash_prefix_length} chars) -> ${full_path}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
endpoints_in_progress.push(url);
|
endpoints_in_progress.push(url);
|
||||||
@@ -54,9 +59,9 @@ export function registerFileHandlers() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMain.handle('native:launch-from-cache', async (event, { cache_root, hash, temp_root, filename }) => {
|
ipcMain.handle('native:launch-from-cache', async (event, { cache_root, hash, temp_root, filename, hash_prefix_length = 2 }) => {
|
||||||
try {
|
try {
|
||||||
const source = get_legacy_hashed_path(cache_root, hash);
|
const source = get_organized_hashed_path(cache_root, hash, hash_prefix_length);
|
||||||
const expanded_temp = expandPath(temp_root);
|
const expanded_temp = expandPath(temp_root);
|
||||||
const target = path.join(expanded_temp, filename);
|
const target = path.join(expanded_temp, filename);
|
||||||
if (!fs.existsSync(expanded_temp)) fs.mkdirSync(expanded_temp, { recursive: true });
|
if (!fs.existsSync(expanded_temp)) fs.mkdirSync(expanded_temp, { recursive: true });
|
||||||
|
|||||||
Reference in New Issue
Block a user