From 55c32186586bfe590dc884ea6250189b299a07ac Mon Sep 17 00:00:00 2001 From: Katie Rischpater <98350084+the-bay-kay@users.noreply.github.com> Date: Wed, 26 Jun 2024 14:32:26 -0700 Subject: [PATCH] Reworked ShareLocalDB, add extension when sharing - Added File I/O to send logs with `.txt` extension - See PR 1160 for further details --- www/js/services/shareLocalDBFile.ts | 132 ++++++++++++++++++++++------ 1 file changed, 104 insertions(+), 28 deletions(-) diff --git a/www/js/services/shareLocalDBFile.ts b/www/js/services/shareLocalDBFile.ts index 8d9a49e53..6a54f7377 100644 --- a/www/js/services/shareLocalDBFile.ts +++ b/www/js/services/shareLocalDBFile.ts @@ -1,40 +1,116 @@ import i18next from 'i18next'; -import { displayErrorMsg, logDebug, logInfo, logWarn } from '../plugin/logger'; +import { displayError, displayErrorMsg, logDebug, logWarn } from '../plugin/logger'; -export async function sendLocalDBFile(database: string) { - let parentDir = 'unknown'; +function localDBHelpers(fileName: string, fileExtension: string = '.txt') { + async function localCopyFile() { + return new Promise((resolve, reject) => { + let pathToFile, parentDirectory; + if (window['cordova'].platformId == 'android') { + parentDirectory = window['cordova'].file.dataDirectory; + pathToFile = fileName; + } else if (window['cordova'].platformId == 'ios') { + parentDirectory = window['cordova'].file.dataDirectory + '../'; + pathToFile = 'LocalDatabase/' + fileName; + } else { + displayErrorMsg('Error: Unknown OS!'); + throw new Error('Error: Unknown OS!'); + } - if (window['cordova'].platformId == 'android') { - parentDir = 'app://databases'; - } + window['resolveLocalFileSystemURL'](parentDirectory, (fs) => { + fs.filesystem.root.getFile(pathToFile, { create: false, exclusive: false }, (fileEntry) => { + // logDebug(`fileEntry ${fileEntry.nativeURL} is file? ${fileEntry.isFile.toString()}`); + logDebug(`fileEntry is: ${JSON.stringify(fileEntry, null, 2)}`); + window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, (copyDir) => { + logDebug(`DirectoryEntry is: ${JSON.stringify(copyDir.filesystem.root, null, 2)}`); - if (window['cordova'].platformId == 'ios') { - logDebug(window['cordova'].file.dataDirectory); - parentDir = window['cordova'].file.dataDirectory + '../LocalDatabase'; + fileEntry.copyTo( + copyDir.filesystem.root, + fileName + fileExtension, + (res) => { + logDebug(`Res: ${res}`); + resolve(); + }, + (rej) => { + logDebug(`Rej: ${JSON.stringify(rej, null, 2)}`); + reject(); + }, + ); + }); + }); + }); + }); } - if (parentDir == 'unknown') { - displayErrorMsg('parentDir unexpectedly = ' + parentDir + '!'); - return; + function localShareFile() { + return new Promise((resolve, reject) => { + window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, (fs) => { + fs.filesystem.root.getFile( + fileName + fileExtension, + null, + (fileEntry) => { + const shareObj = { + files: [fileEntry.nativeURL], + message: i18next.t('shareFile-service.send-log.body-please-fill-in-what-is-wrong'), + subject: i18next.t('shareFile-service.send-log.subject-logs'), + }; + window['plugins'].socialsharing.shareWithOptions( + shareObj, + (result) => { + logDebug(`Share Completed? ${result.completed}`); // On Android, most likely returns false + logDebug(`Shared to app: ${result.app}`); + resolve(); + }, + (msg) => { + logDebug(`Sharing failed with message ${msg}`); + }, + ); + }, + (error) => { + displayError(error, 'Error while sharing logs'); + reject(error); + }, + ); + }); + }); } - logInfo('Going to email ' + database); - parentDir = parentDir + '/' + database; + function localClearData() { + return new Promise((resolve, reject) => { + window['resolveLocalFileSystemURL'](window['cordova'].file.cacheDirectory, (fs) => { + fs.filesystem.root.getFile(fileName + fileExtension, null, (fileEntry) => { + fileEntry.remove( + () => { + logDebug(`Successfully cleaned up file ${fileName}`); + resolve(); + }, + (err) => { + logWarn(`Error deleting ${fileName} : ${err}`); + reject(err); + }, + ); + }); + }); + }); + } - const shareObj = { - files: [parentDir], - message: i18next.t('shareFile-service.send-log.body-please-fill-in-what-is-wrong'), - subject: i18next.t('shareFile-service.send-log.subject-logs'), + return { + copyFile: localCopyFile, + shareData: localShareFile, + clearData: localClearData, }; - +} +export async function sendLocalDBFile(database: string) { alert(i18next.t('shareFile-service.send-to')); - window['plugins'].socialsharing.shareWithOptions( - shareObj, - (result) => { - logDebug(`Shared to app: ${result.app}`); - }, - (err) => { - logWarn(`Sharing failed with error: ${err}`); - }, - ); + + const dataMethods = localDBHelpers(database); + dataMethods + .copyFile() + .then(dataMethods.shareData) + .then(dataMethods.clearData) + .then(() => { + logDebug(`File Shared!`); + }) + .catch((err) => { + displayError(err); + }); }