Files
twenty/packages/twenty-front/src/modules/activities/utils/getAttachmentPath.ts
T
Etienne 5c2c588885 Files - Migrate attachments in activities (#17808)
As attachment files have migrated from fullPath to file files field,
need to migrate richText logic to fit to new attachment file handling +
data migration
2026-02-13 09:11:23 +00:00

33 lines
909 B
TypeScript

export const getAttachmentPath = (attachmentFullPath: string) => {
if (attachmentFullPath.includes('/files-field/')) {
return attachmentFullPath?.split('?')[0];
}
if (!attachmentFullPath.includes('/files/')) {
return attachmentFullPath?.split('?')[0];
}
const base = attachmentFullPath?.split('/files/')[0];
const rawPath = attachmentFullPath?.split('/files/')[1]?.split('?')[0];
if (!rawPath) {
throw new Error(`Invalid attachment path: ${attachmentFullPath}`);
}
if (!rawPath.startsWith('attachment/')) {
return attachmentFullPath?.split('?')[0];
}
const pathParts = rawPath.split('/');
if (pathParts.length < 2) {
throw new Error(
`Invalid attachment path structure: ${rawPath}. Path must have at least two segments.`,
);
}
const filename = pathParts.pop();
pathParts.pop();
return `${base}/files/${pathParts.join('/')}/${filename}`;
};