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

use substitutions for external service endpoints #1690

Closed
wants to merge 5 commits into from
Closed
Changes from 2 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
36 changes: 29 additions & 7 deletions web/src/views/FileBrowserView/FileBrowser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ import axios from 'axios';

import { dandiRest } from '@/rest';
import { useDandisetStore } from '@/stores/dandiset';
import type { AssetFile, AssetPath } from '@/types';
import type { AssetPath } from '@/types';
import FileBrowserPagination from '@/components/FileBrowser/FileBrowserPagination.vue';
import FileUploadInstructions from '@/components/FileBrowser/FileUploadInstructions.vue';

Expand Down Expand Up @@ -309,35 +309,35 @@ const EXTERNAL_SERVICES = [
name: 'Bioimagesuite/Viewer',
regex: /\.nii(\.gz)?$/,
maxsize: 1e9,
endpoint: 'https://bioimagesuiteweb.github.io/unstableapp/viewer.html?image=',
endpoint: 'https://bioimagesuiteweb.github.io/unstableapp/viewer.html?image=$s3_url$',
},

{
name: 'MetaCell/NWBExplorer',
regex: /\.nwb$/,
maxsize: 1e9,
endpoint: 'http://nwbexplorer.opensourcebrain.org/nwbfile=',
endpoint: 'http://nwbexplorer.opensourcebrain.org/nwbfile=$s3_url$',
},

{
name: 'VTK/ITK Viewer',
regex: /\.ome\.zarr$/,
maxsize: Infinity,
endpoint: 'https://kitware.github.io/itk-vtk-viewer/app/?gradientOpacity=0.3&image=',
endpoint: 'https://kitware.github.io/itk-vtk-viewer/app/?gradientOpacity=0.3&image=$s3_url$',
},

{
name: 'OME Zarr validator',
regex: /\.ome\.zarr$/,
maxsize: Infinity,
endpoint: 'https://ome.github.io/ome-ngff-validator/?source=',
endpoint: 'https://ome.github.io/ome-ngff-validator/?source=$s3_url$',
},

{
name: 'Neurosift',
regex: /\.nwb$/,
maxsize: Infinity,
endpoint: 'https://flatironinstitute.github.io/neurosift?p=/nwb&url=',
endpoint: 'https://flatironinstitute.github.io/neurosift?p=/nwb&url=$api_download_url$',
},
];
type Service = typeof EXTERNAL_SERVICES[0];
Expand Down Expand Up @@ -377,18 +377,40 @@ const isOwner = computed(() => !!(
));
const itemsNotFound = computed(() => items.value && !items.value.length);

function replaceStringsInEndpoint(
endpoint: string,
substitutions: {[key: string]: string} | undefined,
) {
if (!substitutions) return endpoint;
let result = endpoint;
Object.entries(substitutions).forEach(([key, value]) => {
result = result.replace(key, value);
});
return result;
}

const isStaging = process.env.VUE_APP_SENTRY_ENVIRONMENT === 'staging';

function getExternalServices(path: AssetPath) {
const servicePredicate = (service: Service, _path: AssetPath) => (
new RegExp(service.regex).test(path.path)
&& _path.asset !== null
&& _path.aggregate_size <= service.maxsize
);

const baseApiUrl = isStaging
? 'https://api-staging.dandiarchive.org'
: 'https://api.dandiarchive.org';
yarikoptic marked this conversation as resolved.
Show resolved Hide resolved
const substitutions = path.asset ? {
$s3_url$: trimEnd(path.asset.url, '/'),
$api_download_url$: `${baseApiUrl}/api/assets/${path.asset.asset_id}/download/`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are both URLs to the asset download. So I think we could/should make them a little more uniform, what about

Suggested change
$s3_url$: trimEnd(path.asset.url, '/'),
$api_download_url$: `${baseApiUrl}/api/assets/${path.asset.asset_id}/download/`,
$asset_s3_download_url$: trimEnd(path.asset.url, '/'),
$asset_api_download_url$: `${baseApiUrl}/api/assets/${path.asset.asset_id}/download/`,

so in the future we might expand the listing here without fear of collision?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW , in #1655 (does this PR supersedes it?) we seems also wanted to expose asset_id. I think it would be safe to include it here as well but I guess we could add more as demand comes.

Copy link
Contributor Author

@magland magland Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @yarikoptic !

I have adjusted the strings as you suggested and added a $asset_id$ (which of course is unused at this point)

Yes, I think this is accomplishing the idea laid out in #1655

} : undefined;

return EXTERNAL_SERVICES
.filter((service) => servicePredicate(service, path))
.map((service) => ({
name: service.name,
url: `${service.endpoint}${trimEnd((path.asset as AssetFile).url, '/')}`,
url: replaceStringsInEndpoint(service.endpoint, substitutions),
}));
}

Expand Down