fix: correct file_id extraction in markdown file download links

The markdown link handler was incorrectly extracting file_id and filename
from URLs in the format files/{userId}/{file_id}. The .pop() method was
applied twice in reverse order, causing the userId to be assigned to the
file_id variable and the actual file_id to be used as filename.

This caused downloads to fail with 404 errors as the API was called with
/api/files/download/{userId}/{userId} instead of the correct
/api/files/download/{userId}/{file_id}.

Fixed by directly accessing parts[2] to get the file_id and using the
link text (children) as the filename. Also added children to the useMemo
dependency array to properly track changes.
This commit is contained in:
Bastian Seipp 2025-11-13 23:55:58 +01:00 committed by Danny Avila
parent d844754edf
commit 4f7992917b
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956

View file

@ -90,12 +90,13 @@ export const a: React.ElementType = memo(({ href, children }: TAnchorProps) => {
if (match && match[0]) {
const path = match[0];
const parts = path.split('/');
const name = parts.pop();
const file_id = parts.pop();
return { file_id, filename: name, filepath: path };
// parts = ['files', userId, file_id] or ['outputs', userId, file_id]
const file_id = parts[2]; // Get the file_id (third element)
const filename = typeof children === 'string' ? children : file_id; // Use link text as filename
return { file_id, filename, filepath: path };
}
return { file_id: '', filename: '', filepath: '' };
}, [user?.id, href]);
}, [user?.id, href, children]);
const { refetch: downloadFile } = useFileDownload(user?.id ?? '', file_id);
const props: { target?: string; onClick?: React.MouseEventHandler } = { target: '_new' };