-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
se agrego el metodo downloadFile #8
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
import {isString, URLMaker, parsePathParams} from './utils/helpers.js'; | ||
import RNFS from 'react-native-fs'; | ||
import { | ||
isString, | ||
URLMaker, | ||
parsePathParams, | ||
promiseWrapper, | ||
isObject, | ||
isFunction, | ||
isNumber, | ||
} from './utils/helpers.js'; | ||
import crashlytics from './utils/crashlytics.js'; | ||
import updateHeaders from './utils/updateHeaders.js'; | ||
import parseQueryParams from './utils/parseQueryParams.js'; | ||
|
@@ -381,6 +390,91 @@ class Request { | |
return Promise.reject(formattedError); | ||
} | ||
} | ||
|
||
/** | ||
* @name downloadFile | ||
* @description Download a file and save it in the phone using react-native-fs package | ||
* @param {string} fileName - File name for the document with its extension | .zip .pdf etc | ||
* @param {string} folderPath - The path for folder where the document is stored | ||
* @param {string} url - url where file is stored | ||
* @param {object} headers - An object of headers to be passed to the server | ||
* @param {function} begin - callback it is invoked once at the beginning of the process with the response headers and download data | ||
* @param {function} progress - callback that receives the download trace | ||
* @param {number} progressInterval - it will return progress events in the maximum frequency of progressInterval | ||
* @param {number} progressDivider - it will return progress events that divided by progressDivider | ||
* @returns {Promise} Response from package | {jobId: number, statusCode: number, bytesWritten: number} | ||
* @example | ||
* const request = new Request({JANIS_ENV: 'janis_dev'}) | ||
* const pdfFile = await request.downloadFile({ | ||
url: `https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf`, | ||
fileName: `dummy.pdf`, | ||
folderPath: `${RNFS.DocumentDirectoryPath}/pdfs`, | ||
progress: ({jobId, contentLength, bytesWritten }) => {}, | ||
progressDivider: 10 | ||
}) | ||
*/ | ||
async downloadFile({ | ||
fileName, | ||
folderPath, | ||
url, | ||
headers, | ||
begin, | ||
progress, | ||
progressInterval, | ||
progressDivider, | ||
...props | ||
}) { | ||
try { | ||
if (!fileName || !isString(fileName)) throw new Error('fileName is not valid'); | ||
if (!folderPath || !isString(folderPath)) throw new Error('folderPath is not valid'); | ||
if (!url || !isString(url)) throw new Error('url is not valid'); | ||
|
||
const validHeaders = headers && isObject(headers); | ||
const validBegin = progress && isFunction(begin); | ||
const validProgress = progress && isFunction(progress); | ||
const validProgressInterval = progressInterval && isNumber(progressInterval); | ||
const validProgressDivider = progressDivider && isNumber(progressDivider); | ||
|
||
const [downloadRes, downloadErr] = await promiseWrapper( | ||
RNFS.downloadFile({ | ||
fromUrl: url, | ||
toFile: `${folderPath}/${fileName}`, | ||
...(validHeaders && {headers}), | ||
...(validBegin && {begin}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @WilliamSaya-lvl30 acá no se tendrían que usar los mismos headers que en las demás request? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GonzaFran para lo que se utiliza no suele requerir headers, se agrego esa validación solo para cubrir hipoteticos casos futuros. |
||
...(validProgress && {progress}), | ||
...(validProgressInterval && {progressInterval}), | ||
...(validProgressDivider && {progressDivider}), | ||
...{...props}, | ||
}).promise, | ||
); | ||
|
||
if (downloadErr) throw new Error(downloadErr.message); | ||
if ( | ||
!isObject(downloadRes) || | ||
!downloadRes || | ||
downloadRes.bytesWritten === 0 || | ||
downloadRes.statusCode === 403 | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @WilliamSaya-lvl30 esta validación no convendría que verfique si el statuscode es diferente a 200? |
||
throw new Error('The file download did not complete'); | ||
|
||
return { | ||
result: downloadRes, | ||
}; | ||
} catch (error) { | ||
const formattedError = { | ||
result: { | ||
message: error.message, | ||
}, | ||
}; | ||
|
||
crashlytics.recordError( | ||
error, | ||
`Error at downloadAndFile: url: ${url} reason: ${error.message}`, | ||
); | ||
|
||
return Promise.reject(formattedError); | ||
} | ||
} | ||
} | ||
|
||
export default Request; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WilliamSaya-lvl30 revisate que acá estas validando progress cuando debería ser
begin && isFunction(begin)