fix: files download in mobile webview (#1726)

This commit is contained in:
Aman Harwara
2022-09-30 22:31:49 +05:30
committed by GitHub
parent 86a0b61612
commit b15d80eb29
3 changed files with 31 additions and 9 deletions

View File

@@ -20,6 +20,7 @@ import { action, makeObservable, observable, reaction } from 'mobx'
import { WebApplication } from '../Application/Application'
import { AbstractViewController } from './Abstract/AbstractViewController'
import { NotesController } from './NotesController'
import { downloadOrShareBlobBasedOnPlatform } from '@/Utils/DownloadOrShareBasedOnPlatform'
const UnprotectedFileActions = [PopoverFileItemActionType.ToggleFileProtection]
const NonMutatingFileActions = [PopoverFileItemActionType.DownloadFile, PopoverFileItemActionType.PreviewFile]
@@ -267,7 +268,10 @@ export class FilesController extends AbstractViewController {
await saver.finish()
} else {
const finalBytes = concatenateUint8Arrays(decryptedBytesArray)
saver.saveFile(file.name, finalBytes)
const blob = new Blob([finalBytes], {
type: file.mimeType,
})
await downloadOrShareBlobBasedOnPlatform(this.application, blob, file.name, false)
}
addToast({

View File

@@ -3,18 +3,31 @@ import { getBase64FromBlob } from '@/Utils'
import { Platform } from '@standardnotes/snjs'
import { addToast, ToastType, dismissToast } from '@standardnotes/toast'
export const downloadBlobOnAndroid = async (application: WebApplication, blob: Blob, filename: string) => {
export const downloadBlobOnAndroid = async (
application: WebApplication,
blob: Blob,
filename: string,
showToast = true,
) => {
if (!application.isNativeMobileWeb() || application.platform !== Platform.Android) {
throw new Error('Download function being used on non-android platform')
}
const loadingToastId = addToast({
type: ToastType.Loading,
message: `Downloading ${filename}..`,
})
let loadingToastId: string | undefined
if (showToast) {
loadingToastId = addToast({
type: ToastType.Loading,
message: `Downloading ${filename}..`,
})
}
const base64 = await getBase64FromBlob(blob)
const downloaded = await application.mobileDevice.downloadBase64AsFile(base64, filename)
if (downloaded) {
if (loadingToastId) {
dismissToast(loadingToastId)
}
if (!showToast) {
return
}
if (downloaded) {
addToast({
type: ToastType.Success,
message: `Downloaded ${filename}`,

View File

@@ -3,7 +3,12 @@ import { downloadBlobOnAndroid } from '@/NativeMobileWeb/DownloadBlobOnAndroid'
import { shareBlobOnMobile } from '@/NativeMobileWeb/ShareBlobOnMobile'
import { Platform } from '@standardnotes/snjs'
export const downloadOrShareBlobBasedOnPlatform = async (application: WebApplication, blob: Blob, filename: string) => {
export const downloadOrShareBlobBasedOnPlatform = async (
application: WebApplication,
blob: Blob,
filename: string,
showToastOnAndroid = true,
) => {
if (!application.isNativeMobileWeb()) {
application.getArchiveService().downloadData(blob, filename)
return
@@ -15,7 +20,7 @@ export const downloadOrShareBlobBasedOnPlatform = async (application: WebApplica
}
if (application.platform === Platform.Android) {
downloadBlobOnAndroid(application, blob, filename)
downloadBlobOnAndroid(application, blob, filename, showToastOnAndroid)
return
}
}