Skip to content

Commit

Permalink
fix: decode and encode again the contentUrl to be sure that we can re…
Browse files Browse the repository at this point in the history
…solve the file

fix: use _self when possible

fix: remove version from files to be able to resolve by the resolver
  • Loading branch information
bilalesi committed Jul 25, 2023
1 parent 578e2f5 commit 1aeacce
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/shared/components/ImagePreview/ImagePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const fetchImageResources = async ({
const rawData = await nexus.File.get(
orgLabel,
projectLabel,
parseResourceId(contentUrl),
encodeURIComponent(decodeURIComponent(contentUrl)),
{ as: 'blob' }
);
const blob = new Blob([rawData as string], {
Expand Down
4 changes: 2 additions & 2 deletions src/shared/components/Preview/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ const Preview: React.FC<{
contentUrl = url;
options.rev = parseInt(rev, 10);
}

try {
const rawData = await nexus.File.get(
orgLabel,
projectLabel,
encodeURIComponent(contentUrl),
encodeURIComponent(decodeURIComponent(contentUrl)),
options
);

downloadBlobHelper(rawData, asset.name);
} catch (error) {
notification.error({
Expand Down
18 changes: 16 additions & 2 deletions src/shared/components/ResourceEditor/ResourcesLRUCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import LRUCache from 'lru-cache';

// TODO: Use nexus.httpGet to prepare for using http cache headers
// since the nexus SDK can not accept the headers as an argument

const parseResourceId = (url: string) => {
const fileUrlPattern = /files\/([\w-]+)\/([\w-]+)\/(.*)/;
if (fileUrlPattern.test(url)) {
const [, , , resourceId] = url.match(fileUrlPattern) as string[];
return decodeURIComponent(resourceId.split('?rev=')[0]);
}
return decodeURIComponent(url);
};

const lookByProjectResolver = async ({
nexus,
apiEndpoint,
Expand All @@ -18,7 +28,9 @@ const lookByProjectResolver = async ({
resourceId: string;
}): Promise<Resource> => {
return await nexus.httpGet({
path: `${apiEndpoint}/resolvers/${orgLabel}/${projectLabel}/_/${resourceId}`,
path: `${apiEndpoint}/resolvers/${orgLabel}/${projectLabel}/_/${encodeURIComponent(
parseResourceId(resourceId)
)}`,
});
};
const lookBySearchApi = async ({
Expand All @@ -31,7 +43,9 @@ const lookBySearchApi = async ({
resourceId: string;
}): Promise<TPagedResources> => {
return await nexus.httpGet({
path: `${apiEndpoint}/resources?locate=${resourceId}`,
path: `${apiEndpoint}/resources?locate=${encodeURIComponent(
parseResourceId(resourceId)
)}`,
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/shared/components/ResourceEditor/editorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export async function editorLinkResolutionHandler({
apiEndpoint,
orgLabel,
projectLabel,
resourceId: encodeURIComponent(url),
resourceId: url,
},
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const useResolvedLinkEditorPopover = () => {
const data = await nexus.File.get(
orgLabel,
projectLabel,
encodeURIComponent(parseResourceId(resourceId)),
encodeURIComponent(decodeURIComponent(parseResourceId(resourceId))),
{ as: 'blob' }
);
return download(title, ext ?? 'json', data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function fetchImageObjectUrl(
const rawData = await nexus.File.get(
orgLabel,
projectLabel,
encodeURIComponent(imageResourceId),
encodeURIComponent(decodeURIComponent(imageResourceId)),
{
as: 'blob',
}
Expand Down
15 changes: 9 additions & 6 deletions src/shared/containers/ResourceActionsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,15 @@ const ResourceActionsContainer: React.FunctionComponent<{
},
downloadFile: async () => {
try {
const data = await nexus.File.get(
orgLabel,
projectLabel,
parseResourceId(resource._self),
{ as: 'blob' }
);
const data = await nexus.httpGet({
path: resource._self,
headers: {
Accept: 'application/json',
},
context: {
as: 'blob',
},
});
return download(
resource._filename || getResourceLabel(resource),
resource._mediaType,
Expand Down
2 changes: 1 addition & 1 deletion src/shared/containers/TableViewerContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const TableViewerContainer: React.FC<{
await nexus.File.get(
orgLabel,
projectLabel,
encodeURIComponent(resourceId),
encodeURIComponent(decodeURIComponent(resourceId)),
{
as: 'text',
}
Expand Down

0 comments on commit 1aeacce

Please sign in to comment.