Files
notesnook/apps/mobile/app/common/filesystem/download.js

154 lines
4.7 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
import hosts from "@notesnook/core/dist/utils/constants";
import NetInfo from "@react-native-community/netinfo";
2023-05-19 11:05:16 +05:00
import RNFetchBlob from "react-native-blob-util";
2023-08-29 20:42:45 +05:00
import { ToastManager } from "../../services/event-manager";
2022-08-29 16:19:17 +05:00
import { useAttachmentStore } from "../../stores/use-attachment-store";
import { DatabaseLogger, db } from "../database";
import { cacheDir, fileCheck } from "./utils";
import { createCacheDir, exists } from "./io";
2022-02-28 13:48:59 +05:00
export async function downloadFile(filename, data, cancelToken) {
2024-05-04 00:31:02 +05:00
if (!data) {
DatabaseLogger.log(`Error downloading file: ${filename}, reason: No data`);
return false;
}
2024-05-04 00:31:02 +05:00
DatabaseLogger.log(`Downloading ${filename}`);
await createCacheDir();
2022-02-28 13:48:59 +05:00
let { url, headers } = data;
let path = `${cacheDir}/${filename}`;
2022-02-28 13:48:59 +05:00
try {
if (await exists(filename)) {
2024-05-04 00:31:02 +05:00
DatabaseLogger.log(`File Exists already: ${filename}`);
2022-02-28 13:48:59 +05:00
return true;
}
let res = await fetch(url, {
method: "GET",
2022-02-28 13:48:59 +05:00
headers
});
2024-05-04 00:31:02 +05:00
if (!res.ok) {
DatabaseLogger.log(
`Error downloading file: ${filename}, ${res.status}, ${res.statusText}, reason: Unable to resolve download url`
);
throw new Error(`${res.status}: Unable to resolve download url`);
2024-05-04 00:31:02 +05:00
}
2022-02-28 13:48:59 +05:00
const downloadUrl = await res.text();
2024-05-04 00:31:02 +05:00
if (!downloadUrl) {
DatabaseLogger.log(
`Error downloading file: ${filename}, reason: Unable to resolve download url`
);
throw new Error("Unable to resolve download url");
}
2022-02-28 13:48:59 +05:00
let totalSize = 0;
2024-05-04 00:31:02 +05:00
DatabaseLogger.log(`Download starting: ${filename}`);
2022-02-28 13:48:59 +05:00
let request = RNFetchBlob.config({
path: path,
IOSBackgroundTask: true
})
.fetch("GET", downloadUrl, null)
2022-02-28 13:48:59 +05:00
.progress((recieved, total) => {
useAttachmentStore
.getState()
.setProgress(0, total, filename, recieved, "download");
2022-02-28 13:48:59 +05:00
totalSize = total;
2024-05-04 00:31:02 +05:00
DatabaseLogger.log(`Downloading: ${filename}, ${recieved}/${total}`);
2022-02-28 13:48:59 +05:00
});
cancelToken.cancel = () => {
useAttachmentStore.getState().remove(filename);
request.cancel();
2024-05-04 00:31:02 +05:00
DatabaseLogger.log(`Download cancelled: ${filename}`);
};
2024-05-04 00:31:02 +05:00
2022-02-28 13:48:59 +05:00
let response = await request;
await fileCheck(response, totalSize);
let status = response.info().status;
useAttachmentStore.getState().remove(filename);
return status >= 200 && status < 300;
} catch (e) {
if (e.message !== "canceled" && !e.message.includes("NoSuchKey")) {
2023-08-29 20:42:45 +05:00
ToastManager.show({
2023-09-20 19:45:57 +05:00
heading: "Error downloading file",
message: e.message,
type: "error",
context: "global"
});
2023-08-29 20:42:45 +05:00
ToastManager.show({
2023-09-20 19:45:57 +05:00
heading: "Error downloading file",
message: e.message,
type: "error",
context: "local"
});
}
2022-02-28 13:48:59 +05:00
useAttachmentStore.getState().remove(filename);
RNFetchBlob.fs.unlink(path).catch(console.log);
2024-05-04 00:31:02 +05:00
DatabaseLogger.error(e, {
url,
headers
});
2022-02-28 13:48:59 +05:00
return false;
}
}
2022-03-07 15:19:07 +05:00
export async function getUploadedFileSize(hash) {
2023-11-18 11:48:14 +05:00
try {
const url = `${hosts.API_HOST}/s3?name=${hash}`;
2023-08-21 16:24:30 +05:00
const token = await db.tokenManager.getAccessToken();
2023-11-18 11:48:14 +05:00
const attachmentInfo = await fetch(url, {
method: "HEAD",
headers: { Authorization: `Bearer ${token}` }
});
const contentLength = parseInt(
attachmentInfo.headers?.get("content-length")
);
return isNaN(contentLength) ? 0 : contentLength;
} catch (e) {
DatabaseLogger.error(e);
return -1;
2023-11-18 11:48:14 +05:00
}
2022-03-07 15:19:07 +05:00
}
export async function checkAttachment(hash) {
const internetState = await NetInfo.fetch();
const isInternetReachable =
internetState.isConnected && internetState.isInternetReachable;
if (!isInternetReachable) return { success: true };
2023-12-27 09:40:15 +05:00
const attachment = await db.attachments.attachment(hash);
if (!attachment) return { failed: "Attachment not found." };
2022-03-07 15:19:07 +05:00
try {
const size = await getUploadedFileSize(hash);
if (size === -1) return { success: true };
if (size === 0) return { failed: "File length is 0." };
2022-03-07 15:19:07 +05:00
} catch (e) {
return { failed: e?.message };
2022-03-07 15:19:07 +05:00
}
return { success: true };
}