Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix attachment upload #6574

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ export const RichTextEditor = ({
return getFileAbsoluteURI(result.data.uploadFile);
};

const prepareBody = (newStringifiedBody: string) => {
if (!newStringifiedBody) return newStringifiedBody;

const body = JSON.parse(newStringifiedBody);

const bodyWithSignedPayload = body.map((block: any) => {
if (block.type !== 'image' || !block.props.url) {
return block;
}

const imageProps = block.props;
const imageUrl = new URL(imageProps.url);

imageUrl.searchParams.delete('token');

return {
...block,
props: {
...imageProps,
url: `${imageUrl.toString()}`,
},
};
});
return JSON.stringify(bodyWithSignedPayload);
};

const handlePersistBody = useCallback(
(activityBody: string) => {
if (!canCreateActivity) {
Expand All @@ -148,7 +174,7 @@ export const RichTextEditor = ({
if (!activityTitleHasBeenSet && fillTitleFromBody) {
updateTitleAndBody(activityBody);
} else {
persistBodyDebounced(activityBody);
persistBodyDebounced(prepareBody(activityBody));
}
},
[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSi
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
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=[^&]*$/, '');
};

export const useUploadAttachmentFile = () => {
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
const [uploadFile] = useUploadFileMutation();
Expand All @@ -29,9 +34,9 @@ export const useUploadAttachmentFile = () => {
},
});

const attachmentUrl = result?.data?.uploadFile;
const attachmentPath = result?.data?.uploadFile;

if (!attachmentUrl) {
if (!attachmentPath) {
return;
}

Expand All @@ -42,7 +47,7 @@ export const useUploadAttachmentFile = () => {
const attachmentToCreate = {
authorId: currentWorkspaceMember?.id,
name: file.name,
fullPath: attachmentUrl,
fullPath: computePathWithoutToken(attachmentPath),
type: getFileType(file.name),
[targetableObjectFieldIdName]: targetableObject.id,
createdAt: new Date().toISOString(),
Expand Down
Loading