Cordova plugin file und file-transfer geadded

This commit is contained in:
2016-01-08 00:06:37 +01:00
parent 23a833a31b
commit 36c7e2c8e2
234 changed files with 49402 additions and 81 deletions

View File

@@ -0,0 +1,34 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
FILESYSTEM_PROTOCOL = "cdvfile";
module.exports = {
__format__: function(fullPath) {
if (this.name === 'content') {
return 'content:/' + fullPath;
}
var path = ('/' + this.name + (fullPath[0] === '/' ? '' : '/') + encodeURI(fullPath)).replace('//','/');
return FILESYSTEM_PROTOCOL + '://localhost' + path;
}
};

View File

@@ -0,0 +1,135 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
var exec = require('cordova/exec'),
FileError = require('./FileError'),
ProgressEvent = require('./ProgressEvent');
function write(data) {
var that=this;
var supportsBinary = (typeof window.Blob !== 'undefined' && typeof window.ArrayBuffer !== 'undefined');
var isBinary;
// Check to see if the incoming data is a blob
if (data instanceof File || (supportsBinary && data instanceof Blob)) {
var fileReader = new FileReader();
fileReader.onload = function() {
// Call this method again, with the arraybuffer as argument
FileWriter.prototype.write.call(that, this.result);
};
if (supportsBinary) {
fileReader.readAsArrayBuffer(data);
} else {
fileReader.readAsText(data);
}
return;
}
// Mark data type for safer transport over the binary bridge
isBinary = supportsBinary && (data instanceof ArrayBuffer);
// Throw an exception if we are already writing a file
if (this.readyState === FileWriter.WRITING) {
throw new FileError(FileError.INVALID_STATE_ERR);
}
// WRITING state
this.readyState = FileWriter.WRITING;
var me = this;
// If onwritestart callback
if (typeof me.onwritestart === "function") {
me.onwritestart(new ProgressEvent("writestart", {"target":me}));
}
if (data instanceof ArrayBuffer || data.buffer instanceof ArrayBuffer) {
data = new Uint8Array(data);
var binary = "";
for (var i = 0; i < data.byteLength; i++) {
binary += String.fromCharCode(data[i]);
}
data = binary;
}
var prefix = "file://localhost";
var path = this.localURL;
if (path.substr(0, prefix.length) == prefix) {
path = path.substr(prefix.length);
}
// Write file
exec(
// Success callback
function(r) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileWriter.DONE) {
return;
}
// position always increases by bytes written because file would be extended
me.position += r;
// The length of the file is now where we are done writing.
me.length = me.position;
// DONE state
me.readyState = FileWriter.DONE;
// If onwrite callback
if (typeof me.onwrite === "function") {
me.onwrite(new ProgressEvent("write", {"target":me}));
}
// If onwriteend callback
if (typeof me.onwriteend === "function") {
me.onwriteend(new ProgressEvent("writeend", {"target":me}));
}
},
// Error callback
function(e) {
// If DONE (cancelled), then don't do anything
if (me.readyState === FileWriter.DONE) {
return;
}
// DONE state
me.readyState = FileWriter.DONE;
// Save error
me.error = new FileError(e);
// If onerror callback
if (typeof me.onerror === "function") {
me.onerror(new ProgressEvent("error", {"target":me}));
}
// If onwriteend callback
if (typeof me.onwriteend === "function") {
me.onwriteend(new ProgressEvent("writeend", {"target":me}));
}
}, "File", "write", [path, data, this.position, isBinary]);
};
module.exports = {
write: write
};
require("cordova/exec/proxy").add("FileWriter", module.exports);

View File

@@ -0,0 +1,53 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
var fsMap = null;
var FileSystem = require('./FileSystem');
var LocalFileSystem = require('./LocalFileSystem');
var exec = require('cordova/exec');
var requestFileSystem = function(type, size, successCallback) {
var success = function(file_system) {
if (file_system) {
if (successCallback) {
fs = new FileSystem(file_system.name, file_system.root);
successCallback(fs);
}
}
};
exec(success, null, "File", "requestFileSystem", [type, size]);
};
require('./fileSystems').getFs = function(name, callback) {
if (fsMap) {
callback(fsMap[name]);
} else {
requestFileSystem(LocalFileSystem.PERSISTENT, 1, function(fs) {
requestFileSystem(LocalFileSystem.TEMPORARY, 1, function(tmp) {
fsMap = {};
fsMap[tmp.name] = tmp;
fsMap[fs.name] = fs;
callback(fsMap[name]);
});
});
}
};