From 64d10f5050eb1c58e06fa49802db8b939c8f3edc Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Sat, 18 Nov 2023 11:48:14 +0500 Subject: [PATCH] mobile: reupload if file size is zero --- apps/mobile/app/common/filesystem/download.js | 22 ++++++++++++------- .../app/screens/editor/tiptap/picker.js | 5 +++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/apps/mobile/app/common/filesystem/download.js b/apps/mobile/app/common/filesystem/download.js index 7d92bb27d..5623eae50 100644 --- a/apps/mobile/app/common/filesystem/download.js +++ b/apps/mobile/app/common/filesystem/download.js @@ -96,16 +96,22 @@ export async function downloadFile(filename, data, cancelToken) { } export async function getUploadedFileSize(hash) { - const url = `${hosts.API_HOST}/s3?name=${hash}`; - const token = await db.user.tokenManager.getAccessToken(); + try { + const url = `${hosts.API_HOST}/s3?name=${hash}`; + const token = await db.user.tokenManager.getAccessToken(); - const attachmentInfo = await fetch(url, { - method: "HEAD", - headers: { Authorization: `Bearer ${token}` } - }); + const attachmentInfo = await fetch(url, { + method: "HEAD", + headers: { Authorization: `Bearer ${token}` } + }); - const contentLength = parseInt(attachmentInfo.headers?.get("content-length")); - return isNaN(contentLength) ? 0 : contentLength; + const contentLength = parseInt( + attachmentInfo.headers?.get("content-length") + ); + return isNaN(contentLength) ? 0 : contentLength; + } catch (e) { + return 0; + } } export async function checkAttachment(hash) { diff --git a/apps/mobile/app/screens/editor/tiptap/picker.js b/apps/mobile/app/screens/editor/tiptap/picker.js index 07f1e2302..a532f07cc 100644 --- a/apps/mobile/app/screens/editor/tiptap/picker.js +++ b/apps/mobile/app/screens/editor/tiptap/picker.js @@ -35,6 +35,7 @@ import { FILE_SIZE_LIMIT, IMAGE_SIZE_LIMIT } from "../../../utils/constants"; import { eCloseSheet } from "../../../utils/events"; import { editorController, editorState } from "./utils"; import { useSettingStore } from "../../../stores/use-setting-store"; +import filesystem from "../../../common/filesystem"; const showEncryptionSheet = (file) => { presentSheet({ @@ -270,6 +271,10 @@ export async function attachFile(uri, hash, type, filename, options) { return false; } + if (!options.reupload && exists) { + options.reupload = (await filesystem.getUploadedFileSize(hash)) <= 0; + } + if (!exists || options?.reupload) { let key = await db.attachments.generateKey(); encryptionInfo = await Sodium.encryptFile(key, {