Skip to content

Commit

Permalink
Reworked ShareLocalDB, add extension when sharing
Browse files Browse the repository at this point in the history
- Added File I/O to send logs with `.txt` extension
- See PR 1160 for further details
  • Loading branch information
the-bay-kay committed Jun 26, 2024
1 parent 754de73 commit 55c3218
Showing 1 changed file with 104 additions and 28 deletions.
132 changes: 104 additions & 28 deletions www/js/services/shareLocalDBFile.ts
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) => {

Check warning on line 6 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L5-L6

Added lines #L5 - L6 were not covered by tests
let pathToFile, parentDirectory;
if (window['cordova'].platformId == 'android') {
parentDirectory = window['cordova'].file.dataDirectory;
pathToFile = fileName;

Check warning on line 10 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L9-L10

Added lines #L9 - L10 were not covered by tests
} 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!');

Check warning on line 16 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L12-L16

Added lines #L12 - L16 were not covered by tests
}

if (window['cordova'].platformId == 'android') {
parentDir = 'app://databases';
}
window['resolveLocalFileSystemURL'](parentDirectory, (fs) => {
fs.filesystem.root.getFile(pathToFile, { create: false, exclusive: false }, (fileEntry) => {

Check warning on line 20 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L19-L20

Added lines #L19 - L20 were not covered by tests
// 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)}`);

Check warning on line 24 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L22-L24

Added lines #L22 - L24 were not covered by tests

if (window['cordova'].platformId == 'ios') {
logDebug(window['cordova'].file.dataDirectory);
parentDir = window['cordova'].file.dataDirectory + '../LocalDatabase';
fileEntry.copyTo(

Check warning on line 26 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L26

Added line #L26 was not covered by tests
copyDir.filesystem.root,
fileName + fileExtension,
(res) => {
logDebug(`Res: ${res}`);
resolve();

Check warning on line 31 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L29-L31

Added lines #L29 - L31 were not covered by tests
},
(rej) => {
logDebug(`Rej: ${JSON.stringify(rej, null, 2)}`);
reject();

Check warning on line 35 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L33-L35

Added lines #L33 - L35 were not covered by tests
},
);
});
});
});
});
}

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(

Check warning on line 47 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L44-L47

Added lines #L44 - L47 were not covered by tests
fileName + fileExtension,
null,
(fileEntry) => {
const shareObj = {

Check warning on line 51 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L50-L51

Added lines #L50 - L51 were not covered by tests
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(

Check warning on line 56 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L56

Added line #L56 was not covered by tests
shareObj,
(result) => {
logDebug(`Share Completed? ${result.completed}`); // On Android, most likely returns false
logDebug(`Shared to app: ${result.app}`);
resolve();

Check warning on line 61 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L58-L61

Added lines #L58 - L61 were not covered by tests
},
(msg) => {
logDebug(`Sharing failed with message ${msg}`);

Check warning on line 64 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L63-L64

Added lines #L63 - L64 were not covered by tests
},
);
},
(error) => {
displayError(error, 'Error while sharing logs');
reject(error);

Check warning on line 70 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L68-L70

Added lines #L68 - L70 were not covered by tests
},
);
});
});
}

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();

Check warning on line 84 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L77-L84

Added lines #L77 - L84 were not covered by tests
},
(err) => {
logWarn(`Error deleting ${fileName} : ${err}`);
reject(err);

Check warning on line 88 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L86-L88

Added lines #L86 - L88 were not covered by tests
},
);
});
});
});
}

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 {

Check warning on line 96 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L96

Added line #L96 was not covered by tests
copyFile: localCopyFile,
shareData: localShareFile,
clearData: localClearData,
};

}
export async function sendLocalDBFile(database: string) {
alert(i18next.t('shareFile-service.send-to'));

Check warning on line 103 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L102-L103

Added lines #L102 - L103 were not covered by tests
window['plugins'].socialsharing.shareWithOptions(
shareObj,
(result) => {
logDebug(`Shared to app: ${result.app}`);
},
(err) => {
logWarn(`Sharing failed with error: ${err}`);
},
);

const dataMethods = localDBHelpers(database);
dataMethods

Check warning on line 106 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L105-L106

Added lines #L105 - L106 were not covered by tests
.copyFile()
.then(dataMethods.shareData)
.then(dataMethods.clearData)
.then(() => {
logDebug(`File Shared!`);

Check warning on line 111 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L110-L111

Added lines #L110 - L111 were not covered by tests
})
.catch((err) => {
displayError(err);

Check warning on line 114 in www/js/services/shareLocalDBFile.ts

View check run for this annotation

Codecov / codecov/patch

www/js/services/shareLocalDBFile.ts#L113-L114

Added lines #L113 - L114 were not covered by tests
});
}

0 comments on commit 55c3218

Please sign in to comment.