Compare commits
2 Commits
notificati
...
cross-plat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ee4a7e411 | ||
|
|
1c8528fb20 |
@@ -1,16 +1,9 @@
|
||||
/**
|
||||
* Data Export Section Component
|
||||
*
|
||||
* Provides UI and functionality for exporting user data and backing up identifier seeds.
|
||||
* Includes buttons for seed backup and database export, with platform-specific download instructions.
|
||||
*
|
||||
* @component
|
||||
* @displayName DataExportSection
|
||||
* @example
|
||||
* ```vue
|
||||
* <DataExportSection :active-did="currentDid" />
|
||||
* ```
|
||||
*/
|
||||
/** * Data Export Section Component * * Provides UI and functionality for
|
||||
exporting user data and backing up identifier seeds. * Includes buttons for seed
|
||||
backup and database export, with platform-specific download instructions. * *
|
||||
@component * @displayName DataExportSection * @example * ```vue *
|
||||
<DataExportSection :active-did="currentDid" />
|
||||
* ``` */
|
||||
|
||||
<template>
|
||||
<div
|
||||
@@ -36,28 +29,24 @@
|
||||
(excluding Identifier Data)
|
||||
</button>
|
||||
<a
|
||||
v-if="platformService?.needsSecondaryDownloadLink()"
|
||||
ref="downloadLink"
|
||||
:class="computedDownloadLinkClassNames()"
|
||||
class="block w-full text-center text-md bg-gradient-to-b from-green-500 to-green-800 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-1.5 py-2 rounded-md mb-6"
|
||||
>
|
||||
If no download happened yet, click again here to download now.
|
||||
</a>
|
||||
<div class="mt-4" v-if="showPlatformInstructions">
|
||||
<p>
|
||||
After the download, you can save the file in your preferred storage
|
||||
location.
|
||||
<div
|
||||
v-if="platformService?.getExportInstructions().length > 0"
|
||||
class="mt-4"
|
||||
>
|
||||
<p
|
||||
v-for="instruction in platformService?.getExportInstructions()"
|
||||
:key="instruction"
|
||||
class="list-disc list-outside ml-4"
|
||||
>
|
||||
{{ instruction }}
|
||||
</p>
|
||||
<ul>
|
||||
<li v-if="platformService.isCapacitor() && isIOS" class="list-disc list-outside ml-4">
|
||||
On iOS: Choose "More..." and select a place in iCloud, or go "Back"
|
||||
and save to another location.
|
||||
</li>
|
||||
<li v-if="platformService.isCapacitor() && !isIOS" class="list-disc list-outside ml-4">
|
||||
On Android: Choose "Open" and then share
|
||||
<font-awesome icon="share-nodes" class="fa-fw" />
|
||||
to your prefered place.
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -100,32 +89,26 @@ export default class DataExportSection extends Vue {
|
||||
/**
|
||||
* Platform service instance for platform-specific operations
|
||||
*/
|
||||
private platformService: PlatformService = PlatformServiceFactory.getInstance();
|
||||
|
||||
/**
|
||||
* Whether the current platform is iOS
|
||||
*/
|
||||
private get isIOS(): boolean {
|
||||
return /iPad|iPhone|iPod/.test(navigator.userAgent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to show platform-specific instructions
|
||||
*/
|
||||
private get showPlatformInstructions(): boolean {
|
||||
return this.platformService.isCapacitor();
|
||||
}
|
||||
private platformService?: PlatformService;
|
||||
|
||||
/**
|
||||
* Lifecycle hook to clean up resources
|
||||
* Revokes object URL when component is unmounted (web platform only)
|
||||
*/
|
||||
beforeUnmount() {
|
||||
if (this.downloadUrl && this.platformService.isWeb()) {
|
||||
if (this.downloadUrl && this.platformService?.needsDownloadCleanup()) {
|
||||
URL.revokeObjectURL(this.downloadUrl);
|
||||
}
|
||||
}
|
||||
|
||||
async mounted() {
|
||||
this.platformService = await PlatformServiceFactory.getInstance();
|
||||
logger.log(
|
||||
"DataExportSection mounted on platform:",
|
||||
process.env.VITE_PLATFORM,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the database to a JSON file
|
||||
* Uses platform-specific methods for saving the exported data
|
||||
@@ -136,39 +119,42 @@ export default class DataExportSection extends Vue {
|
||||
*/
|
||||
public async exportDatabase() {
|
||||
try {
|
||||
if (!this.platformService) {
|
||||
this.platformService = await PlatformServiceFactory.getInstance();
|
||||
}
|
||||
logger.log(
|
||||
"Starting database export on platform:",
|
||||
process.env.VITE_PLATFORM,
|
||||
);
|
||||
|
||||
const blob = await db.export({ prettyJson: true });
|
||||
const fileName = `${db.name}-backup.json`;
|
||||
logger.log("Database export details:", {
|
||||
fileName,
|
||||
blobSize: `${blob.size} bytes`,
|
||||
platform: process.env.VITE_PLATFORM,
|
||||
});
|
||||
|
||||
if (this.platformService.isWeb()) {
|
||||
// Web platform: Use download link
|
||||
this.downloadUrl = URL.createObjectURL(blob);
|
||||
const downloadAnchor = this.$refs.downloadLink as HTMLAnchorElement;
|
||||
downloadAnchor.href = this.downloadUrl;
|
||||
downloadAnchor.download = fileName;
|
||||
downloadAnchor.click();
|
||||
setTimeout(() => URL.revokeObjectURL(this.downloadUrl), 1000);
|
||||
} else if (this.platformService.isCapacitor()) {
|
||||
// Capacitor platform: Write to app directory
|
||||
const content = await blob.text();
|
||||
await this.platformService.writeFile(fileName, content);
|
||||
} else {
|
||||
// Other platforms: Use platform service
|
||||
await this.platformService.writeFile(fileName, await blob.text());
|
||||
}
|
||||
await this.platformService.exportDatabase(blob, fileName);
|
||||
logger.log("Database export completed successfully:", {
|
||||
fileName,
|
||||
platform: process.env.VITE_PLATFORM,
|
||||
});
|
||||
|
||||
this.$notify(
|
||||
{
|
||||
group: "alert",
|
||||
type: "success",
|
||||
title: "Export Successful",
|
||||
text: this.platformService.isWeb()
|
||||
? "See your downloads directory for the backup. It is in the Dexie format."
|
||||
: "The backup has been saved to your device.",
|
||||
text: this.platformService.getExportSuccessMessage(),
|
||||
},
|
||||
-1,
|
||||
);
|
||||
} catch (error) {
|
||||
logger.error("Export Error:", error);
|
||||
logger.error("Database export failed:", {
|
||||
error,
|
||||
platform: process.env.VITE_PLATFORM,
|
||||
});
|
||||
this.$notify(
|
||||
{
|
||||
group: "alert",
|
||||
@@ -187,7 +173,8 @@ export default class DataExportSection extends Vue {
|
||||
*/
|
||||
public computedStartDownloadLinkClassNames() {
|
||||
return {
|
||||
hidden: this.downloadUrl && this.platformService.isWeb(),
|
||||
hidden:
|
||||
this.downloadUrl && this.platformService?.needsSecondaryDownloadLink(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -197,7 +184,9 @@ export default class DataExportSection extends Vue {
|
||||
*/
|
||||
public computedDownloadLinkClassNames() {
|
||||
return {
|
||||
hidden: !this.downloadUrl || !this.platformService.isWeb(),
|
||||
hidden:
|
||||
!this.downloadUrl ||
|
||||
!this.platformService?.needsSecondaryDownloadLink(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,6 +108,7 @@ import { retrieveSettingsForActiveAccount } from "../db/index";
|
||||
import { accessToken } from "../libs/crypto";
|
||||
import { logger } from "../utils/logger";
|
||||
import { PlatformServiceFactory } from "../services/PlatformServiceFactory";
|
||||
import { PlatformService } from "../services/PlatformService";
|
||||
|
||||
@Component({ components: { VuePictureCropper } })
|
||||
export default class PhotoDialog extends Vue {
|
||||
@@ -123,13 +124,14 @@ export default class PhotoDialog extends Vue {
|
||||
uploading = false;
|
||||
visible = false;
|
||||
|
||||
private platformService = PlatformServiceFactory.getInstance();
|
||||
private platformService?: PlatformService;
|
||||
URL = window.URL || window.webkitURL;
|
||||
|
||||
async mounted() {
|
||||
try {
|
||||
const settings = await retrieveSettingsForActiveAccount();
|
||||
this.activeDid = settings.activeDid || "";
|
||||
this.platformService = await PlatformServiceFactory.getInstance();
|
||||
} catch (err: unknown) {
|
||||
logger.error("Error retrieving settings from database:", err);
|
||||
this.$notify(
|
||||
@@ -137,7 +139,10 @@ export default class PhotoDialog extends Vue {
|
||||
group: "alert",
|
||||
type: "danger",
|
||||
title: "Error",
|
||||
text: err.message || "There was an error retrieving your settings.",
|
||||
text:
|
||||
err instanceof Error
|
||||
? err.message
|
||||
: "There was an error retrieving your settings.",
|
||||
},
|
||||
-1,
|
||||
);
|
||||
@@ -181,6 +186,9 @@ export default class PhotoDialog extends Vue {
|
||||
|
||||
async takePhoto() {
|
||||
try {
|
||||
if (!this.platformService) {
|
||||
this.platformService = await PlatformServiceFactory.getInstance();
|
||||
}
|
||||
const result = await this.platformService.takePicture();
|
||||
this.blob = result.blob;
|
||||
this.fileName = result.fileName;
|
||||
@@ -200,6 +208,9 @@ export default class PhotoDialog extends Vue {
|
||||
|
||||
async pickPhoto() {
|
||||
try {
|
||||
if (!this.platformService) {
|
||||
this.platformService = await PlatformServiceFactory.getInstance();
|
||||
}
|
||||
const result = await this.platformService.pickImage();
|
||||
this.blob = result.blob;
|
||||
this.fileName = result.fileName;
|
||||
|
||||
@@ -46,6 +46,15 @@ export interface PlatformService {
|
||||
*/
|
||||
listFiles(directory: string): Promise<string[]>;
|
||||
|
||||
/**
|
||||
* Exports a database blob to a file, handling platform-specific save operations.
|
||||
* @param blob - The database blob to export
|
||||
* @param fileName - The name of the file to save
|
||||
* @returns Promise that resolves when the export is complete
|
||||
* @throws Error if export fails
|
||||
*/
|
||||
exportDatabase(blob: Blob, fileName: string): Promise<void>;
|
||||
|
||||
// Camera operations
|
||||
/**
|
||||
* Activates the device camera to take a picture.
|
||||
@@ -91,4 +100,28 @@ export interface PlatformService {
|
||||
* @returns Promise that resolves when the deep link has been handled
|
||||
*/
|
||||
handleDeepLink(url: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* Gets platform-specific instructions for saving exported files
|
||||
* @returns Array of instruction strings for the current platform
|
||||
*/
|
||||
getExportInstructions(): string[];
|
||||
|
||||
/**
|
||||
* Gets the success message for database export
|
||||
* @returns Success message appropriate for the current platform
|
||||
*/
|
||||
getExportSuccessMessage(): string;
|
||||
|
||||
/**
|
||||
* Checks if the platform requires a secondary download link
|
||||
* @returns true if platform needs a secondary download link
|
||||
*/
|
||||
needsSecondaryDownloadLink(): boolean;
|
||||
|
||||
/**
|
||||
* Checks if the platform needs cleanup after download
|
||||
* @returns true if platform needs cleanup after download
|
||||
*/
|
||||
needsDownloadCleanup(): boolean;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import { PlatformService } from "./PlatformService";
|
||||
import { WebPlatformService } from "./platforms/WebPlatformService";
|
||||
import { CapacitorPlatformService } from "./platforms/CapacitorPlatformService";
|
||||
import { ElectronPlatformService } from "./platforms/ElectronPlatformService";
|
||||
import { PyWebViewPlatformService } from "./platforms/PyWebViewPlatformService";
|
||||
|
||||
/**
|
||||
* Factory class for creating platform-specific service implementations.
|
||||
@@ -30,29 +27,43 @@ export class PlatformServiceFactory {
|
||||
*
|
||||
* @returns {PlatformService} The singleton instance of PlatformService
|
||||
*/
|
||||
public static getInstance(): PlatformService {
|
||||
public static async getInstance(): Promise<PlatformService> {
|
||||
if (PlatformServiceFactory.instance) {
|
||||
return PlatformServiceFactory.instance;
|
||||
}
|
||||
|
||||
const platform = process.env.VITE_PLATFORM || "web";
|
||||
let service: PlatformService;
|
||||
|
||||
switch (platform) {
|
||||
case "capacitor":
|
||||
PlatformServiceFactory.instance = new CapacitorPlatformService();
|
||||
case "capacitor": {
|
||||
const { CapacitorPlatformService } = await import(
|
||||
"./platforms/CapacitorPlatformService"
|
||||
);
|
||||
service = new CapacitorPlatformService();
|
||||
break;
|
||||
case "electron":
|
||||
PlatformServiceFactory.instance = new ElectronPlatformService();
|
||||
}
|
||||
case "electron": {
|
||||
const { ElectronPlatformService } = await import(
|
||||
"./platforms/ElectronPlatformService"
|
||||
);
|
||||
service = new ElectronPlatformService();
|
||||
break;
|
||||
case "pywebview":
|
||||
PlatformServiceFactory.instance = new PyWebViewPlatformService();
|
||||
}
|
||||
case "pywebview": {
|
||||
const { PyWebViewPlatformService } = await import(
|
||||
"./platforms/PyWebViewPlatformService"
|
||||
);
|
||||
service = new PyWebViewPlatformService();
|
||||
break;
|
||||
}
|
||||
case "web":
|
||||
default:
|
||||
PlatformServiceFactory.instance = new WebPlatformService();
|
||||
service = new WebPlatformService();
|
||||
break;
|
||||
}
|
||||
|
||||
return PlatformServiceFactory.instance;
|
||||
PlatformServiceFactory.instance = service;
|
||||
return service;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { ImageResult, PlatformService } from "../PlatformService";
|
||||
import { Filesystem, Directory } from "@capacitor/filesystem";
|
||||
import { Camera, CameraResultType, CameraSource } from "@capacitor/camera";
|
||||
import { logger } from "../../utils/logger";
|
||||
import { Share } from "@capacitor/share";
|
||||
|
||||
/**
|
||||
* Platform service implementation for Capacitor (mobile) platform.
|
||||
@@ -65,7 +66,9 @@ export class CapacitorPlatformService implements PlatformService {
|
||||
path: directory,
|
||||
directory: Directory.Data,
|
||||
});
|
||||
return result.files.map(file => typeof file === 'string' ? file : file.name);
|
||||
return result.files.map((file) =>
|
||||
typeof file === "string" ? file : file.name,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,4 +191,91 @@ export class CapacitorPlatformService implements PlatformService {
|
||||
// This is just a placeholder for the interface
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
getExportInstructions(): string[] {
|
||||
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
|
||||
if (isIOS) {
|
||||
return [
|
||||
"On iOS: Choose 'More...' and select a place in iCloud, or go 'Back' and save to another location.",
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
"On Android: Choose 'Open' and then share to your preferred place.",
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
getExportSuccessMessage(): string {
|
||||
return "The backup has been saved to your device.";
|
||||
}
|
||||
|
||||
needsSecondaryDownloadLink(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
needsDownloadCleanup(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
async exportDatabase(blob: Blob, fileName: string): Promise<void> {
|
||||
logger.log("Starting database export on Capacitor platform:", {
|
||||
fileName,
|
||||
blobSize: `${blob.size} bytes`,
|
||||
});
|
||||
|
||||
// Create a File object from the Blob
|
||||
const file = new File([blob], fileName, { type: "application/json" });
|
||||
|
||||
try {
|
||||
logger.log("Attempting to use native share sheet");
|
||||
// Use the native share sheet
|
||||
await navigator.share({
|
||||
files: [file],
|
||||
title: fileName,
|
||||
});
|
||||
logger.log("Database export completed via native share sheet");
|
||||
} catch (error) {
|
||||
logger.log("Native share failed, falling back to Capacitor Share API");
|
||||
// Fallback to Capacitor Share API if Web Share API fails
|
||||
// First save to temporary file
|
||||
const base64Data = await this.blobToBase64(blob);
|
||||
const result = await Filesystem.writeFile({
|
||||
path: fileName,
|
||||
data: base64Data,
|
||||
directory: Directory.Cache, // Use Cache instead of Documents for temporary files
|
||||
recursive: true,
|
||||
});
|
||||
logger.log("Temporary file created for sharing:", result.uri);
|
||||
|
||||
// Then share using Capacitor Share API
|
||||
await Share.share({
|
||||
title: fileName,
|
||||
url: result.uri,
|
||||
});
|
||||
logger.log("Database export completed via Capacitor Share API");
|
||||
|
||||
// Clean up the temporary file
|
||||
try {
|
||||
await Filesystem.deleteFile({
|
||||
path: fileName,
|
||||
directory: Directory.Cache,
|
||||
});
|
||||
logger.log("Temporary file cleaned up successfully");
|
||||
} catch (cleanupError) {
|
||||
logger.warn("Failed to clean up temporary file:", cleanupError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async blobToBase64(blob: Blob): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = () => {
|
||||
const base64data = reader.result as string;
|
||||
resolve(base64data.split(",")[1]);
|
||||
};
|
||||
reader.onerror = reject;
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,4 +121,16 @@ export class ElectronPlatformService implements PlatformService {
|
||||
logger.error("handleDeepLink not implemented in Electron platform");
|
||||
throw new Error("Not implemented");
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports a database blob to a file.
|
||||
* @param _blob - The database blob to export
|
||||
* @param _fileName - The name of the file to save
|
||||
* @throws Error with "Not implemented" message
|
||||
* @todo Implement file export using Electron's file system API
|
||||
*/
|
||||
async exportDatabase(_blob: Blob, _fileName: string): Promise<void> {
|
||||
logger.error("exportDatabase not implemented in Electron platform");
|
||||
throw new Error("Not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,4 +122,16 @@ export class PyWebViewPlatformService implements PlatformService {
|
||||
logger.error("handleDeepLink not implemented in PyWebView platform");
|
||||
throw new Error("Not implemented");
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports a database blob to a file using the Python backend.
|
||||
* @param _blob - The database blob to export
|
||||
* @param _fileName - The name of the file to save
|
||||
* @throws Error with "Not implemented" message
|
||||
* @todo Implement file export through pywebview's Python-JavaScript bridge
|
||||
*/
|
||||
async exportDatabase(_blob: Blob, _fileName: string): Promise<void> {
|
||||
logger.error("exportDatabase not implemented in PyWebView platform");
|
||||
throw new Error("Not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,4 +209,53 @@ export class WebPlatformService implements PlatformService {
|
||||
// Web platform can handle deep links through URL parameters
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
getExportInstructions(): string[] {
|
||||
return [
|
||||
"After the download, you can save the file in your preferred storage location.",
|
||||
];
|
||||
}
|
||||
|
||||
getExportSuccessMessage(): string {
|
||||
return "See your downloads directory for the backup. It is in the Dexie format.";
|
||||
}
|
||||
|
||||
needsSecondaryDownloadLink(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
needsDownloadCleanup(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
async exportDatabase(blob: Blob, fileName: string): Promise<void> {
|
||||
logger.log("Starting database export on web platform:", {
|
||||
fileName,
|
||||
blobSize: `${blob.size} bytes`,
|
||||
});
|
||||
|
||||
try {
|
||||
logger.log("Creating download link for database export");
|
||||
// Create a download link
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
|
||||
logger.log("Triggering download");
|
||||
// Trigger the download
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
|
||||
logger.log("Cleaning up download link");
|
||||
// Clean up
|
||||
window.URL.revokeObjectURL(url);
|
||||
document.body.removeChild(a);
|
||||
|
||||
logger.log("Database export completed successfully");
|
||||
} catch (error) {
|
||||
logger.error("Failed to export database:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,21 @@ export async function createBuildConfig(mode: string) {
|
||||
assetsDir: 'assets',
|
||||
chunkSizeWarningLimit: 1000,
|
||||
rollupOptions: {
|
||||
external: isCapacitor ? ['@capacitor/app'] : []
|
||||
external: isCapacitor
|
||||
? [
|
||||
'@capacitor/app',
|
||||
'@capacitor/share',
|
||||
'@capacitor/filesystem',
|
||||
'@capacitor/camera',
|
||||
'@capacitor/core'
|
||||
]
|
||||
: [
|
||||
'@capacitor/app',
|
||||
'@capacitor/share',
|
||||
'@capacitor/filesystem',
|
||||
'@capacitor/camera',
|
||||
'@capacitor/core'
|
||||
]
|
||||
}
|
||||
},
|
||||
define: {
|
||||
|
||||
Reference in New Issue
Block a user