Skip to content

Commit

Permalink
[FIX] firefox os race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabhg71 committed Oct 27, 2023
1 parent 512a99b commit 8049409
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
32 changes: 18 additions & 14 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,8 +1257,11 @@ let webKitFileList = null
* Displays the zone to select files from the archive
*/
function displayFileSelect () {
const isFireFoxOsNativeFileApiAvailable = typeof navigator.getDeviceStorages === 'function';

console.debug('File system api supported', params.isFileSystemApiSupported);
console.debug('Webkit supported', params.isWebkitDirApiSupported);
console.debug('Firefox os native file support api', isFireFoxOsNativeFileApiAvailable)

document.getElementById('openLocalFiles').style.display = 'block';
if (params.isFileSystemApiSupported || params.isWebkitDirApiSupported) {
Expand All @@ -1281,25 +1284,26 @@ function displayFileSelect () {
globalDropZone.addEventListener('drop', handleFileDrop);
}

document.getElementById('archiveList').addEventListener('change', async function (e) {
// handle zim selection from dropdown if multiple files are loaded via webkitdirectory or filesystem api
if (params.isFileSystemApiSupported) {
const files = await abstractFilesystemAccess.getSelectedZimFromCache(e.target.value)
setLocalArchiveFromFileList(files);
} else {
const files = abstractFilesystemAccess.getSelectedZimFromWebkitList(webKitFileList, e.target.value)
setLocalArchiveFromFileList(files);
}
});

if (typeof window.showDirectoryPicker === 'function') {
if (!isFireFoxOsNativeFileApiAvailable) {
document.getElementById('archiveList').addEventListener('change', async function (e) {
// handle zim selection from dropdown if multiple files are loaded via webkitdirectory or filesystem api
if (params.isFileSystemApiSupported) {
const files = await abstractFilesystemAccess.getSelectedZimFromCache(e.target.value)
setLocalArchiveFromFileList(files);
} else {
const files = abstractFilesystemAccess.getSelectedZimFromWebkitList(webKitFileList, e.target.value)
setLocalArchiveFromFileList(files);
}
});
}
if (params.isFileSystemApiSupported && !isFireFoxOsNativeFileApiAvailable) {
// Handles Folder selection when showDirectoryPicker is supported
document.getElementById('folderSelect').addEventListener('click', async function (e) {
e.preventDefault();
await abstractFilesystemAccess.selectDirectoryFromPickerViaFileSystemApi()
})
}
if (params.isWebkitDirApiSupported && !params.isFileSystemApiSupported) {
if (params.isWebkitDirApiSupported && !params.isFileSystemApiSupported && !isFireFoxOsNativeFileApiAvailable) {
// Handles Folder selection when webkitdirectory is supported but showDirectoryPicker is not
document.getElementById('folderSelect').addEventListener('change', async function (e) {
e.preventDefault();
Expand All @@ -1312,7 +1316,7 @@ function displayFileSelect () {
await abstractFilesystemAccess.updateZimDropdownOptions(filenames, '');
})
}
if (params.isFileSystemApiSupported) {
if (params.isFileSystemApiSupported && !isFireFoxOsNativeFileApiAvailable) {
// Handles File selection when showOpenFilePicker is supported and uses the filesystem api
document.getElementById('archiveFiles').addEventListener('click', async function (e) {
e.preventDefault();
Expand Down
8 changes: 6 additions & 2 deletions www/js/lib/abstractFilesystemAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ async function selectFileFromPickerViaFileSystemApi () {
const fileHandles = await window.showOpenFilePicker({ multiple: false });
const [selectedFile] = fileHandles;
const file = await selectedFile.getFile();

const filenameList = [selectedFile.name];
localStorage.setItem('zimFilenames', filenameList.join('|'));
cache.idxDB('zimFiles', selectedFile, function () {
// file saved in DB
updateZimDropdownOptions(filenameList, selectedFile.name);
});
updateZimDropdownOptions([selectedFile.name], selectedFile.name);
return [file];
}

Expand All @@ -155,6 +156,8 @@ async function selectFileFromPickerViaFileSystemApi () {
function getSelectedZimFromCache (selectedFilename) {
return new Promise((resolve, _reject) => {
cache.idxDB('zimFiles', async function (fileOrDirHandle) {
// Left it here for debugging purposes as its sometimes asking for permission even when its granted
console.debug('FileHandle and Permission', fileOrDirHandle, fileOrDirHandle.queryPermission())
if ((await fileOrDirHandle.queryPermission()) !== 'granted') await fileOrDirHandle.requestPermission();

if (fileOrDirHandle.kind === 'directory') {
Expand Down Expand Up @@ -220,6 +223,7 @@ async function handleFolderDropViaFileSystemAPI (packet) {
const fileInfo = packet.dataTransfer.items[0];
const fileOrDirHandle = await fileInfo.getAsFileSystemHandle();
if (fileOrDirHandle.kind === 'file') {
localStorage.setItem([fileOrDirHandle.name], [fileOrDirHandle.name].join('|'));
cache.idxDB('zimFiles', fileOrDirHandle, function () {
// save file in DB
updateZimDropdownOptions([fileOrDirHandle.name], fileOrDirHandle.name);
Expand Down

0 comments on commit 8049409

Please sign in to comment.