expo-downloads-manager
Version:
download manager based on expo react native
64 lines • 1.93 kB
JavaScript
import * as FileSystem from "expo-file-system";
import { Platform } from "react-native";
import * as Sharing from "expo-sharing";
import { startActivityAsync } from "expo-intent-launcher";
const ios = Platform.OS === "ios";
export async function downloadFileFromUri(uri, fileName, downloadProgressCallback) {
const fileUri = `${FileSystem.documentDirectory}${fileName}`;
let status = "downloading";
let error = null;
try {
if (downloadProgressCallback) {
const downloadResumable = FileSystem.createDownloadResumable(uri, fileUri, {}, downloadProgressCallback);
await downloadResumable.downloadAsync();
status = "finished";
return {
status,
};
}
else {
await FileSystem.downloadAsync(uri, fileUri);
status = "finished";
return {
status,
};
}
}
catch (e) {
console.log(`ERROR: ${e}`);
status = "error";
error = e;
return { status, error };
}
}
export async function openDownloadedFile(fileName) {
const fileUri = `${FileSystem.documentDirectory}${fileName}`;
if (ios) {
const UTI = "public.item";
await Sharing.shareAsync(fileUri, {
UTI,
});
}
else {
const cUri = await FileSystem.getContentUriAsync(fileUri);
startActivityAsync("android.intent.action.VIEW", {
data: cUri,
flags: 1,
});
}
}
export async function checkFileIsAvailable(fileName) {
const fileUri = `${FileSystem.documentDirectory}${fileName}`;
let isAvailable = false;
const fileInfo = await FileSystem.getInfoAsync(fileUri);
if (fileInfo?.exists) {
isAvailable = true;
return {
isAvailable,
};
}
return {
isAvailable,
};
}
//# sourceMappingURL=index.js.map