fix: downloading backups in mobile webview (#1703)
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { WebApplication } from '@/Application/Application'
|
||||
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) => {
|
||||
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}..`,
|
||||
})
|
||||
const base64 = await getBase64FromBlob(blob)
|
||||
const downloaded = await application.mobileDevice.downloadBase64AsFile(base64, filename)
|
||||
if (downloaded) {
|
||||
dismissToast(loadingToastId)
|
||||
addToast({
|
||||
type: ToastType.Success,
|
||||
message: `Downloaded ${filename}`,
|
||||
})
|
||||
} else {
|
||||
addToast({
|
||||
type: ToastType.Error,
|
||||
message: `Could not download ${filename}`,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
import { WebApplication } from '@/Application/Application'
|
||||
import { getBase64FromBlob } from '@/Utils'
|
||||
import { getNoteBlob, getNoteFileName } from '@/Utils/NoteExportUtils'
|
||||
import { parseFileName } from '@standardnotes/filepicker'
|
||||
import { Platform, SNNote } from '@standardnotes/snjs'
|
||||
import { addToast, dismissToast, ToastType } from '@standardnotes/toast'
|
||||
import { sanitizeFileName } from '@standardnotes/ui-services'
|
||||
|
||||
export const downloadSelectedItemsOnAndroid = async (application: WebApplication, notes: SNNote[]) => {
|
||||
if (!application.isNativeMobileWeb() || application.platform !== Platform.Android) {
|
||||
throw new Error('Function being used on non-android platform')
|
||||
}
|
||||
if (notes.length === 1) {
|
||||
const note = notes[0]
|
||||
const blob = getNoteBlob(application, note)
|
||||
const base64 = await getBase64FromBlob(blob)
|
||||
const { name, ext } = parseFileName(getNoteFileName(application, note))
|
||||
const filename = `${sanitizeFileName(name)}.${ext}`
|
||||
const loadingToastId = addToast({
|
||||
type: ToastType.Loading,
|
||||
message: `Exporting ${filename}..`,
|
||||
})
|
||||
const downloaded = await application.mobileDevice.downloadBase64AsFile(base64, filename)
|
||||
if (downloaded) {
|
||||
dismissToast(loadingToastId)
|
||||
addToast({
|
||||
type: ToastType.Success,
|
||||
message: `Exported ${filename}`,
|
||||
})
|
||||
} else {
|
||||
addToast({
|
||||
type: ToastType.Error,
|
||||
message: `Could not export ${filename}`,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
if (notes.length > 1) {
|
||||
const zippedDataBlob = await application.getArchiveService().zipData(
|
||||
notes.map((note) => {
|
||||
return {
|
||||
name: getNoteFileName(application, note),
|
||||
content: getNoteBlob(application, note),
|
||||
}
|
||||
}),
|
||||
)
|
||||
const zippedDataAsBase64 = await getBase64FromBlob(zippedDataBlob)
|
||||
const filename = `Standard Notes Export - ${application.getArchiveService().formattedDateForExports()}.zip`
|
||||
const loadingToastId = addToast({
|
||||
type: ToastType.Loading,
|
||||
message: `Exporting ${filename}..`,
|
||||
})
|
||||
const downloaded = await application.mobileDevice.downloadBase64AsFile(zippedDataAsBase64, filename)
|
||||
if (downloaded) {
|
||||
dismissToast(loadingToastId)
|
||||
addToast({
|
||||
type: ToastType.Success,
|
||||
message: `Exported ${filename}`,
|
||||
})
|
||||
} else {
|
||||
addToast({
|
||||
type: ToastType.Error,
|
||||
message: `Could not export ${filename}`,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { WebApplication } from '@/Application/Application'
|
||||
import { getNoteBlob, getNoteFileName } from '@/Utils/NoteExportUtils'
|
||||
import { parseFileName } from '@standardnotes/filepicker'
|
||||
import { Platform, SNNote } from '@standardnotes/snjs'
|
||||
import { sanitizeFileName } from '@standardnotes/ui-services'
|
||||
import { downloadBlobOnAndroid } from './DownloadBlobOnAndroid'
|
||||
|
||||
export const downloadSelectedNotesOnAndroid = async (application: WebApplication, notes: SNNote[]) => {
|
||||
if (!application.isNativeMobileWeb() || application.platform !== Platform.Android) {
|
||||
throw new Error('Function being used on non-android platform')
|
||||
}
|
||||
if (notes.length === 1) {
|
||||
const note = notes[0]
|
||||
const blob = getNoteBlob(application, note)
|
||||
const { name, ext } = parseFileName(getNoteFileName(application, note))
|
||||
const filename = `${sanitizeFileName(name)}.${ext}`
|
||||
await downloadBlobOnAndroid(application, blob, filename)
|
||||
return
|
||||
}
|
||||
if (notes.length > 1) {
|
||||
const zippedDataBlob = await application.getArchiveService().zipData(
|
||||
notes.map((note) => {
|
||||
return {
|
||||
name: getNoteFileName(application, note),
|
||||
content: getNoteBlob(application, note),
|
||||
}
|
||||
}),
|
||||
)
|
||||
const filename = `Standard Notes Export - ${application.getArchiveService().formattedDateForExports()}.zip`
|
||||
await downloadBlobOnAndroid(application, zippedDataBlob, filename)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { WebApplication } from '@/Application/Application'
|
||||
import { getBase64FromBlob } from '@/Utils'
|
||||
|
||||
export const shareBlobOnMobile = async (application: WebApplication, blob: Blob, filename: string) => {
|
||||
if (!application.isNativeMobileWeb()) {
|
||||
throw new Error('Share function being used outside mobile webview')
|
||||
}
|
||||
const base64 = await getBase64FromBlob(blob)
|
||||
application.mobileDevice.shareBase64AsFile(base64, filename)
|
||||
}
|
||||
@@ -1,21 +1,20 @@
|
||||
import { WebApplication } from '@/Application/Application'
|
||||
import { getBase64FromBlob } from '@/Utils'
|
||||
import { getNoteBlob, getNoteFileName } from '@/Utils/NoteExportUtils'
|
||||
import { parseFileName } from '@standardnotes/filepicker'
|
||||
import { SNNote } from '@standardnotes/snjs'
|
||||
import { sanitizeFileName } from '@standardnotes/ui-services'
|
||||
import { shareBlobOnMobile } from './ShareBlobOnMobile'
|
||||
|
||||
export const shareSelectedItems = async (application: WebApplication, notes: SNNote[]) => {
|
||||
export const shareSelectedNotes = async (application: WebApplication, notes: SNNote[]) => {
|
||||
if (!application.isNativeMobileWeb()) {
|
||||
throw new Error('Share function being used outside mobile webview')
|
||||
}
|
||||
if (notes.length === 1) {
|
||||
const note = notes[0]
|
||||
const blob = getNoteBlob(application, note)
|
||||
const base64 = await getBase64FromBlob(blob)
|
||||
const { name, ext } = parseFileName(getNoteFileName(application, note))
|
||||
const filename = `${sanitizeFileName(name)}.${ext}`
|
||||
application.mobileDevice.shareBase64AsFile(base64, filename)
|
||||
void shareBlobOnMobile(application, blob, filename)
|
||||
return
|
||||
}
|
||||
if (notes.length > 1) {
|
||||
@@ -27,9 +26,9 @@ export const shareSelectedItems = async (application: WebApplication, notes: SNN
|
||||
}
|
||||
}),
|
||||
)
|
||||
const zippedDataAsBase64 = await getBase64FromBlob(zippedDataBlob)
|
||||
application.mobileDevice.shareBase64AsFile(
|
||||
zippedDataAsBase64,
|
||||
void shareBlobOnMobile(
|
||||
application,
|
||||
zippedDataBlob,
|
||||
`Standard Notes Export - ${application.getArchiveService().formattedDateForExports()}.zip`,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user