Войдите в систему при первой загрузке (кеш пуст):

Вторая загрузка (то же самое для любого файла)

Код
import * as FileSystem from "expo-file-system/legacy";
import { Paths, File } from "expo-file-system";
const handleDownload = async (item, subject) => {
const startTotal = Date.now();
dispatch(setNote({ name: "loading", value: true }));
let cacheFile = null;
try {
const url = item?.noteUrl;
if (!item || !url) {
Alert.alert("Error", "File URL not found.");
return;
}
//
let name = item.name || "file";
let noteType = item.noteType;
let ext = getExtensionName(url) || "pdf";
name = name
.replace(/[/\\?%*:|".]/g, "_")
.trim()
.replace(/\s+/g, "_");
const filename = `${name}.${ext}`;
const subjectName = subject.name;
console.log(`
//
const startDownload = Date.now();
//
const response = await fetch(url);
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`);
console.log("Response:");
console.log(response);
cacheFile = new File(Paths.cache, filename);
await cacheFile.write(await response.bytes());
const cacheUri = cacheFile.uri;
const endDownload = Date.now();
console.log(
`
2
)}s`
);
//
if (Platform.OS === "android") {
console.log("
//
const startPicker = Date.now();
const graderUri = await getFolderUri(["Notes", subjectName]);
const endPicker = Date.now();
console.log(
`
2
)}s`
);
console.log(`
//
const startCreate = Date.now();
const newUri = await FileSystem.StorageAccessFramework.createFileAsync(
graderUri,
filename,
noteType || "application/pdf"
);
const endCreate = Date.now();
console.log(
`
2
)}s`
);
//
const startRead = Date.now();
const cacheFileBytes = await cacheFile.bytes();
const endRead = Date.now();
console.log(
`
2
)}s`
);
//
const startWrite = Date.now();
const file = new File(newUri);
file.write(cacheFileBytes);
const endWrite = Date.now();
console.log(
`
(endWrite - startWrite) /
1000
).toFixed(2)}s`
);
Alert.alert("Success
} else {
//
if (await Sharing.isAvailableAsync()) {
await Sharing.shareAsync(cacheUri, {
mimeType: noteType || "application/pdf",
});
} else {
Alert.alert("Downloaded
}
}
} catch (error) {
console.error("
Alert.alert("Error
} finally {
//
if (cacheFile) {
try {
const startDelete = Date.now();
await cacheFile.delete();
const endDelete = Date.now();
console.log(
`
(endDelete - startDelete) /
1000
).toFixed(2)}s`
);
} catch (e) {
console.warn("
}
}
dispatch(setNote({ name: "loading", value: false }));
const endTotal = Date.now();
console.log(
`
);
}
};
Подробнее здесь: https://stackoverflow.com/questions/797 ... expo-fetch
Мобильная версия