2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // TODO: remove when added to public cordova repository -> begin
18 var plugin_name = 'cordova-plugin-file-transfer.tizen.FileTransfer';
20 cordova.define(plugin_name, function(require, exports, module) {
21 // TODO: remove -> end
23 function getParentPath(filePath) {
24 var pos = filePath.lastIndexOf('/');
25 return filePath.substring(0, pos + 1);
28 function getFileName(filePath) {
29 var pos = filePath.lastIndexOf('/');
30 return filePath.substring(pos + 1);
33 function TizenErrCodeToErrCode(err_code) {
35 case WebAPIException.NOT_FOUND_ERR:
36 return FileTransferError.FILE_NOT_FOUND_ERR;
38 case WebAPIException.URL_MISMATCH_ERR:
39 return FileTransferError.INVALID_URL_ERR;
41 case WebAPIException.NETWORK_ERR:
42 return FileTransferError.CONNECTION_ERR;
44 case WebAPIException.ABORT_ERR:
45 return FileTransferError.ABORT_ERR;
48 return FileTransferError.NOT_MODIFIED_ERR;
52 function FileErrorCodeToErrCode(err_code) {
54 case FileError.SECURITY_ERR:
55 return FileTransferError.ABORT_ERR;
57 return FileTransferError.FILE_NOT_FOUND_ERR;
61 function checkURL(url) {
62 return url.indexOf(' ') === -1;
68 var filePrefix = 'file://';
71 upload: function(successCallback, errorCallback, args) {
72 var filePath = args[0],
74 fileKey = args[2] || 'file',
75 fileName = args[3] || 'image.jpg',
76 mimeType = args[4] || 'image/jpeg',
78 trustAllHosts = args[6], // not used
79 chunkedMode = args[7],
82 httpMethod = args[10] || 'POST';
84 if (0 !== filePath.indexOf(filePrefix)) {
85 filePath = filePrefix + filePath;
88 var fail = function(code, status, response) {
89 uploads[id] && delete uploads[id];
90 var error = new FileTransferError(code, filePath, server, status, response);
91 errorCallback && errorCallback(error);
94 if (!checkURL(server)) {
95 fail(FileTransferError.INVALID_URL_ERR);
99 function successCB(entry) {
101 entry.file(function(file) {
102 function uploadFile(blobFile) {
103 var fd = new FormData();
105 fd.append(fileKey, blobFile, fileName);
107 for (var prop in params) {
108 if(params.hasOwnProperty(prop)) {
109 fd.append(prop, params[prop]);
112 var xhr = uploads[id] = new XMLHttpRequest();
114 xhr.open(httpMethod, server);
117 for (var header in headers) {
118 if (headers.hasOwnProperty(header)) {
119 xhr.setRequestHeader(header, headers[header]);
123 xhr.onload = function(evt) {
124 if (xhr.status === 200) {
125 uploads[id] && delete uploads[id];
127 bytesSent: file.size,
128 responseCode: xhr.status,
129 response: xhr.response
131 } else if (xhr.status === 404) {
132 fail(FileTransferError.INVALID_URL_ERR, this.status, this.response);
134 fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
138 xhr.ontimeout = function(evt) {
139 fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
142 xhr.onerror = function() {
143 fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
146 xhr.onabort = function () {
147 fail(FileTransferError.ABORT_ERR, this.status, this.response);
150 xhr.upload.onprogress = function (e) {
156 // Special case when transfer already aborted, but XHR isn't sent.
157 // In this case XHR won't fire an abort event, so we need to check if transfers record
158 // isn't deleted by filetransfer.abort and if so, call XHR's abort method again
167 fail(FileTransferError.CONNECTION_ERR);
172 function errorCB(error) {
173 fail(FileErrorCodeToErrCode(error.code));
176 resolveLocalFileSystemURL(filePath, successCB, errorCB);
178 download: function(successCallback, errorCallback, args) {
181 trustAllHosts = args[2], // not used
185 if (!checkURL(url)) {
186 errorCallback(new FileTransferError(FileTransferError.INVALID_URL_ERR, url, filePath));
190 var dirPath = getParentPath(filePath);
191 var fileName = getFileName(filePath);
193 var xhr = downloads[id] = new XMLHttpRequest();
195 function fail(code, body) {
196 delete downloads[id];
197 errorCallback(new FileTransferError(code,
205 xhr.addEventListener('progress', function (evt) {
206 successCallback(evt);
209 xhr.addEventListener('abort', function (evt) {
210 fail(FileTransferError.ABORT_ERR, xhr.response);
213 xhr.addEventListener('error', function (evt) {
214 fail(FileTransferError.CONNECTION_ERR, xhr.response);
217 xhr.addEventListener('load', function (evt) {
218 if ((xhr.status === 200 || xhr.status === 0) && xhr.response) {
220 tizen.filesystem.resolve(dirPath, function (dir) {
222 fail(FileTransferError.FILE_NOT_FOUND_ERR);
226 function writeFile(dir) {
227 var file = dir.createFile(fileName);
232 stream.writeBytes(Array.prototype.slice.call(new Uint8Array(xhr.response)));
234 delete downloads[id];
236 resolveLocalFileSystemURL(
238 function (fileEntry) {
239 fileEntry.filesystemName = fileEntry.filesystem.name;
240 successCallback(fileEntry);
242 fail(TizenErrCodeToErrCode(err.code));
245 fail(TizenErrCodeToErrCode(err.code));
259 fail(TizenErrCodeToErrCode(err.code));
263 fail(FileTransferError.ABORT_ERR);
265 } else if (xhr.status === 404) {
266 fail(FileTransferError.INVALID_URL_ERR,
267 String.fromCharCode.apply(null, new Uint8Array(xhr.response)));
269 fail(FileTransferError.CONNECTION_ERR,
270 String.fromCharCode.apply(null, new Uint8Array(xhr.response)));
274 xhr.open('GET', url, true);
275 xhr.responseType = 'arraybuffer';
277 for (var header in headers) {
278 if (headers.hasOwnProperty(header)) {
279 xhr.setRequestHeader(header, headers[header]);
284 abort: function(successCallback, errorCallback, args) {
289 } else if (downloads[id]) {
290 downloads[id].abort();
291 delete downloads[id];
293 console.warn('Unknown file transfer ID: ' + id);
298 require("cordova/exec/proxy").add("FileTransfer", exports);
300 console.log('Loaded cordova.file-transfer API');
302 // TODO: remove when added to public cordova repository -> begin
304 // TODO: remove -> end