Sfoglia il codice sorgente

Fix createAssetUploadEndpoint (#4106)

Bartosz Dryl 1 anno fa
parent
commit
047c3b9d98

+ 1 - 1
packages/atlas/src/providers/uploads/uploads.hooks.ts

@@ -32,7 +32,7 @@ export const useStartFileUpload = () => {
   const navigate = useNavigate()
   const { displaySnackbar } = useSnackbar()
   const { getClosestStorageOperatorForBag, markStorageOperatorFailed } = useStorageOperators()
-  const { mutateAsync: uploadMutation } = useMutation('subtitles-fetch', (params: MutationParams) =>
+  const { mutateAsync: uploadMutation } = useMutation('upload-assets', (params: MutationParams) =>
     axios.post(params.url, params.data, params.config)
   )
 

+ 27 - 0
packages/atlas/src/utils/asset.test.ts

@@ -0,0 +1,27 @@
+import { createAssetUploadEndpoint } from './asset'
+
+const urlWithNoSlash = 'https://example.com/colossus-2'
+const urlWithSlash = 'https://example.com/colossus-2/'
+const notUrl = 'notaurl'
+const mockedUploadParams = {
+  'dataObjectId': '9999',
+  'storageBucketId': '999',
+  'bagId': 'dynamic:channel:999',
+}
+
+const expectedUrl =
+  'https://example.com/colossus-2/api/v1/files?dataObjectId=9999&storageBucketId=999&bagId=dynamic%3Achannel%3A999'
+
+describe('createAssetUploadEndpoint', () => {
+  it('should return correct url when there is no slash in the end', () => {
+    expect(createAssetUploadEndpoint(urlWithNoSlash, mockedUploadParams)).toEqual(expectedUrl)
+  })
+
+  it('should return correct url when there is slash in the end', () => {
+    expect(createAssetUploadEndpoint(urlWithSlash, mockedUploadParams)).toEqual(expectedUrl)
+  })
+
+  it('should throw error when the url is incorrect', () => {
+    expect(() => createAssetUploadEndpoint(notUrl, mockedUploadParams)).toThrowError()
+  })
+})

+ 10 - 9
packages/atlas/src/utils/asset.ts

@@ -12,15 +12,16 @@ type UploadRequestParams = {
   bagId: string
 }
 export const createAssetUploadEndpoint = (operatorEndpoint: string, uploadParams: UploadRequestParams) => {
-  const uploadEndpoint = new URL(atlasConfig.storage.uploadPath, operatorEndpoint)
-  Object.entries(uploadParams).forEach(([key, value]) => {
-    uploadEndpoint.searchParams.set(key, value)
-  })
-  return uploadEndpoint.toString()
-}
-
-export const createAssetDownloadEndpoint = (distributionOperatorEndpoint: string, dataObjectId: string) => {
-  return joinUrlFragments(distributionOperatorEndpoint, atlasConfig.storage.assetPath, dataObjectId)
+  try {
+    const url = operatorEndpoint[operatorEndpoint.length - 1] === '/' ? operatorEndpoint : operatorEndpoint + '/'
+    const uploadEndpoint = new URL(atlasConfig.storage.uploadPath, url)
+    Object.entries(uploadParams).forEach(([key, value]) => {
+      uploadEndpoint.searchParams.set(key, value)
+    })
+    return uploadEndpoint.toString()
+  } catch (error) {
+    throw new Error(error)
+  }
 }
 
 export const imageUrlValidation = async (imageUrl: string): Promise<boolean> =>