Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to expose additional path over http://meteor.local/<prefix> on Cordova #5203

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions packages/meteor/cordova_environment.js
Expand Up @@ -6,3 +6,94 @@
*/
Meteor.isCordova = true;

Meteor.Cordova = Meteor.Cordova || {};

Meteor.Cordova._additionalDataPath = null;
Meteor.Cordova._additionalDataUrlPrefix = 'data';

Meteor.Cordova._trimFileProtocol = function(path) {
var fileProtocol = 'file://';

if (path.substr(0, fileProtocol.length) === fileProtocol)
path = path.substr(fileProtocol.length);

return path;
};

/**
* @memberOf Meteor
* @summary Sets an additional path on the mobile device that will be available in the Cordova environment using the url http://meteor.local/data
* @locus Client
* @param {String} [path] An absolute, valid path to a directory on the mobile device
*/
Meteor.Cordova.setAdditionalDataPath = function(path) {

var cordovaUpdate = cordova && cordova.plugins && cordova.plugins.CordovaUpdate;
if (! cordovaUpdate) {
throw new Error('No CordovaUpdate plugin found. Is this running in Cordova?');
}

if (! path || path.length === 0) {
throw new Error('Path not specified.');
}

path = Meteor.Cordova._trimFileProtocol(path);

if (path.substr(0,1) !== '/') {
throw new Error('Relative paths are not supported.');
}

cordovaUpdate.setAdditionalDataPath(
path,
function() { Meteor.Cordova._additionalDataPath = path; },
function(error) { throw new Error(error); }
);
};

/**
* @memberOf Meteor
* @summary Sets the url prefix that the additional data path is available on so that the url looks like http://meteor.local/<prefix>. The default prefix is `data`.
* @locus Client
* @param {String} [prefix] Must be alphaumeric and different from `plugins`
*/
Meteor.Cordova.setAdditionalDataUrlPrefix = function(prefix) {

var cordovaUpdate = cordova && cordova.plugins && cordova.plugins.CordovaUpdate;
if (! cordovaUpdate) {
throw new Error('No CordovaUpdate plugin found. Is this running in Cordova?');
}

if (! prefix || prefix.length === 0) {
throw new Error('Prefix not specified.');
}

cordovaUpdate.setAdditionalDataUrlPrefix(
prefix,
function() { Meteor.Cordova._additionalDataUrlPrefix = prefix; },
function(error) { throw new Error(error); }
);
};

/**
* @memberOf Meteor
* @summary Super simple method that returns the proper url for accessing a file that is in the directory that was previously set as an additional data path.
* @locus Client
* @param {String} [path] An absolute, valid path to a file on the mobile device
*/
Meteor.Cordova.getUrlForPath = function(path) {
if (! path) {
throw new Error('Path not provided.');
}

if (! Meteor.Cordova._additionalDataPath) {
throw new Error('Additional data path not set. Use Meteor.setAdditionalDataPath first.');
}

path = Meteor.Cordova._trimFileProtocol(path);

if (path.substr(0, Meteor.Cordova._additionalDataPath.length) !== Meteor.Cordova._additionalDataPath) {
throw new Error('Path must be an absolute path and start with the path previously set with Meteor.setAdditionalDataPath.');
}

return "http://meteor.local/" + Meteor.Cordova._additionalDataUrlPrefix + '/' + path.substr(Meteor.Cordova._additionalDataPath.length);
};
1 change: 0 additions & 1 deletion packages/meteor/cordova_environment_test.js
Expand Up @@ -3,4 +3,3 @@ Tinytest.add("environment - cordova basics", function (test) {
test.isTrue(Meteor.isClient);
test.isTrue(Meteor.isCordova);
});

2 changes: 2 additions & 0 deletions packages/webapp/package.js
Expand Up @@ -17,6 +17,8 @@ Cordova.depends({
'cordova-plugin-legacy-whitelist': '1.1.0',
// the cordova plugin built by Meteor Core team that "emulates a server" on
// the mobile device. Serving the files and checking for the HCP updates.

// Update will be needed!
'com.meteor.cordova-update': 'https://github.com/meteor/com.meteor.cordova-update/tarball/92fe99b7248075318f6446b288995d4381d24cd2'
});

Expand Down