A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
mobile: fix attachment downloading
Ammar Ahmed97f135cd075f
diff --git a/apps/mobile/app/common/filesystem/download-attachment.js b/apps/mobile/app/common/filesystem/download-attachment.jsindex 4a18e7e0f..e5b82cdcb 100644--- a/apps/mobile/app/common/filesystem/download-attachment.js+++ b/apps/mobile/app/common/filesystem/download-attachment.js@@ -207,7 +207,11 @@ export default async function downloadAttachment(try {await db.fs()- .downloadFile(options.groupId || attachment.hash, attachment.hash);+ .downloadFile(+ options.groupId || attachment.hash,+ attachment.hash,+ attachment.chunkSize+ );if (!(await exists(attachment.hash))) {DatabaseLogger.log("Attachment does not exist after download.");return;diff --git a/apps/mobile/app/common/filesystem/download.js b/apps/mobile/app/common/filesystem/download.jsindex 024d22b16..4f34a27c7 100644--- a/apps/mobile/app/common/filesystem/download.js+++ b/apps/mobile/app/common/filesystem/download.js@@ -17,24 +17,25 @@ You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/-import hosts from "@notesnook/core/dist/utils/constants";import NetInfo from "@react-native-community/netinfo";import RNFetchBlob from "react-native-blob-util";import { ToastManager } from "../../services/event-manager";import { useAttachmentStore } from "../../stores/use-attachment-store";import { DatabaseLogger, db } from "../database";-import { cacheDir, fileCheck } from "./utils";import { createCacheDir, exists } from "./io";+import { ABYTES, cacheDir, getUploadedFileSize, parseS3Error } from "./utils";-export async function downloadFile(filename, data, cancelToken) {- if (!data) {- DatabaseLogger.log(`Error downloading file: ${filename}, reason: No data`);+export async function downloadFile(filename, requestOptions, cancelToken) {+ if (!requestOptions) {+ DatabaseLogger.log(+ `Error downloading file: ${filename}, reason: No requestOptions`+ );return false;}DatabaseLogger.log(`Downloading ${filename}`);await createCacheDir();- let { url, headers } = data;+ let { url, headers, chunkSize } = requestOptions;let tempFilePath = `${cacheDir}/${filename}_temp`;let originalFilePath = `${cacheDir}/${filename}`;try {@@ -42,6 +43,28 @@ export async function downloadFile(filename, data, cancelToken) {DatabaseLogger.log(`File Exists already: ${filename}`);return true;}+ const attachment = await db.attachments.attachment(filename);+ const size = await getUploadedFileSize(filename);++ if (size === -1) {+ const error = `Uploaded file verification failed. (File hash: ${filename})`;+ throw new Error(error);+ }++ if (size === 0) {+ const error = `File length is 0. Please upload this file again from the attachment manager. (File hash: ${filename})`;+ 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) {+ const error = `File length mismatch. Expected ${attachment.size} but got ${decryptedLength} bytes. Please upload this file again from the attachment manager. (File hash: ${filename})`;+ await db.attachments.markAsFailed(attachment.id, error);+ throw new Error(error);+ }let res = await fetch(url, {method: "GET",@@ -63,18 +86,18 @@ export async function downloadFile(filename, data, cancelToken) {);throw new Error("Unable to resolve download url");}- let totalSize = 0;+DatabaseLogger.log(`Download starting: ${filename}`);let request = RNFetchBlob.config({path: tempFilePath,IOSBackgroundTask: true}).fetch("GET", downloadUrl, null)- .progress((recieved, total) => {+ .progress(async (recieved, total) => {useAttachmentStore.getState().setProgress(0, total, filename, recieved, "download");- totalSize = total;+DatabaseLogger.log(`Downloading: ${filename}, ${recieved}/${total}`);});@@ -86,8 +109,17 @@ export async function downloadFile(filename, data, cancelToken) {};let response = await request;+ console.log(response.info().headers);++ 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}`);+ }- await fileCheck(response, totalSize);let status = response.info().status;useAttachmentStore.getState().remove(filename);await RNFetchBlob.fs.mv(tempFilePath, originalFilePath);@@ -124,24 +156,6 @@ export async function downloadFile(filename, data, cancelToken) {}}-export async function getUploadedFileSize(hash) {- try {- const url = `${hosts.API_HOST}/s3?name=${hash}`;- const token = await db.tokenManager.getAccessToken();- 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;- }-}-export async function checkAttachment(hash) {const internetState = await NetInfo.fetch();const isInternetReachable =diff --git a/apps/mobile/app/common/filesystem/io.js b/apps/mobile/app/common/filesystem/io.jsindex 6a0fa289e..cda03e045 100644--- a/apps/mobile/app/common/filesystem/io.js+++ b/apps/mobile/app/common/filesystem/io.js@@ -23,7 +23,7 @@ import RNFetchBlob from "react-native-blob-util";import { eSendEvent } from "../../services/event-manager";import { IOS_APPGROUPID } from "../../utils/constants";import { DatabaseLogger, db } from "../database";-import { cacheDir, cacheDirOld, getRandomId } from "./utils";+import { ABYTES, cacheDir, cacheDirOld, getRandomId } from "./utils";export async function readEncrypted(filename, key, cipherData) {await migrateFilesFromCache();@@ -182,7 +182,6 @@ export async function deleteDCacheFiles() {}}-const ABYTES = 17;export async function exists(filename) {let path = `${cacheDir}/${filename}`;diff --git a/apps/mobile/app/common/filesystem/upload.js b/apps/mobile/app/common/filesystem/upload.jsindex 54631ebf5..20453a39a 100644--- a/apps/mobile/app/common/filesystem/upload.js+++ b/apps/mobile/app/common/filesystem/upload.js@@ -23,25 +23,24 @@ import { ToastManager } from "../../services/event-manager";import { useAttachmentStore } from "../../stores/use-attachment-store";import { IOS_APPGROUPID } from "../../utils/constants";import { DatabaseLogger, db } from "../database";-import { getUploadedFileSize } from "./download";import { createCacheDir } from "./io";-import { cacheDir } from "./utils";+import { cacheDir, getUploadedFileSize } from "./utils";-export async function uploadFile(filename, data, cancelToken) {- if (!data) return false;- let { url, headers } = data;+export async function uploadFile(filename, requestOptions, cancelToken) {+ if (!requestOptions) return false;+ let { url, headers } = requestOptions;await createCacheDir();DatabaseLogger.info(`Preparing to upload file: ${filename}`);try {const uploadedFileSize = await getUploadedFileSize(filename);+if (uploadedFileSize === -1) {- DatabaseLogger.log("Upload verification failed.");- return false;+ const error = `Uploaded file verification failed. (File hash: ${filename})`;+ throw new Error(error);}- const isUploaded = uploadedFileSize !== 0;- if (isUploaded) {+ if (uploadedFileSize !== 0) {DatabaseLogger.log(`File ${filename} is already uploaded.`);return true;}diff --git a/apps/mobile/app/common/filesystem/utils.js b/apps/mobile/app/common/filesystem/utils.jsindex 921e4278d..5c4fd2761 100644--- a/apps/mobile/app/common/filesystem/utils.js+++ b/apps/mobile/app/common/filesystem/utils.js@@ -17,10 +17,13 @@ You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/-import * as ScopedStorage from "react-native-scoped-storage";import { Platform } from "react-native";import RNFetchBlob from "react-native-blob-util";+import * as ScopedStorage from "react-native-scoped-storage";+import { DatabaseLogger, db } from "../database";+import hosts from "@notesnook/core/dist/utils/constants";+export const ABYTES = 17;export const cacheDirOld = RNFetchBlob.fs.dirs.CacheDir;export const cacheDir =@@ -34,24 +37,36 @@ export function getRandomId(prefix) {.replace("0.", prefix || "");}-export function extractValueFromXmlTag(code, xml) {- if (!xml.includes(code)) return `Unknown ${code}`;- return xml.slice(- xml.indexOf(`<${code}>`) + code.length + 2,- xml.indexOf(`</${code}>`)- );-}+/**+ *+ * @param {string | undefined} data+ * @returns+ */+export function parseS3Error(data) {+ const xml = typeof data === "string" ? data : null;++ const error = {+ Code: "UNKNOWN",+ Message: xml+ };++ try {+ if (!xml) return error;+ const doc = new DOMParser().parseFromString(xml, "text/xml");-export async function fileCheck(response, totalSize) {- if (totalSize < 1000) {- let text = await response.text();- if (text.startsWith("<?xml")) {- let errorJson = {- Code: extractValueFromXmlTag("Code", text),- Message: extractValueFromXmlTag("Message", text)- };- throw new Error(`${errorJson.Code}: ${errorJson.Message}`);+ const ErrorElement = doc.getElementsByTagName("Error")[0];+ if (!ErrorElement) return error;++ for (const child of ErrorElement.children) {+ if (+ child.textContent &&+ (child.tagName === "Code" || child.tagName === "Message")+ )+ error[child.tagName] = child.textContent;}+ return error;+ } catch (e) {+ return error;}}@@ -59,9 +74,9 @@ export function cancelable(operation) {const cancelToken = {cancel: () => {}};- return (filename, { url, headers }) => {+ return (filename, requestOptions) => {return {- execute: () => operation(filename, { url, headers }, cancelToken),+ execute: () => operation(filename, requestOptions, cancelToken),cancel: async () => {await cancelToken.cancel();}@@ -90,3 +105,21 @@ export async function releasePermissions(path) {}}}++export async function getUploadedFileSize(hash) {+ try {+ const url = `${hosts.API_HOST}/s3?name=${hash}`;+ const token = await db.tokenManager.getAccessToken();+ 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;+ }+}