• R/O
  • HTTP
  • SSH
  • HTTPS

zephyr: Commit

This repository is for zephyr, which is implemented for a GUI of Eos.


Commit MetaInfo

Revisiond9faf4befdc87347d89514f566fc6fb33ccc9cd4 (tree)
Time2016-01-26 21:14:43
Authorhimetani_cafe <fumifumi@yasu...>
Commiterhimetani_cafe

Log Message

[Develop and manual Test] API endpoint (post) /api/v1/fileUpload

ファイル属性を含むFormオブジェクトpost
Form
{
parentDirectory: [string],
name: [Blob],
.
.
.
nameN: [Blob]
}
ファイルのアップロードに成功すると、アップロードに成功したファイルの情報を返す。
ファイルのアップロードに失敗すると、エラーの情報を返す。

Change Summary

Incremental Difference

--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,4 @@ user-specific-files/meta/
1515 user-specific-files/workspace/
1616 user-specific-files/workspace.debug
1717 user-specific-files/db/
18+user-specific-files/tmp/
--- a/server/api/v1/fileUpload/index.js
+++ b/server/api/v1/fileUpload/index.js
@@ -4,36 +4,82 @@ var express = require('express');
44 var router = express.Router()
55 var path = require('path')
66 var formidable = require('formidable')
7-var DB = require(__dirname + '/../../../../server/class/DB').instance;
7+var db = require(__dirname + '/../../../../server/class/DB').instance;
8+var uuid = require('node-uuid');
9+var fs = require('fs');
810
911 router.post('/', function(req, res) {
1012 var form = new formidable.IncomingForm();
11- if(process.env['NODE_ENV'] === 'debug') {
12- form.uploadDir = __dirname + '/../../../../user-specific-files/workspace.debug';
13- } else {
14- form.uploadDir = __dirname + '/../../../../user-specific-files/workspace';
15- }
16-
17- DB.createFile('hoge.txt','/')
18- .then(function(r) {
19- console.log(r);
13+ var filesInfo = [];
14+
15+ form.uploadDir = __dirname + '/../../../../user-specific-files/tmp';
16+
17+ form.on('fileBegin', function(parentDirectory, file) {
18+ });
19+
20+ form.on('error', function(err) {
21+ console.errro(err);
22+ var i = {
23+ status: 'error',
24+ message: err
25+ }
26+ res.send(i);
27+ });
28+
29+
30+ form.parse(req, function(err, fields, files) {
31+ var workspace;
32+ var parentDirectory = fields['parentDirectory'];
33+ if(process.env['NODE_ENV'] === 'debug') {
34+ workspace = __dirname + '/../../../../user-specific-files/workspace.debug/';
35+ } else {
36+ workspace = __dirname + '/../../../../user-specific-files/workspace/';
37+ }
38+ var promises = [];
39+ Object.keys(files).forEach(function(key) {
40+ var file = files[key];
41+ var fileName = file.name;
42+ var tmpPath = file.path;
43+ var info = {
44+ status: null,
45+ parentDirectory: parentDirectory,
46+ fileName: fileName,
47+ tmpPath: tmpPath,
48+ fileId: null
49+ };
50+
51+ filesInfo.push(info);
52+ promises.push(db.createFile(fileName, parentDirectory)); // file作成に成功したらresolve, 失敗したらreject
53+ });
54+
55+ Promise.all(promises)
56+ .catch(function(err) {
57+ filesInfo.forEach(function(fileInfo) {
58+ fs.unlinkSync(fileInfo.tmpPath);
59+ });
60+
61+ var info = {
62+ status: 'error',
63+ messag: err.message
64+ };
65+
66+ res.send(info);
67+ })
68+ .then(function(fileIds) {
69+ fileIds.forEach(function(fileId, i) {
70+ filesInfo[i].status = 'success';
71+ filesInfo[i].fileId = fileId;
72+ fs.renameSync(filesInfo[i].tmpPath, workspace + fileId);
73+ delete tmpPaths[i];
74+ })
75+ res.send(responsesInfo);
76+ });
77+ });
78+
79+ form.on('end', function() {
80+
2081 });
2182
22- /*
23- form.parse(req, function(err, fields, files) {
24- var parentDirectory = fields['parentDirectory'];
25- var keys = Object.keys(files);
26- keys.forEach(function(f) {
27- db.createFile(files[f].name, parentDirectory)
28- .then(function(path) {
29- files[f].path = form.uploadDir + '/' + path;
30- console.log(files[f].path);
31- });
32- });
33- res.send("done");
34- });
35- */
3683 });
3784
3885 module.exports = router;
39-
--- a/server/app.js
+++ b/server/app.js
@@ -15,16 +15,17 @@ require('./routes')(app);
1515 // DBのコンストラクタを実行
1616 var DB = require('./class/DB').instance;
1717 DB.init()
18+.then(function() {
19+ return DB.test2();
20+})
1821 .then(function(methods) {
19- console.log(DB);
20-});
22+ var server = app.listen(config.port, config.ip, function () {
23+ console.log('Zephyr listening at http://%s:%s', config.ip, config.port);
24+ console.log(app.get('env'))
25+ });
2126
22-var server = app.listen(config.port, config.ip, function () {
23- console.log('Zephyr listening at http://%s:%s', config.ip, config.port);
24- console.log(app.get('env'))
27+ var io = require('socket.io').listen(server);
28+ require('./ws')(io);
2529 });
2630
27-var io = require('socket.io').listen(server);
28-require('./ws')(io);
29-
3031 //exports = module.exports = app;
Show on old repository browser