forked from FreeTubeApp/FreeTube
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import custom localforage which maps to cordova file api
- Loading branch information
1 parent
636f570
commit 561bd55
Showing
3 changed files
with
149 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import localforage from '../../node_modules/localforage/dist/localforage' | ||
import { requestDirectory, readFromFile, writeToFile } from '../renderer/helpers/cordova' | ||
|
||
export function createInstance(kwargs) { | ||
const instance = localforage.createInstance(kwargs) | ||
return { | ||
async getItem(key) { | ||
const fs = await requestDirectory() | ||
const data = await readFromFile(fs, key) | ||
// if the data is empty, fallback to localstorage | ||
if (data === '') return instance.getItem(key) | ||
// if not, return the data | ||
return data | ||
}, | ||
async setItem(key, value) { | ||
const fs = await requestDirectory() | ||
await writeToFile(fs, key, value) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import cordova from 'cordova' | ||
|
||
/** | ||
* @typedef FileOptions | ||
* @property {boolean} create | ||
* @property {boolean} exclusive | ||
*/ | ||
|
||
/** | ||
* @typedef Writer | ||
* @property {Function} onerror | ||
* @property {Function} write | ||
*/ | ||
|
||
/** | ||
* @callback CreateWriterCallback | ||
* @param {Writer} writer | ||
* @returns {void} | ||
*/ | ||
|
||
/** | ||
* @callback CreateWriter | ||
* @param {CreateWriterCallback} callback | ||
* @returns {void} | ||
*/ | ||
|
||
/** | ||
* @callback GetBlobCallback | ||
* @param {Blob} blob | ||
* @returns {void} | ||
*/ | ||
|
||
/** | ||
* @callback GetBlob | ||
* @param {GetBlobCallback} callback | ||
*/ | ||
|
||
/** | ||
* @typedef FileReference | ||
* @property {CreateWriter} createWriter | ||
* @property {GetBlob} file | ||
*/ | ||
|
||
/** | ||
* @callback GetFileCallback | ||
* @param {FileReference} file | ||
*/ | ||
|
||
/** | ||
* @callback GetFile | ||
* @param {string} patch | ||
* @param {FileOptions} options | ||
* @param {GetFileCallback} callback | ||
* @returns {void} | ||
*/ | ||
|
||
/** | ||
* @typedef FileSystemHook | ||
* @property {GetFile} getFile | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {string} directory this *must* be one of the accepted cordova file keys "cordova.file.*"" | ||
* @returns {Promise<FileSystemHook>} a filesystem hook that starts in the given directory | ||
*/ | ||
export function requestDirectory(directory = cordova.file.externalApplicationStorageDirectory) { | ||
return new Promise((resolve, _reject) => { | ||
window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 10000, function (_) { | ||
window.resolveLocalFileSystemURL(directory, resolve) | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* | ||
* @param {FileSystemHook} fsHook | ||
* @param {string} path | ||
* @param {string} content | ||
* @param {FileOptions} options | ||
* @returns {Promise<void>} | ||
*/ | ||
export function writeToFile(fsHook, path, content, options = { create: true, exclusive: false }) { | ||
return new Promise((resolve, reject) => { | ||
fsHook.getFile(path, options, (file) => { | ||
file.createWriter((writer) => { | ||
writer.onerror = reject | ||
writer.write(content) | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* | ||
* @param {FileSystemHook} fsHook | ||
* @param {string} path | ||
* @param {string} content | ||
* @param {FileOptions} options | ||
* @returns {Promise<string>} | ||
*/ | ||
export function readFromFile(fsHook, path) { | ||
return new Promise((resolve, reject) => { | ||
fsHook.getFile(path, { create: true, exclusive: false }, (fileEntry) => { | ||
fileEntry.file((file) => { | ||
const reader = new FileReader() | ||
reader.onloadend = () => resolve(reader.result) | ||
reader.onerror = (e) => reject(e) | ||
reader.readAsText(file) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
export function removeFile(fsHook, path) { | ||
return new Promise((resolve, reject) => { | ||
fsHook.remove(() => { | ||
resolve() | ||
}, (error) => { | ||
reject(error) | ||
}, () => { | ||
reject(new Error('File does not exist')) | ||
}) | ||
}) | ||
} |