fix: files download in mobile webview (#1726)
This commit is contained in:
@@ -20,6 +20,7 @@ import { action, makeObservable, observable, reaction } from 'mobx'
|
|||||||
import { WebApplication } from '../Application/Application'
|
import { WebApplication } from '../Application/Application'
|
||||||
import { AbstractViewController } from './Abstract/AbstractViewController'
|
import { AbstractViewController } from './Abstract/AbstractViewController'
|
||||||
import { NotesController } from './NotesController'
|
import { NotesController } from './NotesController'
|
||||||
|
import { downloadOrShareBlobBasedOnPlatform } from '@/Utils/DownloadOrShareBasedOnPlatform'
|
||||||
|
|
||||||
const UnprotectedFileActions = [PopoverFileItemActionType.ToggleFileProtection]
|
const UnprotectedFileActions = [PopoverFileItemActionType.ToggleFileProtection]
|
||||||
const NonMutatingFileActions = [PopoverFileItemActionType.DownloadFile, PopoverFileItemActionType.PreviewFile]
|
const NonMutatingFileActions = [PopoverFileItemActionType.DownloadFile, PopoverFileItemActionType.PreviewFile]
|
||||||
@@ -267,7 +268,10 @@ export class FilesController extends AbstractViewController {
|
|||||||
await saver.finish()
|
await saver.finish()
|
||||||
} else {
|
} else {
|
||||||
const finalBytes = concatenateUint8Arrays(decryptedBytesArray)
|
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({
|
addToast({
|
||||||
|
|||||||
@@ -3,18 +3,31 @@ import { getBase64FromBlob } from '@/Utils'
|
|||||||
import { Platform } from '@standardnotes/snjs'
|
import { Platform } from '@standardnotes/snjs'
|
||||||
import { addToast, ToastType, dismissToast } from '@standardnotes/toast'
|
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) {
|
if (!application.isNativeMobileWeb() || application.platform !== Platform.Android) {
|
||||||
throw new Error('Download function being used on non-android platform')
|
throw new Error('Download function being used on non-android platform')
|
||||||
}
|
}
|
||||||
const loadingToastId = addToast({
|
let loadingToastId: string | undefined
|
||||||
type: ToastType.Loading,
|
if (showToast) {
|
||||||
message: `Downloading ${filename}..`,
|
loadingToastId = addToast({
|
||||||
})
|
type: ToastType.Loading,
|
||||||
|
message: `Downloading ${filename}..`,
|
||||||
|
})
|
||||||
|
}
|
||||||
const base64 = await getBase64FromBlob(blob)
|
const base64 = await getBase64FromBlob(blob)
|
||||||
const downloaded = await application.mobileDevice.downloadBase64AsFile(base64, filename)
|
const downloaded = await application.mobileDevice.downloadBase64AsFile(base64, filename)
|
||||||
if (downloaded) {
|
if (loadingToastId) {
|
||||||
dismissToast(loadingToastId)
|
dismissToast(loadingToastId)
|
||||||
|
}
|
||||||
|
if (!showToast) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (downloaded) {
|
||||||
addToast({
|
addToast({
|
||||||
type: ToastType.Success,
|
type: ToastType.Success,
|
||||||
message: `Downloaded ${filename}`,
|
message: `Downloaded ${filename}`,
|
||||||
|
|||||||
@@ -3,7 +3,12 @@ import { downloadBlobOnAndroid } from '@/NativeMobileWeb/DownloadBlobOnAndroid'
|
|||||||
import { shareBlobOnMobile } from '@/NativeMobileWeb/ShareBlobOnMobile'
|
import { shareBlobOnMobile } from '@/NativeMobileWeb/ShareBlobOnMobile'
|
||||||
import { Platform } from '@standardnotes/snjs'
|
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()) {
|
if (!application.isNativeMobileWeb()) {
|
||||||
application.getArchiveService().downloadData(blob, filename)
|
application.getArchiveService().downloadData(blob, filename)
|
||||||
return
|
return
|
||||||
@@ -15,7 +20,7 @@ export const downloadOrShareBlobBasedOnPlatform = async (application: WebApplica
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (application.platform === Platform.Android) {
|
if (application.platform === Platform.Android) {
|
||||||
downloadBlobOnAndroid(application, blob, filename)
|
downloadBlobOnAndroid(application, blob, filename, showToastOnAndroid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user