Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Valums committed Sep 12, 2010
1 parent f4bf836 commit e3377a6
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 6 deletions.
24 changes: 18 additions & 6 deletions client/fileuploader.js
Expand Up @@ -867,6 +867,8 @@ qq.UploadHandlerAbstract = function(o){
qq.extend(this._options, o);

this._queue = [];
// params for files in queue
this._params = [];
};
qq.UploadHandlerAbstract.prototype = {
/**
Expand All @@ -878,11 +880,16 @@ qq.UploadHandlerAbstract.prototype = {
* Sends the file identified by id and additional query params to the server
*/
upload: function(id, params){
var len = this._queue.push(id);
// too many active uploads, wait...
if (len > this._options.maxConnections) return;

this._upload(id, params);
var len = this._queue.push(id);

var copy = {};
qq.extend(copy, params);
this._params[id] = copy;

// if too many active uploads, wait...
if (len <= this._options.maxConnections){
this._upload(id, this._params[id]);
}
},
/**
* Cancels file upload by id
Expand Down Expand Up @@ -933,7 +940,8 @@ qq.UploadHandlerAbstract.prototype = {
var max = this._options.maxConnections;

if (this._queue.length >= max){
this._upload(this._queue[max-1]);
var nextId = this._queue[max-1];
this._upload(nextId, this._params[nextId]);
}
}
};
Expand Down Expand Up @@ -1128,6 +1136,10 @@ qq.extend(qq.UploadHandlerXhr.prototype, {
* Returns id to use with upload, cancel
**/
add: function(file){
if (!(file instanceof File)){
throw new Error('Passed obj in not a File (in qq.UploadHandlerXhr)');
}

return this._files.push(file) - 1;
},
getName: function(id){
Expand Down
31 changes: 31 additions & 0 deletions tests/action-handler-queue-test.php
@@ -0,0 +1,31 @@
<?php

sleep(4);

$fileName;

if (isset($_GET['qqfile'])){
$fileName = $_GET['qqfile'];

// xhr request
$headers = apache_request_headers();
if ((int)$headers['Content-Length'] == 0){
die ('{error: "content length is zero"}');
}
} elseif (isset($_FILES['qqfile'])){
$fileName = basename($_FILES['qqfile']['name']);

// form request
if ($_FILES['qqfile']['size'] == 0){
die ('{error: "file size is zero"}');
}
} else {
die ('{error: "file not passed"}');
}

if (count($_GET)){
$_GET['success'] = true;
echo json_encode(array_merge($_GET));
} else {
die ('{error: "query params not passed"}');
}
81 changes: 81 additions & 0 deletions tests/test-handler-queue.htm
@@ -0,0 +1,81 @@
<!DOCTYPE HTML>
<html>
<head>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>

<link href="qunit/qunit/qunit.css" rel="stylesheet" type="text/css" media="screen" />
<script src="qunit/qunit/qunit.js" type="text/javascript"></script>

<script src="../client/fileuploader.js" type="text/javascript" ></script>
<script>
jQuery(function(){

function getHandler(){
if(qq.UploadHandlerXhr.isSupported()){
return qq.UploadHandlerXhr;
} else {
return qq.UploadHandlerForm;
}
}

asyncTest("upload", function() {
expect(2);

var data = {stringOne: 'rtdfghdfhfh',stringTwo: 'dfsgsdfgsdg',stringThree: 'dfsgfhdfhdg'};
var savedId;

var uploadHandler = new (getHandler())({
action: 'action-handler-queue-test.php',
maxConnections: 1,
onComplete: function(id, fileName, response){
if (!response.success){
ok(false, 'server did not receive file')
return;
}

delete response.success;
delete response.qqfile;

same(response, data, 'server received file and data');
}
});


$('#testinput1, #testinput2').change(upload);

function upload(){
setTimeout(start, 9000);

var file = this;
if (uploadHandler instanceof qq.UploadHandlerXhr){
file = this.files[0];
}
var id = uploadHandler.add(file);
uploadHandler.upload(id, data);
}


});
});
</script>
</head>
<body>
<h1 id="qunit-header">File uploader tests</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>


<p>
Please select a file for each input below,
should be less than 4 sec, between selection.
</p>


<input id="testinput1" type="file">
<input id="testinput2" type="file">

</body>
</html>


0 comments on commit e3377a6

Please sign in to comment.