diff --git a/lib/gui/app/components/source-selector/source-selector.tsx b/lib/gui/app/components/source-selector/source-selector.tsx index 1f82391b..3f0b193d 100644 --- a/lib/gui/app/components/source-selector/source-selector.tsx +++ b/lib/gui/app/components/source-selector/source-selector.tsx @@ -65,6 +65,7 @@ import SrcSvg from '../../../assets/src.svg'; import { DriveSelector } from '../drive-selector/drive-selector'; import { DrivelistDrive } from '../../../../shared/drive-constraints'; import axios, { AxiosRequestConfig } from 'axios'; +import { isJson } from '../../../../shared/utils'; const recentUrlImagesKey = 'recentUrlImages'; @@ -378,7 +379,9 @@ export class SourceSelector extends React.Component< this.setState({ imageLoading: true }); await this.selectSource( imagePath, - isURL(imagePath) ? sourceDestination.Http : sourceDestination.File, + isURL(this.normalizeImagePath(imagePath)) + ? sourceDestination.Http + : sourceDestination.File, ).promise; this.setState({ imageLoading: false }); } @@ -394,7 +397,7 @@ export class SourceSelector extends React.Component< analytics.logException(error); } - if (this.isJson(decodeURIComponent(selected))) { + if (isJson(decodeURIComponent(selected))) { const config: AxiosRequestConfig = JSON.parse( decodeURIComponent(selected), ); @@ -413,13 +416,12 @@ export class SourceSelector extends React.Component< return new sourceDestination.Http({ url: selected, auth }); } - public isJson(jsonString: string) { - try { - JSON.parse(jsonString); - } catch (e) { - return false; + public normalizeImagePath(imgPath: string) { + const decodedPath = decodeURIComponent(imgPath); + if (isJson(decodedPath)) { + return JSON.parse(decodedPath).url ?? decodedPath; } - return true; + return decodedPath; } private reselectSource() { @@ -445,7 +447,10 @@ export class SourceSelector extends React.Component< let source; let metadata: SourceMetadata | undefined; if (isString(selected)) { - if (SourceType === sourceDestination.Http && !isURL(selected)) { + if ( + SourceType === sourceDestination.Http && + !isURL(this.normalizeImagePath(selected)) + ) { this.handleError( 'Unsupported protocol', selected, diff --git a/lib/gui/modules/child-writer.ts b/lib/gui/modules/child-writer.ts index 7a5041c1..b517ba18 100644 --- a/lib/gui/modules/child-writer.ts +++ b/lib/gui/modules/child-writer.ts @@ -35,8 +35,10 @@ import { totalmem } from 'os'; import { toJSON } from '../../shared/errors'; import { GENERAL_ERROR, SUCCESS } from '../../shared/exit-codes'; -import { delay } from '../../shared/utils'; +import { delay, isJson } from '../../shared/utils'; import { SourceMetadata } from '../app/components/source-selector/source-selector'; +import axios from 'axios'; +import * as _ from 'lodash'; ipc.config.id = process.env.IPC_CLIENT_ID as string; ipc.config.socketRoot = process.env.IPC_SOCKET_ROOT as string; @@ -171,6 +173,7 @@ interface WriteOptions { autoBlockmapping: boolean; decompressFirst: boolean; SourceType: string; + httpRequest?: any; } ipc.connectTo(IPC_SERVER_ID, () => { @@ -281,7 +284,17 @@ ipc.connectTo(IPC_SERVER_ID, () => { path: imagePath, }); } else { - source = new Http({ url: imagePath, avoidRandomAccess: true }); + const decodedImagePath = decodeURIComponent(imagePath); + if (isJson(decodedImagePath)) { + const imagePathObject = JSON.parse(decodedImagePath); + source = new Http({ + url: imagePathObject.url, + avoidRandomAccess: true, + axiosInstance: axios.create(_.omit(imagePathObject, ['url'])), + }); + } else { + source = new Http({ url: imagePath, avoidRandomAccess: true }); + } } } const results = await writeAndValidate({ diff --git a/lib/shared/utils.ts b/lib/shared/utils.ts index f312dee5..100672b8 100755 --- a/lib/shared/utils.ts +++ b/lib/shared/utils.ts @@ -61,3 +61,12 @@ export function getAppPath(): string { ) ); } + +export function isJson(jsonString: string) { + try { + JSON.parse(jsonString); + } catch (e) { + return false; + } + return true; +}