11744 emails broken image in emails (#12265)

- refactor file tokens
- update file token management
  - generate one token per file per workspaceId
  - move token from query params to url path
This commit is contained in:
martmull
2025-05-26 22:05:21 +02:00
committed by GitHub
parent 69badf2a66
commit aa58259019
53 changed files with 775 additions and 386 deletions

View File

@ -1,14 +0,0 @@
import { computePathWithoutToken } from '../useUploadAttachmentFile';
describe('computePathWithoutToken', () => {
it('should remove token from path', () => {
const input = 'https://example.com/image.jpg?token=abc123';
const expected = 'https://example.com/image.jpg';
expect(computePathWithoutToken(input)).toBe(expected);
});
it('should handle path without token', () => {
const input = 'https://example.com/image.jpg?size=large';
expect(computePathWithoutToken(input)).toBe(input);
});
});

View File

@ -7,13 +7,8 @@ import { getActivityTargetObjectFieldIdName } from '@/activities/utils/getActivi
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
import { isNonEmptyString } from '@sniptt/guards';
import { FileFolder, useUploadFileMutation } from '~/generated/graphql';
// Note: This is probably not the right way to do this.
export const computePathWithoutToken = (attachmentPath: string): string => {
return attachmentPath.replace(/\?token=[^&]*$/, '');
};
import { isDefined } from 'twenty-shared/utils';
export const useUploadAttachmentFile = () => {
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
@ -36,12 +31,14 @@ export const useUploadAttachmentFile = () => {
},
});
const attachmentPath = result?.data?.uploadFile;
const signedFile = result?.data?.uploadFile;
if (!isNonEmptyString(attachmentPath)) {
if (!isDefined(signedFile)) {
throw new Error("Couldn't upload the attachment.");
}
const { path: attachmentPath } = signedFile;
const targetableObjectFieldIdName = getActivityTargetObjectFieldIdName({
nameSingular: targetableObject.targetObjectNameSingular,
});
@ -49,7 +46,7 @@ export const useUploadAttachmentFile = () => {
const attachmentToCreate = {
authorId: currentWorkspaceMember?.id,
name: file.name,
fullPath: computePathWithoutToken(attachmentPath),
fullPath: attachmentPath,
type: getFileType(file.name),
[targetableObjectFieldIdName]: targetableObject.id,
createdAt: new Date().toISOString(),