2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
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
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-08-30 09:59:53 +05:00
|
|
|
import hosts from "@notesnook/core/utils/constants";
|
2022-09-09 19:09:22 +05:00
|
|
|
import NetInfo from "@react-native-community/netinfo";
|
2022-08-26 16:19:39 +05:00
|
|
|
import RNFetchBlob from "rn-fetch-blob";
|
2022-09-09 19:09:22 +05:00
|
|
|
import { ToastEvent } from "../../services/event-manager";
|
2022-08-29 16:19:17 +05:00
|
|
|
import { useAttachmentStore } from "../../stores/use-attachment-store";
|
2022-08-26 16:19:39 +05:00
|
|
|
import { db } from "../database";
|
|
|
|
|
import { cacheDir, fileCheck } from "./utils";
|
2022-02-28 13:48:59 +05:00
|
|
|
|
|
|
|
|
export async function downloadFile(filename, data, cancelToken) {
|
|
|
|
|
if (!data) return false;
|
|
|
|
|
let { url, headers } = data;
|
|
|
|
|
|
|
|
|
|
let path = `${cacheDir}/${filename}`;
|
|
|
|
|
try {
|
|
|
|
|
let exists = await RNFetchBlob.fs.exists(path);
|
|
|
|
|
if (exists) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let res = await fetch(url, {
|
2022-08-26 16:19:39 +05:00
|
|
|
method: "GET",
|
2022-02-28 13:48:59 +05:00
|
|
|
headers
|
|
|
|
|
});
|
2022-08-26 16:19:39 +05:00
|
|
|
if (!res.ok)
|
|
|
|
|
throw new Error(`${res.status}: Unable to resolve download url`);
|
2022-02-28 13:48:59 +05:00
|
|
|
const downloadUrl = await res.text();
|
|
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
if (!downloadUrl) throw new Error("Unable to resolve download url");
|
2022-02-28 13:48:59 +05:00
|
|
|
let totalSize = 0;
|
|
|
|
|
let request = RNFetchBlob.config({
|
|
|
|
|
path: path,
|
|
|
|
|
IOSBackgroundTask: true
|
|
|
|
|
})
|
2022-08-26 16:19:39 +05:00
|
|
|
.fetch("GET", downloadUrl, null)
|
2022-02-28 13:48:59 +05:00
|
|
|
.progress((recieved, total) => {
|
2022-08-26 16:19:39 +05:00
|
|
|
useAttachmentStore
|
|
|
|
|
.getState()
|
|
|
|
|
.setProgress(0, total, filename, recieved, "download");
|
2022-02-28 13:48:59 +05:00
|
|
|
totalSize = total;
|
2022-08-26 16:19:39 +05:00
|
|
|
console.log("downloading: ", recieved, total);
|
2022-02-28 13:48:59 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cancelToken.cancel = request.cancel;
|
|
|
|
|
let response = await request;
|
|
|
|
|
await fileCheck(response, totalSize);
|
|
|
|
|
let status = response.info().status;
|
|
|
|
|
useAttachmentStore.getState().remove(filename);
|
|
|
|
|
return status >= 200 && status < 300;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ToastEvent.show({
|
2022-08-26 16:19:39 +05:00
|
|
|
heading: "Error downloading file",
|
2022-02-28 13:48:59 +05:00
|
|
|
message: e.message,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "error",
|
|
|
|
|
context: "global"
|
2022-03-07 15:19:07 +05:00
|
|
|
});
|
|
|
|
|
ToastEvent.show({
|
2022-08-26 16:19:39 +05:00
|
|
|
heading: "Error downloading file",
|
2022-03-07 15:19:07 +05:00
|
|
|
message: e.message,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "error",
|
|
|
|
|
context: "local"
|
2022-02-28 13:48:59 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useAttachmentStore.getState().remove(filename);
|
|
|
|
|
RNFetchBlob.fs.unlink(path).catch(console.log);
|
2022-08-26 16:19:39 +05:00
|
|
|
console.log("download file 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) {
|
|
|
|
|
const url = `${hosts.API_HOST}/s3?name=${hash}`;
|
|
|
|
|
const token = await db.user.tokenManager.getAccessToken();
|
|
|
|
|
|
|
|
|
|
const attachmentInfo = await fetch(url, {
|
2022-08-26 16:19:39 +05:00
|
|
|
method: "HEAD",
|
2022-03-07 15:19:07 +05:00
|
|
|
headers: { Authorization: `Bearer ${token}` }
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
const contentLength = parseInt(attachmentInfo.headers?.get("content-length"));
|
2022-03-07 15:19:07 +05:00
|
|
|
return isNaN(contentLength) ? 0 : contentLength;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function checkAttachment(hash) {
|
2022-04-19 16:35:11 +05:00
|
|
|
const internetState = await NetInfo.fetch();
|
2022-08-26 16:19:39 +05:00
|
|
|
const isInternetReachable =
|
|
|
|
|
internetState.isConnected && internetState.isInternetReachable;
|
2022-04-19 16:35:11 +05:00
|
|
|
if (!isInternetReachable) return { success: true };
|
2022-03-07 15:19:07 +05:00
|
|
|
const attachment = db.attachments.attachment(hash);
|
2022-08-26 16:19:39 +05:00
|
|
|
if (!attachment) return { failed: "Attachment not found." };
|
2022-03-07 15:19:07 +05:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const size = await getUploadedFileSize(hash);
|
2022-08-26 16:19:39 +05:00
|
|
|
if (size <= 0) return { failed: "File length is 0." };
|
2022-03-07 15:19:07 +05:00
|
|
|
} catch (e) {
|
2022-04-19 16:35:11 +05:00
|
|
|
return { failed: e?.message };
|
2022-03-07 15:19:07 +05:00
|
|
|
}
|
|
|
|
|
return { success: true };
|
|
|
|
|
}
|