Skip to content

Commit

Permalink
处理了 clipboard 的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
leeight committed May 31, 2015
1 parent f010a82 commit ca961b4
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 32 deletions.
2 changes: 1 addition & 1 deletion test/browser/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</form>

<fieldset id="dropzone">
<legend>选择文件或者把文件,文件夹拖进来</legend>
<legend>选择文件、把文件/文件夹拖进来、截图直接 Ctrl + V</legend>
<button id="upload"><i class="fa fa-cloud-upload"></i> Upload</button>
<button id="camera"><i class="fa fa-video-camera"></i> Record Video</button>
<input type="file" id="file" multiple hidden/>
Expand Down
2 changes: 2 additions & 0 deletions test/browser/demo/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define(function (require) {
var uploader = require('./uploader');
var fileList = require('./file-list');
var dnd = require('./dnd');
var clipboard = require('./clipboard');

var exports = {};

Expand All @@ -29,6 +30,7 @@ define(function (require) {
uploader.init();
fileList.init();
dnd.init();
clipboard.init();
};

return exports;
Expand Down
62 changes: 62 additions & 0 deletions test/browser/demo/src/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
*
* Licensed 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.
*/

define(function (require) {
var exports = {};

var $ = require('jquery');
var u = require('underscore');

var config = require('./config');
var helper = require('./helper');
var fileList = require('./file-list');

function onPaste(e) {
var options = config.getOptions();
var bucketName = options.bucketName;
var prefix = options.prefix;

var clipboardData = e.originalEvent.clipboardData;
var items = clipboardData.items || [];
u.each(items, function (item) {
var blob = item.getAsFile();
if (!blob) {
return;
}

var key = prefix + Date.now();
var options = {
'Content-Type': blob.type + '; charset=UTF-8'
};
helper.upload(bucketName, key, blob, options)
.then(u.bind(fileList.refresh, fileList));
});
}

exports.init = function () {
$(document).on('paste', onPaste);
};

return exports;
});










/* vim: set ts=4 sw=4 sts=4 tw=120: */
40 changes: 26 additions & 14 deletions test/browser/demo/src/dnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ define(function (require) {

var $ = require('jquery');
var u = require('underscore');
var async = require('async');

var config = require('./config');
var Klient = require('./client');
var fileList = require('./file-list');
var TaskManager = require('./task_manager');
var Task = require('./task');
Expand All @@ -42,16 +40,31 @@ define(function (require) {
e.preventDefault();

gTM = new TaskManager();
gTM.startup().then(function () {
console.log('gTM.done');
});

u.each(e.originalEvent.dataTransfer.items, function (item) {
var entry = item.webkitGetAsEntry();
if (entry) {
traverseFileTree(entry);
}
});
gTM.startup().then(u.bind(fileList.refresh, fileList));

var dataTransfer = e.originalEvent.dataTransfer;
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer
if (dataTransfer.items) {
// Chrome
u.each(dataTransfer.items, function (item) {
var getAsEntry = item.getAsEntry || item.webkitGetAsEntry;
if (typeof getAsEntry === 'function') {
var entry = getAsEntry.call(item);
if (entry) {
traverseFileTree(entry);
}
}
});
}
else if (dataTransfer.files) {
// Firefox
u.each(dataTransfer.files, function (file) {
var options = config.getOptions();
var bucketName = options.bucketName;
var key = options.prefix + file.name;
gTM.addTask(new Task(bucketName, key, file, options));
});
}

return false;
}
Expand All @@ -60,8 +73,7 @@ define(function (require) {
path = path || '';
if (entry.isFile) {
entry.file(function (file) {
console.log('File: ', path + file.name);

// console.log('File: ', path + file.name);
var options = config.getOptions();
var bucketName = options.bucketName;
var key = options.prefix + path + file.name;
Expand Down
30 changes: 16 additions & 14 deletions test/browser/demo/src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ define(function (require) {
var config = require('./config');

function uploadSingleFile(client, bucketName, key, blob, options) {
var ext = key.split(/\./g).pop();
var mimeType = sdk.MimeType.guess(ext);
if (/^text\//.test(mimeType)) {
mimeType += '; charset=UTF-8';
if (!options['Content-Type']) {
var ext = key.split(/\./g).pop();
var mimeType = sdk.MimeType.guess(ext);
if (/^text\//.test(mimeType)) {
mimeType += '; charset=UTF-8';
}
options['Content-Type'] = mimeType;
}
u.extend(options, {
'Content-Type': mimeType
});

return client.putObjectFromBlob(bucketName, key, blob, options);
}
Expand Down Expand Up @@ -90,13 +90,15 @@ define(function (require) {
}

function uploadSuperFile(client, bucketName, key, blob, options) {
var ext = key.split(/\./g).pop();
// Firefox在POST的时候,Content-Type 一定会有Charset的,因此
// 这里不管3721,都加上.
var mimeType = sdk.MimeType.guess(ext) + '; charset=UTF-8';
u.extend(options, {
'Content-Type': mimeType
});
if (!options['Content-Type']) {
var ext = key.split(/\./g).pop();
// Firefox在POST的时候,Content-Type 一定会有Charset的,因此
// 这里不管3721,都加上.
var mimeType = sdk.MimeType.guess(ext) + '; charset=UTF-8';
u.extend(options, {
'Content-Type': mimeType
});
}

var uploadId = null;
return client.initiateMultipartUpload(bucketName, key, options)
Expand Down
8 changes: 5 additions & 3 deletions test/browser/demo/src/task_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ define(function (require) {
var task = this.tasks.shift();
if (task && typeof task.run === 'function') {
this.running ++;
task.run()
.done(u.bind(this.runTaskSuccess, this))
.catch(u.bind(this.runTaskFailure, this));
task.run().then(
u.bind(this.runTaskSuccess, this),
u.bind(this.runTaskFailure, this)
);
}
};

Expand All @@ -78,6 +79,7 @@ define(function (require) {
TaskManager.prototype.runTaskSuccess = function () {
this.running--;
this.runNext();

};

/**
Expand Down

0 comments on commit ca961b4

Please sign in to comment.