Fix getS3Key to normalize paths — resolve ../ to prevent duplicate S3 keys

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 08:33:09 -08:00
parent 1f95c5c70b
commit f0877932f6

View File

@@ -11,9 +11,8 @@
* storage: { type: "local" } (default)
*/
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { basename, dirname } from 'path';
import { basename, dirname, join, resolve as resolvePath } from 'path';
import { tmpdir } from 'os';
import { join } from 'path';
let _s3Client = null;
let _config = { type: 'local' };
@@ -27,15 +26,15 @@ export function storageType() {
}
function getS3Key(filePath) {
// Extract relative path from project root (e.g. config/foo.json or data/bar.json)
// Normalize and extract relative path from project root (e.g. data/jobs_queue.json)
const storageUrl = new URL(import.meta.url);
const projectRoot = dirname(dirname(storageUrl.pathname));
const abs = filePath.startsWith('/') ? filePath : join(projectRoot, filePath);
const abs = resolvePath(filePath.startsWith('/') ? filePath : join(projectRoot, filePath));
if (abs.startsWith(projectRoot + '/')) {
return abs.slice(projectRoot.length + 1);
}
// Fallback: use last two path segments (e.g. data/jobs_queue.json)
const parts = filePath.split('/');
// Fallback: use last two path segments
const parts = abs.split('/');
return parts.slice(-2).join('/');
}