mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-20 18:18:56 +00:00
feature(directory) add ability to upload directories via drag n drop in Chrome
This commit is contained in:
parent
2790799b98
commit
44531adfc6
27 changed files with 1137 additions and 4 deletions
|
|
@ -23,7 +23,8 @@ var CloudCmd;
|
|||
'client',
|
||||
client + 'buffer',
|
||||
client + 'listeners',
|
||||
client + 'key'
|
||||
client + 'key',
|
||||
client + 'directory'
|
||||
].map(function(name) {
|
||||
return lib + name;
|
||||
}),
|
||||
|
|
|
|||
116
lib/client/directory.js
Normal file
116
lib/client/directory.js
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/* global CloudCmd */
|
||||
/* global DOM */
|
||||
/* global CloudFunc */
|
||||
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports)
|
||||
module.exports = uploadDirectory;
|
||||
else
|
||||
DOM.uploadDirectory = uploadDirectory;
|
||||
|
||||
function uploadDirectory(items) {
|
||||
var entries,
|
||||
Images = DOM.Images,
|
||||
Info = DOM.CurrentInfo,
|
||||
load = DOM.load,
|
||||
url = '',
|
||||
array = [
|
||||
'findit',
|
||||
'philip'
|
||||
];
|
||||
|
||||
Images.show('top');
|
||||
|
||||
entries = [].map.call(items, function(item) {
|
||||
return item.webkitGetAsEntry();
|
||||
});
|
||||
|
||||
if (!window.Emitify)
|
||||
array.unshift('emitify');
|
||||
|
||||
array = array.map(function(name) {
|
||||
var result = [
|
||||
'/modules/' + name,
|
||||
'/lib/' + name,
|
||||
'.js'
|
||||
].join('');
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
if (!window.fetch)
|
||||
array.unshift('/modules/fetch/fetch.js');
|
||||
|
||||
url = CloudCmd.join(array);
|
||||
|
||||
load.js(url, function() {
|
||||
var path = Info.dirPath
|
||||
.replace(/\/$/, ''),
|
||||
|
||||
uploader;
|
||||
|
||||
uploader = window.philip(entries, function(type, name, data, callback) {
|
||||
var full = path + name;
|
||||
switch(type) {
|
||||
case 'file':
|
||||
uploadFile(full, data, callback);
|
||||
break;
|
||||
|
||||
case 'directory':
|
||||
uploadDir(full, callback);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
uploader.on('error', function(error) {
|
||||
alert(error);
|
||||
uploader.abort();
|
||||
});
|
||||
|
||||
uploader.on('progress', function(count) {
|
||||
Images.setProgress(count);
|
||||
Images.show('top');
|
||||
});
|
||||
|
||||
uploader.on('end', function() {
|
||||
CloudCmd.refresh();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function uploadFile(url, data, callback) {
|
||||
upload(url, data, callback);
|
||||
}
|
||||
|
||||
function uploadDir(url, callback) {
|
||||
upload(url + '?dir', null, callback);
|
||||
}
|
||||
|
||||
function upload(url, body, callback) {
|
||||
var prefix = CloudCmd.PREFIX,
|
||||
apiURL = prefix + CloudFunc.apiURL,
|
||||
api = apiURL + '/fs',
|
||||
xhr = new XMLHttpRequest();
|
||||
|
||||
url = encodeURI(url);
|
||||
url = url.replace('#', '%23');
|
||||
|
||||
xhr.open('put', api + url, true);
|
||||
xhr.onreadystatechange = function() {
|
||||
var error,
|
||||
over = xhr.readyState === 4,
|
||||
OK = 200;
|
||||
|
||||
if (over) {
|
||||
if (xhr.status !== OK)
|
||||
error = Error(xhr.responseText);
|
||||
|
||||
callback(error);
|
||||
}
|
||||
};
|
||||
xhr.send(body);
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
@ -265,10 +265,15 @@ var Util, DOM, CloudFunc, CloudCmd;
|
|||
});
|
||||
},
|
||||
onDrop = function(event) {
|
||||
var files = event.dataTransfer.files;
|
||||
var files = event.dataTransfer.files,
|
||||
items = event.dataTransfer.items;
|
||||
|
||||
event.preventDefault();
|
||||
DOM.uploadFiles(files);
|
||||
|
||||
if (items && items[0].webkitGetAsEntry)
|
||||
DOM.uploadDirectory(items);
|
||||
else
|
||||
DOM.uploadFiles(files);
|
||||
},
|
||||
/**
|
||||
* In Mac OS Chrome dropEffect = 'none'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue