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

211 lines
6.4 KiB
TypeScript
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
2024-12-13 14:07:15 +05:00
import { RequestOptions } from "@notesnook/core";
2024-07-27 10:19:43 +05:00
import { strings } from "@notesnook/intl";
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 { createCacheDir, exists } from "./io";
2024-08-08 12:19:21 +05:00
import { ABYTES, cacheDir, getUploadedFileSize, parseS3Error } from "./utils";
2022-02-28 13:48:59 +05:00
2024-12-13 14:07:15 +05:00
export async function downloadFile(
filename: string,
requestOptions: RequestOptions,
cancelToken: {
cancel: (reason?: string) => Promise<void>;
}
) {
2024-08-08 12:19:21 +05:00
if (!requestOptions) {
DatabaseLogger.log(
`Error downloading file: ${filename}, reason: No requestOptions`
);
2024-05-04 00:31:02 +05:00
return false;
}
2024-05-04 00:31:02 +05:00
DatabaseLogger.log(`Downloading ${filename}`);
await createCacheDir();
2024-12-13 14:07:15 +05:00
const { url, headers, chunkSize } = requestOptions;
const tempFilePath = `${cacheDir}/${filename}_temp`;
const originalFilePath = `${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;
}
2024-08-08 12:19:21 +05:00
const attachment = await db.attachments.attachment(filename);
2024-12-13 14:07:15 +05:00
if (!attachment) return false;
2024-08-08 12:19:21 +05:00
const size = await getUploadedFileSize(filename);
if (size === -1) {
2024-07-27 10:19:43 +05:00
const error = `${strings.fileVerificationFailed()} (File hash: ${filename})`;
2024-08-08 12:19:21 +05:00
throw new Error(error);
}
if (size === 0) {
2024-07-27 10:19:43 +05:00
const error = `${strings.fileLengthError()} (File hash: ${filename})`;
2024-08-08 12:19:21 +05:00
await db.attachments.markAsFailed(attachment.id, error);
throw new Error(error);
}
const totalChunks = Math.ceil(size / chunkSize);
const decryptedLength = size - totalChunks * ABYTES;
if (attachment && attachment.size !== decryptedLength) {
2024-07-27 10:19:43 +05:00
const error = `${strings.fileLengthMismatch(
attachment.size,
decryptedLength
)} (File hash: ${filename})`;
2024-08-08 12:19:21 +05:00
await db.attachments.markAsFailed(attachment.id, error);
throw new Error(error);
}
2022-02-28 13:48:59 +05:00
2024-12-13 14:07:15 +05:00
const resolveUrlResponse = await fetch(url, {
method: "GET",
2022-02-28 13:48:59 +05:00
headers
});
2024-05-04 00:31:02 +05:00
2024-12-13 14:07:15 +05:00
if (!resolveUrlResponse.ok) {
2024-05-04 00:31:02 +05:00
DatabaseLogger.log(
2024-12-13 14:07:15 +05:00
`Error downloading file: ${filename}, ${resolveUrlResponse.status}, ${resolveUrlResponse.statusText}, reason: Unable to resolve download url`
2024-05-04 00:31:02 +05:00
);
2024-07-27 10:19:43 +05:00
throw new Error(
2024-12-13 14:07:15 +05:00
`${resolveUrlResponse.status}: ${strings.failedToResolvedDownloadUrl()}`
2024-07-27 10:19:43 +05:00
);
2024-05-04 00:31:02 +05:00
}
2024-12-13 14:07:15 +05:00
const downloadUrl = await resolveUrlResponse.text();
2022-02-28 13:48:59 +05:00
2024-05-04 00:31:02 +05:00
if (!downloadUrl) {
DatabaseLogger.log(
`Error downloading file: ${filename}, reason: Unable to resolve download url`
);
2024-07-27 10:19:43 +05:00
throw new Error(strings.failedToResolvedDownloadUrl());
2024-05-04 00:31:02 +05:00
}
2024-08-08 12:19:21 +05:00
2024-05-04 00:31:02 +05:00
DatabaseLogger.log(`Download starting: ${filename}`);
2024-12-13 14:07:15 +05:00
const request = RNFetchBlob.config({
path: tempFilePath,
IOSBackgroundTask: true,
overwrite: true
2022-02-28 13:48:59 +05:00
})
2024-12-13 14:07:15 +05:00
.fetch("GET", downloadUrl)
2024-08-08 12:19:21 +05:00
.progress(async (recieved, total) => {
useAttachmentStore
.getState()
.setProgress(0, total, filename, recieved, "download");
2024-08-08 12:19:21 +05:00
2024-05-04 00:31:02 +05:00
DatabaseLogger.log(`Downloading: ${filename}, ${recieved}/${total}`);
2022-02-28 13:48:59 +05:00
});
2024-12-13 14:07:15 +05:00
cancelToken.cancel = async (reason) => {
useAttachmentStore.getState().remove(filename);
request.cancel();
RNFetchBlob.fs.unlink(tempFilePath).catch(() => {
/* empty */
});
2024-12-13 14:07:15 +05:00
DatabaseLogger.log(`Download cancelled: ${reason} ${filename}`);
};
2024-05-04 00:31:02 +05:00
2024-12-13 14:07:15 +05:00
const response = await request;
2024-08-08 12:19:21 +05:00
const contentType =
response.info().headers?.["content-type"] ||
response.info().headers?.["Content-Type"];
if (contentType === "application/xml") {
const error = parseS3Error(await response.text());
throw new Error(`[${error.Code}] ${error.Message}`);
}
2024-12-13 14:07:15 +05:00
const status = response.info().status;
2022-02-28 13:48:59 +05:00
useAttachmentStore.getState().remove(filename);
2024-12-13 14:07:15 +05:00
if (await exists(originalFilePath)) {
await RNFetchBlob.fs.unlink(originalFilePath).catch(() => {
/* empty */
});
}
await RNFetchBlob.fs.mv(tempFilePath, originalFilePath);
if (!(await exists(filename))) {
throw new Error("File size mismatch");
}
2022-02-28 13:48:59 +05:00
return status >= 200 && status < 300;
} catch (e) {
2024-12-13 14:07:15 +05:00
if (
(e as Error).message !== "canceled" &&
!(e as Error).message.includes("NoSuchKey")
) {
2024-07-27 10:19:43 +05:00
const toast = {
2024-12-13 14:07:15 +05:00
heading: strings.downloadError((e as Error).message),
message: (e as Error).message,
type: "error" as const,
2023-09-20 19:45:57 +05:00
context: "global"
2024-07-27 10:19:43 +05:00
};
ToastManager.show(toast);
toast.context = "local";
ToastManager.show(toast);
2023-09-20 19:45:57 +05:00
}
2022-02-28 13:48:59 +05:00
useAttachmentStore.getState().remove(filename);
RNFetchBlob.fs.unlink(tempFilePath).catch(() => {
/* empty */
});
RNFetchBlob.fs.unlink(originalFilePath).catch(() => {
/* empty */
});
2024-12-13 14:07:15 +05:00
DatabaseLogger.error(e, "Download failed: ", {
url
2024-05-04 00:31:02 +05:00
});
2022-02-28 13:48:59 +05:00
return false;
}
}
2024-12-13 14:07:15 +05:00
export async function checkAttachment(hash: string) {
const internetState = await NetInfo.fetch();
const isInternetReachable =
internetState.isConnected && internetState.isInternetReachable;
2024-08-16 16:29:05 +05:00
if (!isInternetReachable) return;
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 };
2024-08-16 16:29:05 +05:00
if (size === 0)
return {
failed: `File length is 0. Please upload this file again from the attachment manager. (File hash: ${hash})`
};
2022-03-07 15:19:07 +05:00
} catch (e) {
2024-12-13 14:07:15 +05:00
return { failed: (e as Error)?.message };
2022-03-07 15:19:07 +05:00
}
return { success: true };
}