Skip to content

Commit 9417d06

Browse files
committed
Merge pull request mailru#313 from arusakov/nodejs_test_server
node.js test server to hotfixes branch
2 parents 4aa414b + 08cccc2 commit 9417d06

File tree

7 files changed

+93
-14
lines changed

7 files changed

+93
-14
lines changed

examples/demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@
486486
_upload: function (file){
487487
if( file ){
488488
file.xhr = FileAPI.upload({
489-
url: 'http://www.rubaxa.org/index.php',
489+
url: '/upload',
490490
files: { file: file },
491491
upload: function (){
492492
FU._getEl(file).addClass('b-file_upload');

examples/userpic.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154

155155
// Параметры загрузки
156156
var uploadOpts = {
157-
url: 'http://www.rubaxa.org/index.php' // куда грузить
157+
url: '/upload' // куда грузить
158158
, data: {} // дополнительный POST-параметры
159159
, name: 'userpic' // название POST-параметра загружаемого файла
160160
, activeClassName: 'upload_active' // класс, который будем добавлять общему контейнеру при загрузке

examples/watermark.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ <h4>EXIF</h4>
182182

183183
// Upload
184184
FileAPI.upload({
185-
url: '//rubaxa.org/FileAPI/server/ctrl.php'
185+
url: '/upload'
186186
, files: elms.file
187187
, imageTransform: {
188188
width: elms.width.value|0

examples/webcam.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ <h3>Server</h3>
114114
;
115115

116116
FileAPI.upload({
117-
url: 'http://rubaxa.org/FileAPI/server/ctrl.php'
117+
url: '/upload'
118118
, files: { shot: file }
119119
, complete: function (err, xhr){
120120
var res = JSON.parse(xhr.responseText);

node/file-api.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
function convertToBase64(buffer, mimetype) {
3+
return 'data:' + mimetype + ';base64,' + buffer.toString('base64');
4+
}
5+
6+
function fileApi() {
7+
return function (req, res, next) {
8+
req.body = req.body || {};
9+
req.body.images = {};
10+
11+
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
12+
var buffersArray = [];
13+
14+
file.on('data', function (data) {
15+
buffersArray.push(data);
16+
});
17+
18+
file.on('end', function () {
19+
var bufferResult = Buffer.concat(buffersArray);
20+
req.body.images[fieldname] = {
21+
dataURL: convertToBase64(bufferResult, mimetype),
22+
mime: mimetype,
23+
size: bufferResult.length
24+
};
25+
});
26+
});
27+
28+
req.busboy.on('finish', function () {
29+
next();
30+
});
31+
};
32+
}
33+
34+
module.exports = fileApi;

node/server.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var express = require('express');
2+
var busboy = require('connect-busboy');
3+
var fileApi = require('./file-api');
4+
var app = express();
5+
6+
app.use(express.static('.', {index: 'index.html'}));
7+
8+
app.use(function (req, res, next) {
9+
// Enable CORS for non static files
10+
var origin = req.get('Origin');
11+
if (origin) {
12+
res.set({
13+
'Access-Control-Allow-Origin': origin,
14+
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
15+
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type',
16+
'Access-Control-Allow-Credentials': 'true'
17+
});
18+
}
19+
next();
20+
});
21+
22+
var uploadPath = '/upload';
23+
24+
app.options(uploadPath, function (req, res) {
25+
res.end();
26+
});
27+
28+
app.post(uploadPath, busboy({immediate: true}), fileApi(), function (req, res) {
29+
var jsonp = req.query.callback || null;
30+
res[jsonp ? 'jsonp' : 'json']({
31+
status: 200,
32+
statusText: 'OK',
33+
images: req.body.images
34+
});
35+
});
36+
37+
var server = app.listen(8000, function () {
38+
39+
var host = server.address().address;
40+
var port = server.address().port;
41+
42+
console.log('Test server listening at http://%s:%s', host, port)
43+
});

package.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
"exportName": "FileAPI",
44
"version": "2.0.10",
55
"devDependencies": {
6+
"connect-busboy": "~0.0.2",
7+
"eventemitter2": "~0.4.13",
8+
"express": "~4.12.3",
69
"grunt": "~0.4.5",
7-
"grunt-version": "~0.3.0",
8-
"grunt-contrib-jshint": "~0.10.0",
10+
"grunt-contrib-compress": "~0.9.1",
911
"grunt-contrib-concat": "~0.4.0",
12+
"grunt-contrib-connect": "~0.8.0",
13+
"grunt-contrib-jshint": "~0.10.0",
1014
"grunt-contrib-uglify": "~0.5.0",
1115
"grunt-contrib-watch": "~0.6.1",
12-
"grunt-contrib-connect": "~0.8.0",
13-
"eventemitter2": "~0.4.13",
14-
"semver": "~2.3.1 ",
15-
"temporary": "~0.0.8",
16-
"phantomjs": "~1.9.7-9",
17-
"grunt-curl": "~2.0.2",
16+
"grunt-curl": "~2.0.2",
1817
"grunt-mxmlc": "~0.5.1",
19-
"grunt-contrib-compress": "~0.9.1"
18+
"grunt-version": "~0.3.0",
19+
"phantomjs": "~1.9.7-9",
20+
"semver": "~2.3.1 ",
21+
"temporary": "~0.0.8"
2022
},
2123
"description": "FileAPI — a set of javascript tools for working with files. Multiupload, drag'n'drop and chunked file upload. Images: crop, resize and auto orientation by EXIF.",
2224
"main": "dist/FileAPI.js",
@@ -38,7 +40,7 @@
3840
"contributors": [
3941
"Vladimir Demidov <v.demidov@corp.mail.ru>",
4042
"Ilya Lebedev <ilya@lebedev.net>",
41-
"Mikhail Bezoyan <mbezoyan@gmail.com>"
43+
"Mikhail Bezoyan <mbezoyan@gmail.com>"
4244
],
4345
"license": "BSD"
4446
}

0 commit comments

Comments
 (0)