face-api/src/common/getModelUris.ts

35 lines
1.0 KiB
TypeScript
Raw Normal View History

2020-08-18 13:54:53 +02:00
export function getModelUris(uri: string | undefined, defaultModelName: string) {
2020-12-23 17:26:55 +01:00
const defaultManifestFilename = `${defaultModelName}-weights_manifest.json`;
2020-08-18 13:54:53 +02:00
if (!uri) {
return {
modelBaseUri: '',
2020-12-23 17:26:55 +01:00
manifestUri: defaultManifestFilename,
};
2020-08-18 13:54:53 +02:00
}
if (uri === '/') {
return {
modelBaseUri: '/',
2020-12-23 17:26:55 +01:00
manifestUri: `/${defaultManifestFilename}`,
};
2020-08-18 13:54:53 +02:00
}
2020-12-23 17:26:55 +01:00
// eslint-disable-next-line no-nested-ternary
2020-08-18 13:54:53 +02:00
const protocol = uri.startsWith('http://') ? 'http://' : uri.startsWith('https://') ? 'https://' : '';
uri = uri.replace(protocol, '');
2020-12-23 17:26:55 +01:00
const parts = uri.split('/').filter((s) => s);
2020-08-18 13:54:53 +02:00
const manifestFile = uri.endsWith('.json')
? parts[parts.length - 1]
2020-12-23 17:26:55 +01:00
: defaultManifestFilename;
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
let modelBaseUri = protocol + (uri.endsWith('.json') ? parts.slice(0, parts.length - 1) : parts).join('/');
modelBaseUri = uri.startsWith('/') ? `/${modelBaseUri}` : modelBaseUri;
2020-08-18 13:54:53 +02:00
return {
modelBaseUri,
2020-12-23 17:26:55 +01:00
manifestUri: modelBaseUri === '/' ? `/${manifestFile}` : `${modelBaseUri}/${manifestFile}`,
};
}