Skip to content

Commit 467c1e4

Browse files
committed
feat: adding upload support
1 parent 4055e8f commit 467c1e4

11 files changed

Lines changed: 556 additions & 239 deletions

File tree

dist/workfront.js

Lines changed: 333 additions & 203 deletions
Large diffs are not rendered by default.

dist/workfront.min.js

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/node/image.jpg

17.7 KB
Loading

examples/node/upload-an-image.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2015 Workfront
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
17+
/**
18+
* Logs in, then uploads an image and attaches it to a task
19+
*/
20+
21+
var ApiFactory = require('./../../').ApiFactory;
22+
var util = require('util');
23+
var fs = require('fs');
24+
25+
var stream = fs.createReadStream('./image.jpg');
26+
27+
var instance = ApiFactory.getInstance({
28+
url: 'http://localhost:8080',
29+
version: '4.0'
30+
});
31+
32+
util.log('Logging in ...');
33+
instance.login('admin@user.attask', 'user').then(
34+
function() {
35+
util.log('Uploading a sweet picture...');
36+
instance.upload(stream, {filename: 'sweet.jpg', contentType: 'image/jpeg'}).then(
37+
function(data) {
38+
util.log('Upload success. Received data:');
39+
console.log(util.inspect(data, {colors:true}));
40+
41+
instance.create('DOCU', {
42+
name: 'sweet.jpg',
43+
handle: data.handle,
44+
docObjCode: 'TASK',
45+
46+
//Obviously this will only work with a real TASK ID
47+
objID: '561ffe36000006ee9f8453a6c921d636'
48+
}).then(
49+
function(data) {
50+
util.log('Document creation success. Received data:');
51+
console.log(util.inspect(data, {colors:true}));
52+
},
53+
function(error) {
54+
util.log('Document creation failure. Received data:');
55+
console.log(util.inspect(error, {colors:true}));
56+
}
57+
);
58+
},
59+
function(error) {
60+
util.log('Upload failure. Received data:');
61+
console.log(util.inspect(error, {colors:true}));
62+
}
63+
);
64+
},
65+
function(error) {
66+
util.log('Login failure. Received data:');
67+
console.log(util.inspect(error, {colors:true}));
68+
}
69+
);

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ gulp.task('build', 'Generates browser-ready version for API in '+BUILD_DIR, ['cl
6262
}
6363
)
6464
.ignore('promise/polyfill')
65+
.exclude('./plugins/upload')
6566
.bundle()
6667
.pipe(source('workfront.js'))
6768
.pipe(buffer())

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "A Workfront API for the Node.js and the Web",
55
"main": "index.js",
66
"dependencies": {
7+
"form-data": "0.2.0",
78
"promise": "^6.0.1"
89
},
910
"devDependencies": {

src/Api.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@ require('./plugins/remove')(Api);
9090
require('./plugins/report')(Api);
9191
require('./plugins/count')(Api);
9292
require('./plugins/copy')(Api);
93-
require('./plugins/upload')(Api);
9493
require('./plugins/execute')(Api);
9594
require('./plugins/namedQuery')(Api);
9695
require('./plugins/metadata')(Api);
9796
require('./plugins/apiKey')(Api);
9897

99-
module.exports = Api;
98+
if(typeof(window)==='undefined'){
99+
//These plugins only work in node
100+
require('./plugins/upload')(Api);
101+
}
102+
103+
module.exports = Api;

src/plugins/request.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ module.exports = function(Api) {
2727
return method !== Api.Methods.GET && method !== Api.Methods.PUT;
2828
};
2929

30+
Api.prototype._handleResponse = function(resolve, reject){
31+
return function (response) {
32+
var body = '';
33+
if (typeof response.setEncoding === 'function') {
34+
response.setEncoding('utf8');
35+
}
36+
response.on('data', function (chunk) {
37+
body += chunk;
38+
});
39+
response.on('end', function () {
40+
var data;
41+
try {
42+
data = JSON.parse(body);
43+
}
44+
catch(e) {
45+
reject(body);
46+
return;
47+
}
48+
if (data.error) {
49+
reject(data);
50+
} else {
51+
resolve(data.data);
52+
}
53+
});
54+
};
55+
};
56+
3057
Api.prototype.request = function(path, params, fields, method) {
3158
fields = fields || [];
3259
if (typeof fields === 'string') {
@@ -38,7 +65,7 @@ module.exports = function(Api) {
3865

3966
var options = {},
4067
alwaysUseGet = this.httpOptions.alwaysUseGet;
41-
68+
4269
util._extend(options, this.httpOptions);
4370
if (alwaysUseGet) {
4471
params.method = method;
@@ -72,36 +99,12 @@ module.exports = function(Api) {
7299
var httpTransport = this.httpTransport;
73100

74101
return new Promise(function (resolve, reject) {
75-
var request = httpTransport.request(options, function (response) {
76-
var body = '';
77-
if (typeof response.setEncoding === 'function') {
78-
response.setEncoding('utf8');
79-
}
80-
response.on('data', function (chunk) {
81-
body += chunk;
82-
});
83-
response.on('end', function () {
84-
var data;
85-
try {
86-
data = JSON.parse(body);
87-
}
88-
catch(e) {
89-
reject(body);
90-
return;
91-
}
92-
if (data.error) {
93-
reject(data);
94-
} else {
95-
resolve(data.data);
96-
}
97-
});
98-
});
102+
var request = httpTransport.request(options, this._handleResponse(resolve, reject));
99103
request.on('error', reject);
100104
if (!alwaysUseGet && params && requestHasData(options.method)) {
101105
request.write(params);
102106
}
103107
request.end();
104-
});
108+
}.bind(this));
105109
};
106110
};
107-

src/plugins/upload.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17+
var FormData = require('form-data'),
18+
util = require('util');
19+
1720
/**
1821
* @author Hovhannes Babayan <bhovhannes at gmail dot com>
22+
* @author Matt Winchester <mwinche at gmail dot com>
1923
*/
2024
module.exports = function(Api) {
2125
/**
@@ -24,8 +28,32 @@ module.exports = function(Api) {
2428
* Returned 'handle' can be passed to create() method to create a new document.
2529
* @memberOf Workfront.Api
2630
* @param {fs.ReadStream} stream A readable stream with file contents
31+
* @param {Object} [overrides] Override the filename and content type (using keys
32+
* `filename` and `contentType` respectively).
2733
*/
28-
Api.prototype.upload = function (/*stream*/) {
29-
throw new Error('Not implemented')
34+
Api.prototype.upload = function(stream, overrides) {
35+
var form = new FormData();
36+
form.append('uploadedFile', stream, overrides);
37+
38+
var options = {
39+
method: 'POST'
40+
};
41+
42+
util._extend(options, this.httpOptions);
43+
options.headers = form.getHeaders();
44+
options.headers.sessionID = this.httpOptions.headers.sessionID;
45+
options.path += '/upload';
46+
47+
delete options.headers['Content-Length'];
48+
49+
var httpTransport = this.httpTransport;
50+
51+
return new Promise(function (resolve, reject) {
52+
var request = httpTransport.request(options, this._handleResponse(resolve, reject));
53+
54+
form.pipe(request);
55+
56+
request.on('error', reject);
57+
}.bind(this));
3058
};
31-
};
59+
};

test/plugins/sample.file

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Here is some sample content that needs to be uploaded.
2+
3+
It is a few lines.

0 commit comments

Comments
 (0)