-
-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import isPromise from './utils/isPromise'; | ||
|
||
/** | ||
* Module for file downloading. Handle case, when you have to use custom downloading method | ||
*/ | ||
export default class Downloader { | ||
/** | ||
* @param {object} params - downloader module params | ||
* @param {ImageConfig} params.config - image tool config | ||
* @param {Function} params.onDownload - callback which is called, when file is downloaded | ||
* @param {Function} params.onError - callback for downloading errors | ||
*/ | ||
constructor({ config, onDownload, onError }) { | ||
this.config = config; | ||
this.onDownload = onDownload | ||
this.onError = onError; | ||
} | ||
|
||
/** | ||
* Try to download file data and fill it using stored data | ||
* | ||
* @param {string} fileData - may be some key for custom downloading or url | ||
*/ | ||
download(fileData) { | ||
/** | ||
* Check that custom downloader passed | ||
*/ | ||
if (this.config.downloader && typeof this.config.downloader.download === 'function') { | ||
const downloadData = this.config.downloader.download(fileData); | ||
|
||
if (!isPromise(downloadData)) { | ||
console.warn('Custom downloader method download should return a Promise'); | ||
} | ||
|
||
downloadData.then((response) => { | ||
/** | ||
* Call callback for successful downloading with url | ||
*/ | ||
this.onDownload(response); | ||
}).catch((error) => { | ||
this.onError(error); | ||
}); | ||
} else { | ||
/** | ||
* If there is no custom download method, fileData is correct url | ||
* We only need to call callback | ||
*/ | ||
this.onDownload(fileData); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters