Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 5be9ba1

Browse files
authored
feat(uploader.basic.js): more flexible server endpoint support (#1939)
* Local dev/testing ports 3000/3001 clash with my local env, and possibly others - moving to 4000/4001. * returned onUploadChunk promise can override method, params, headers, & url * promissory onUpload callback * always ensure test server are killed either on test start or stop * don't try to kill test server on CI before tests start * option to allow upload responses without { "success": true } * allow default params to be omitted from upload requests * don't fail upload w/ non-JSON response when requireSuccessJson = false * more flexible chunking.success request support * add .editorconfig (can't believe this didn't exist until now) * Allow custom resume keys and data to be specified. * include customResumeData in return value of getResumableFilesData API method * add isResumable public API method * introduce chunking.success.resetOnStatus to allow FU to reset a file based on chunking.success response code * new API method: isResumable(id) * Allow onUpload resolved Promise to pause the file. Use case: When onUpload is called, you make a request to your server to see if the file already exists. If it does, you want to let your user decide if they want to overwrite the file, or cancel the upload entirely. While waiting for user input you don't want to hold a spot in the upload queue. If the user decided to overwrite the file, call the `continueUpload` API method. * Allow per-file chunk sizes to be specified. chunking.partSize now accepts a function, which passes the file ID and size * feat(beforeUnload): new option to turn off beforeUnload alert during uploads * feat(features.js): auto-detect folder support * Allow access to Blob when file status is still SUBMITTING * docs: options, API, and events doc updates * added qq.status.UPLOAD_FINALIZING - don't cancel or pause in this state closes #848 closes #1697 closes #1755 closes #1325 closes #1647 closes #1703
1 parent 007c4de commit 5be9ba1

31 files changed

+1354
-201
lines changed

.editorconfig

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
# Unix-style newlines with a newline ending every file
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
9+
# Matches multiple files with brace expansion notation
10+
# Set default charset
11+
[{*.js}]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 4
15+
16+
# Tab indentation (no size specified)
17+
[Makefile]
18+
indent_style = tab
19+
20+
# Matches the exact files either package.json or .travis.yml
21+
[{package.json,.travis.yml}]
22+
indent_style = space
23+
indent_size = 2

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.*
2+
!.editorconfig
23
*.ipr
34
*~
45
.*.sw[a-z]

Makefile

+11-5
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,27 @@ start-test-resources-server: test-resources-server.PID
322322
start-root-server: root-server.PID
323323

324324
test-resources-server.PID:
325-
$(npm-bin)/static test/unit/resources -H '{"Access-Control-Allow-Origin": "*"}' -p 3000 & echo $$! > $@
325+
$(npm-bin)/static test/unit/resources -H '{"Access-Control-Allow-Origin": "*"}' -p 4000 & echo $$! > $@
326326

327327
root-server.PID:
328-
$(npm-bin)/static . -p 3001 & echo $$! > $@
328+
$(npm-bin)/static . -p 4001 & echo $$! > $@
329329

330330
stop-test-resources-server: test-resources-server.PID
331331
kill `cat $<` && rm $<
332332

333333
stop-root-server: root-server.PID
334334
kill `cat $<` && rm $<
335335

336-
test: start-test-resources-server start-root-server build-all-ui
336+
test:
337+
$(MAKE) stop-test-resources-server
338+
$(MAKE) stop-root-server
339+
$(MAKE) start-test-resources-server
340+
$(MAKE) start-root-server
341+
$(MAKE) build-all-ui
337342
$(npm-bin)/karma start config/karma.conf.js
338-
make stop-test-resources-server
339-
make stop-root-server
343+
$(MAKE) stop-test-resources-server
344+
$(MAKE) stop-root-server
345+
.PHONY: test
340346

341347
zip: zip-traditional zip-s3 zip-azure zip-all
342348

client/js/azure/azure.xhr.upload.handler.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ qq.azure.XhrUploadHandler = function(spec, proxy) {
139139
}
140140

141141
qq.extend(this, {
142-
uploadChunk: function(id, chunkIdx) {
142+
uploadChunk: function(params) {
143+
var chunkIdx = params.chunkIdx;
144+
var id = params.id;
145+
143146
var promise = new qq.Promise();
144147

145148
getSignedUrl(id, chunkIdx).then(

client/js/features.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ qq.supportedFeatures = (function() {
3838
return supported;
3939
}
4040

41-
//only way to test for Filesystem API support since webkit does not expose the DataTransfer interface
42-
function isChrome21OrHigher() {
43-
return (qq.chrome() || qq.opera()) &&
44-
navigator.userAgent.match(/Chrome\/[2][1-9]|Chrome\/[3-9][0-9]/) !== undefined;
45-
}
46-
4741
//only way to test for complete Clipboard API support at this time
4842
function isChrome14OrHigher() {
4943
return (qq.chrome() || qq.opera()) &&
@@ -109,7 +103,13 @@ qq.supportedFeatures = (function() {
109103

110104
supportsFileDrop = supportsAjaxFileUploading && isDragAndDropSupported();
111105

112-
supportsFolderDrop = supportsFileDrop && isChrome21OrHigher();
106+
// adapted from https://stackoverflow.com/a/23278460/486979
107+
supportsFolderDrop = supportsFileDrop && (function() {
108+
var input = document.createElement("input");
109+
110+
input.type = "file";
111+
return !!("webkitdirectory" in (input || document.querySelectorAll("input[type=file]")[0]));
112+
}());
113113

114114
supportsChunking = supportsAjaxFileUploading && qq.isFileChunkingSupported();
115115

client/js/s3/s3.xhr.upload.handler.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,10 @@ qq.s3.XhrUploadHandler = function(spec, proxy) {
538538
}
539539
},
540540

541-
start: function(id, optChunkIdx) {
541+
start: function(params) {
542+
var id = params.id;
543+
var optChunkIdx = params.chunkIdx;
544+
542545
var promise = new qq.Promise();
543546

544547
upload.key.promise(id).then(function() {
@@ -587,7 +590,9 @@ qq.s3.XhrUploadHandler = function(spec, proxy) {
587590

588591
qq.extend(this, {
589592
uploadChunk: upload.start,
590-
uploadFile: upload.start
593+
uploadFile: function(id) {
594+
return upload.start({ id: id });
595+
}
591596
});
592597

593598
qq.extend(this, new qq.XhrUploadHandler({

client/js/traditional/all-chunks-done.ajax.requester.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@ qq.traditional.AllChunksDoneAjaxRequester = function(o) {
1010
"use strict";
1111

1212
var requester,
13-
method = "POST",
1413
options = {
1514
cors: {
1615
allowXdr: false,
1716
expected: false,
1817
sendCredentials: false
1918
},
2019
endpoint: null,
21-
log: function(str, level) {}
20+
log: function(str, level) {},
21+
method: "POST"
2222
},
2323
promises = {},
2424
endpointHandler = {
2525
get: function(id) {
26+
if (qq.isFunction(options.endpoint)) {
27+
return options.endpoint(id);
28+
}
29+
2630
return options.endpoint;
2731
}
2832
};
@@ -31,8 +35,9 @@ qq.traditional.AllChunksDoneAjaxRequester = function(o) {
3135

3236
requester = qq.extend(this, new qq.AjaxRequester({
3337
acceptHeader: "application/json",
34-
validMethods: [method],
35-
method: method,
38+
contentType: options.jsonPayload ? "application/json" : "application/x-www-form-urlencoded",
39+
validMethods: [options.method],
40+
method: options.method,
3641
endpointStore: endpointHandler,
3742
allowXRequestedWithAndCacheControl: false,
3843
cors: options.cors,
@@ -60,8 +65,8 @@ qq.traditional.AllChunksDoneAjaxRequester = function(o) {
6065
promises[id] = promise;
6166

6267
requester.initTransport(id)
63-
.withParams(params)
64-
.withHeaders(headers)
68+
.withParams(options.params(id) || params)
69+
.withHeaders(options.headers(id) || headers)
6570
.send(xhr);
6671

6772
return promise;

0 commit comments

Comments
 (0)