Skip to content

Commit

Permalink
Merge 1346
Browse files Browse the repository at this point in the history
Hotfix: File download with encodedURL
  • Loading branch information
bilalesi authored Jul 25, 2023
2 parents 578e2f5 + 7a035ba commit 007d26e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/shared/components/ImagePreview/ImagePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import {
SortDescendingOutlined,
DownloadOutlined,
} from '@ant-design/icons';
import { orderBy, isNil, create, isArray, isObject } from 'lodash';
import { orderBy, isNil, isArray, isObject } from 'lodash';
import { parseProjectUrl, parseResourceId } from '../Preview/Preview';
import nexusUrlHardEncode from '../../utils/nexusEncode';

import './ImagePreview.less';

Expand Down Expand Up @@ -114,7 +115,7 @@ const fetchImageResources = async ({
const rawData = await nexus.File.get(
orgLabel,
projectLabel,
parseResourceId(contentUrl),
nexusUrlHardEncode(contentUrl),
{ as: 'blob' }
);
const blob = new Blob([rawData as string], {
Expand Down
5 changes: 3 additions & 2 deletions src/shared/components/Preview/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import useNotification from '../../hooks/useNotification';
import TableViewerContainer from '../../containers/TableViewerContainer';
import { useSelector } from 'react-redux';
import { RootState } from '../../store/reducers';
import nexusUrlHardEncode from '../../utils/nexusEncode';

export const parseResourceId = (url: string) => {
const fileUrlPattern = /files\/([\w-]+)\/([\w-]+)\/(.*)/;
Expand Down Expand Up @@ -244,14 +245,14 @@ const Preview: React.FC<{
contentUrl = url;
options.rev = parseInt(rev, 10);
}

try {
const rawData = await nexus.File.get(
orgLabel,
projectLabel,
encodeURIComponent(contentUrl),
nexusUrlHardEncode(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 @@ -15,6 +15,7 @@ import {
import { parseResourceId } from '../Preview/Preview';
import { download } from '../../utils/download';
import { getDataExplorerResourceItemArray } from './editorUtils';
import nexusUrlHardEncode from '../../utils/nexusEncode';

const useResolvedLinkEditorPopover = () => {
const nexus = useNexusContext();
Expand Down Expand Up @@ -76,7 +77,7 @@ const useResolvedLinkEditorPopover = () => {
const data = await nexus.File.get(
orgLabel,
projectLabel,
encodeURIComponent(parseResourceId(resourceId)),
nexusUrlHardEncode(parseResourceId(resourceId)),
{ as: 'blob' }
);
return download(title, ext ?? 'json', data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
AnalysisAssetSparqlQueryRowResult,
ReportGeneration,
} from '../../types/plugins/report';
import useNotification from '../../../shared/hooks/useNotification';
import useNotification from '../../hooks/useNotification';
import nexusUrlHardEncode from '../../utils/nexusEncode';

async function fetchImageObjectUrl(
nexus: NexusClient,
Expand All @@ -41,7 +42,7 @@ async function fetchImageObjectUrl(
const rawData = await nexus.File.get(
orgLabel,
projectLabel,
encodeURIComponent(imageResourceId),
nexusUrlHardEncode(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
5 changes: 3 additions & 2 deletions src/shared/containers/TableViewerContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import * as csvParser from 'csv-string';

import TableViewer from '../components/TableViewer';
import useNotification from '../hooks/useNotification';
import { parseResourceId } from '../../shared/components/Preview/Preview';
import { parseResourceId } from '../components/Preview/Preview';
import nexusUrlHardEncode from '../utils/nexusEncode';

const TableViewerContainer: React.FC<{
resourceUrl: string;
Expand All @@ -27,7 +28,7 @@ const TableViewerContainer: React.FC<{
await nexus.File.get(
orgLabel,
projectLabel,
encodeURIComponent(resourceId),
nexusUrlHardEncode(resourceId),
{
as: 'text',
}
Expand Down
5 changes: 5 additions & 0 deletions src/shared/utils/nexusEncode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const nexusUrlHardEncode = (url: string): string => {
return encodeURIComponent(decodeURIComponent(url));
};

export default nexusUrlHardEncode;

0 comments on commit 007d26e

Please sign in to comment.