Skip to content

Commit

Permalink
improved cordova electron plugin api
Browse files Browse the repository at this point in the history
  • Loading branch information
regnete committed Nov 7, 2023
1 parent 1b9c63a commit 6e3a524
Showing 1 changed file with 14 additions and 58 deletions.
72 changes: 14 additions & 58 deletions src/electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,7 @@ const PROGRESS_INTERVAL_MILLIS = 400;
// const fetch = require('electron-fetch').default
const FormData = require('form-data')

Check failure on line 35 in src/electron/index.js

View workflow job for this annotation

GitHub Actions / Lint Test

Missing semicolon

/**
*
* @param callbackContext
* @returns {any}
*/
function getFilePluginUtil(callbackContext)
{
return callbackContext.getCordovaService('File').util;
}

Check failure on line 37 in src/electron/index.js

View workflow job for this annotation

GitHub Actions / Lint Test

More than 1 blank line not allowed
/**
* get absolute file path for given url (cdvfile://, efs://)
* @param {string} url
* @param callbackContext
* @returns {string | null}
*/
function urlToFilePath(url, callbackContext)
{
return getFilePluginUtil(callbackContext).urlToFilePath(url);
}

/**
* @param {string} uri
* @param callbackContext
* @returns {Promise<EntryInfo>}
*/
function resolveLocalFileSystemURI(uri, callbackContext)
{
return getFilePluginUtil(callbackContext).resolveLocalFileSystemURI(uri);
}

class FileTransferError
{

Check failure on line 40 in src/electron/index.js

View workflow job for this annotation

GitHub Actions / Lint Test

Opening curly brace does not appear on the same line as controlling statement
Expand Down Expand Up @@ -225,7 +196,7 @@ const fileTransferPlugin = {
if (!checkURL(target))
return callbackContext.error(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target));

const filePath = urlToFilePath(source, callbackContext);
const filePath = _file_plugin_util.urlToFilePath(source);
if (!filePath)
return callbackContext.error(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target));

Expand Down Expand Up @@ -314,7 +285,7 @@ const fileTransferPlugin = {
if (!checkURL(target))
return callbackContext.error(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target));

const filePath = urlToFilePath(source, callbackContext);
const filePath = _file_plugin_util.urlToFilePath(source);
if (!filePath)
return callbackContext.error(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target));

Expand Down Expand Up @@ -444,7 +415,7 @@ const fileTransferPlugin = {
if (!checkURL(target))
return callbackContext.error(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target));

const filePath = urlToFilePath(source, callbackContext);
const filePath = _file_plugin_util.urlToFilePath(source);
if (!filePath)
return callbackContext.error(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target));

Expand Down Expand Up @@ -579,7 +550,7 @@ const fileTransferPlugin = {
if (!checkURL(source))
return callbackContext.error(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target));

const filePath = urlToFilePath(target, callbackContext);
const filePath = _file_plugin_util.urlToFilePath(target);
if (!filePath)
return callbackContext.error(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target));

Expand Down Expand Up @@ -673,7 +644,7 @@ const fileTransferPlugin = {
}
}

transaction.success(await resolveLocalFileSystemURI(target, callbackContext));
transaction.success(await _file_plugin_util.resolveLocalFileSystemURI(target));

} catch (error)
{
Expand Down Expand Up @@ -735,30 +706,15 @@ const plugin = function (action, args, callbackContext)
return true;
}

// backwards compatibility: attach api methods for direct access from old cordova-electron platform impl
Object.keys(fileTransferPlugin).forEach((apiMethod) =>
{
plugin[apiMethod] = (args) =>
{
return Promise.resolve((resolve, reject) =>
{
fileTransferPlugin[apiMethod](args, {
progress: (data) =>
{
console.warn("cordova-plugin-file-transfer: ignoring progress event as not supported in old plugin API", data);
},
success: (data) =>
{
resolve(data)
},
error: (data) =>
{
reject(data)
}
});
});
}
});
let _file_plugin_util;

/**
* @param {Record<string, string>} variables
* @param {(serviceName:string)=>Promise<any>} serviceLoader
* @returns {Promise<void>}
*/
plugin.init = async (variables, serviceLoader)=>{
_file_plugin_util = _file_plugin_util || (await serviceLoader('File')).util
}

module.exports = plugin;

0 comments on commit 6e3a524

Please sign in to comment.