-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked ShareLocalDB, add extension when sharing
- Added File I/O to send logs with `.txt` extension - See PR 1160 for further details
- Loading branch information
1 parent
754de73
commit 55c3218
Showing
1 changed file
with
104 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void>((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<void>((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<void>((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); | ||
}); | ||
} |