Skip to content

The open alternative to Salesforce, designed for AI.

1.0 KB
Raw Blame
import { doNotTranslate } from 'twenty-shared/i18n';

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(
      doNotTranslate(`Invalid attachment path: ${attachmentFullPath}`),
    );
  }

  if (!rawPath.startsWith('attachment/')) {
    return attachmentFullPath?.split('?')[0];
  }

  const pathParts = rawPath.split('/');
  if (pathParts.length < 2) {
    throw new Error(
      doNotTranslate(
        `Invalid attachment path structure: ${rawPath}. Path must have at least two segments.`,
      ),
    );
  }
  const filename = pathParts.pop();

  pathParts.pop();

  return `${base}/files/${pathParts.join('/')}/${filename}`;
};