Фикс предпросмотра файлов с абсолютными self-URL

- extractFileKey понимает абсолютные URL (по pathname) — превью работает для уже записанных файлов
- normalizeFileUrl: абсолютные self-URL нормализуются в относительные при записи file-полей (REST field-values, MCP update_task_fields/upload-инструменты, bot-api, create task)
This commit is contained in:
2026-07-24 11:10:55 +03:00
parent b981614095
commit 59079992dc
6 changed files with 81 additions and 16 deletions

View File

@@ -19,13 +19,22 @@ export function stripFileTokensFromHtml(html: string): string {
}
// Extracts the file key from a local file URL.
// Supports /api/files/:key and /uploads/:key patterns.
// Supports /api/files/:key and /uploads/:key patterns, including absolute
// self-URLs (https://iistwin.ru/api/files/<key>) written by external agents via REST/MCP.
export function extractFileKey(url: string): string | null {
if (url.startsWith('/api/files/')) {
return url.slice('/api/files/'.length).split('?')[0];
let path = url;
if (url.startsWith('http://') || url.startsWith('https://')) {
try {
path = new URL(url).pathname;
} catch {
return null;
}
}
if (url.startsWith('/uploads/')) {
return url.slice('/uploads/'.length).split('?')[0];
if (path.startsWith('/api/files/')) {
return path.slice('/api/files/'.length).split('?')[0];
}
if (path.startsWith('/uploads/')) {
return path.slice('/uploads/'.length).split('?')[0];
}
return null;
}