diff --git a/keyserver/src/services/blob.js b/keyserver/src/services/blob.js --- a/keyserver/src/services/blob.js +++ b/keyserver/src/services/blob.js @@ -5,6 +5,7 @@ import { makeBlobServiceEndpointURL, downloadBlob, + assignBlobHolder, } from 'lib/utils/blob-service.js'; import { uploadBlob, @@ -44,26 +45,9 @@ async function assignHolder( params: BlobDescriptor, ): Promise { - const { hash, holder } = params; + const { hash: blobHash, holder } = params; const headers = await createRequestHeaders(); - const assignHolderResponse = await fetch( - makeBlobServiceEndpointURL(blobService.httpEndpoints.ASSIGN_HOLDER), - { - method: blobService.httpEndpoints.ASSIGN_HOLDER.method, - body: JSON.stringify({ - holder, - blob_hash: hash, - }), - headers, - }, - ); - - if (!assignHolderResponse.ok) { - const { status, statusText } = assignHolderResponse; - return { success: false, reason: 'OTHER', status, statusText }; - } - - return { success: true }; + return assignBlobHolder({ blobHash, holder }, headers); } async function uploadBlobKeyserverWrapper( diff --git a/lib/actions/upload-actions.js b/lib/actions/upload-actions.js --- a/lib/actions/upload-actions.js +++ b/lib/actions/upload-actions.js @@ -28,13 +28,11 @@ makeBlobServiceEndpointURL, makeBlobServiceURI, generateBlobHolder, + assignBlobHolder, } from '../utils/blob-service.js'; import { getMessageForException } from '../utils/errors.js'; import { useDispatch } from '../utils/redux-utils.js'; -import { - handleHTTPResponseError, - createDefaultHTTPRequestHeaders, -} from '../utils/services-utils.js'; +import { createDefaultHTTPRequestHeaders } from '../utils/services-utils.js'; export type MultimediaUploadCallbacks = Partial<{ +onProgress: (percent: number) => void, @@ -126,24 +124,16 @@ // 1. Assign new holder for blob with given blobHash let blobAlreadyExists: boolean; try { - const assignHolderEndpoint = blobService.httpEndpoints.ASSIGN_HOLDER; - const assignHolderResponse = await fetch( - makeBlobServiceEndpointURL(assignHolderEndpoint), - { - method: assignHolderEndpoint.method, - body: JSON.stringify({ - holder: blobHolder, - blob_hash: blobHash, - }), - headers: { - ...defaultHeaders, - 'content-type': 'application/json', - }, - }, + const assignHolderResult = await assignBlobHolder( + { blobHash, holder: blobHolder }, + defaultHeaders, ); - handleHTTPResponseError(assignHolderResponse); + if (!assignHolderResult.success) { + const { status, statusText } = assignHolderResult; + throw new Error(`Server responded with HTTP ${status}: ${statusText}`); + } const { data_exists: dataExistsResponse } = - await assignHolderResponse.json(); + await assignHolderResult.response.json(); blobAlreadyExists = dataExistsResponse; } catch (e) { throw new Error( @@ -303,22 +293,14 @@ // 1. Assign new holder for blob with given blobHash try { - const assignHolderEndpoint = blobService.httpEndpoints.ASSIGN_HOLDER; - const assignHolderResponse = await fetch( - makeBlobServiceEndpointURL(assignHolderEndpoint), - { - method: assignHolderEndpoint.method, - body: JSON.stringify({ - holder: blobHolder, - blob_hash: blobHash, - }), - headers: { - ...defaultHeaders, - 'content-type': 'application/json', - }, - }, + const assignHolderResult = await assignBlobHolder( + { blobHash, holder: blobHolder }, + defaultHeaders, ); - handleHTTPResponseError(assignHolderResponse); + if (!assignHolderResult.success) { + const { status, statusText } = assignHolderResult; + throw new Error(`Server responded with HTTP ${status}: ${statusText}`); + } } catch (e) { throw new Error( `Failed to assign holder: ${ diff --git a/lib/utils/blob-service.js b/lib/utils/blob-service.js --- a/lib/utils/blob-service.js +++ b/lib/utils/blob-service.js @@ -75,6 +75,7 @@ export type BlobOperationResult = | { +success: true, + +response: Response, } | { +success: false, @@ -139,7 +140,35 @@ }; } - return { success: true }; + return { success: true, response: uploadBlobResponse }; +} + +async function assignBlobHolder( + blobInfo: BlobInfo, + headers: { [string]: string }, +): Promise { + const { blobHash, holder } = blobInfo; + const response = await fetch( + makeBlobServiceEndpointURL(blobServiceConfig.httpEndpoints.ASSIGN_HOLDER), + { + method: blobServiceConfig.httpEndpoints.ASSIGN_HOLDER.method, + body: JSON.stringify({ + holder, + blob_hash: blobHash, + }), + headers: { + ...headers, + 'content-type': 'application/json', + }, + }, + ); + + if (!response.ok) { + const { status, statusText } = response; + return { success: false, reason: 'OTHER', status, statusText }; + } + + return { success: true, response }; } async function assignMultipleHolders( @@ -243,6 +272,7 @@ makeBlobServiceEndpointURL, downloadBlob, uploadBlob, + assignBlobHolder, assignMultipleHolders, removeMultipleHolders, }; diff --git a/lib/utils/services-utils.js b/lib/utils/services-utils.js --- a/lib/utils/services-utils.js +++ b/lib/utils/services-utils.js @@ -23,13 +23,6 @@ // (secondary login). const usingRestoreFlow = false; -function handleHTTPResponseError(response: Response): void { - if (!response.ok) { - const { status, statusText } = response; - throw new Error(`Server responded with HTTP ${status}: ${statusText}`); - } -} - function createHTTPAuthorizationHeader(authMetadata: AuthMetadata): string { // explicit destructure to make it future-proof const { userID, deviceID, accessToken } = authMetadata; @@ -48,7 +41,6 @@ } export { - handleHTTPResponseError, usingCommServicesAccessToken, supportingMultipleKeyservers, relyingOnAuthoritativeKeyserver,