-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Expand file tree
/
Copy pathgetAttachmentPath.ts
More file actions
32 lines (25 loc) · 909 Bytes
/
getAttachmentPath.ts
File metadata and controls
32 lines (25 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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}`;
};