2022-08-26 16:19:39 +05:00
|
|
|
import RNFetchBlob from "rn-fetch-blob";
|
2022-02-28 13:48:59 +05:00
|
|
|
|
|
|
|
|
export const cacheDir = RNFetchBlob.fs.dirs.CacheDir;
|
|
|
|
|
|
|
|
|
|
export function getRandomId(prefix) {
|
|
|
|
|
return Math.random()
|
|
|
|
|
.toString(36)
|
2022-08-26 16:19:39 +05:00
|
|
|
.replace("0.", prefix || "");
|
2022-02-28 13:48:59 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function extractValueFromXmlTag(code, xml) {
|
|
|
|
|
if (!xml.includes(code)) return `Unknown ${code}`;
|
2022-08-26 16:19:39 +05:00
|
|
|
return xml.slice(
|
|
|
|
|
xml.indexOf(`<${code}>`) + code.length + 2,
|
|
|
|
|
xml.indexOf(`</${code}>`)
|
|
|
|
|
);
|
2022-02-28 13:48:59 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fileCheck(response, totalSize) {
|
|
|
|
|
if (totalSize < 1000) {
|
|
|
|
|
let text = await response.text();
|
2022-08-26 16:19:39 +05:00
|
|
|
if (text.startsWith("<?xml")) {
|
2022-02-28 13:48:59 +05:00
|
|
|
let errorJson = {
|
2022-08-26 16:19:39 +05:00
|
|
|
Code: extractValueFromXmlTag("Code", text),
|
|
|
|
|
Message: extractValueFromXmlTag("Message", text)
|
2022-02-28 13:48:59 +05:00
|
|
|
};
|
|
|
|
|
throw new Error(`${errorJson.Code}: ${errorJson.Message}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function cancelable(operation) {
|
|
|
|
|
const cancelToken = {
|
|
|
|
|
cancel: () => {}
|
|
|
|
|
};
|
|
|
|
|
return (filename, { url, headers }) => {
|
|
|
|
|
return {
|
|
|
|
|
execute: () => operation(filename, { url, headers }, cancelToken),
|
|
|
|
|
cancel: async () => {
|
|
|
|
|
await cancelToken.cancel();
|
|
|
|
|
RNFetchBlob.fs.unlink(`${cacheDir}/${filename}`).catch(console.log);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|