corinthia-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fr...@apache.org
Subject [12/17] incubator-corinthia git commit: Added uncommited change for server example
Date Wed, 29 Apr 2015 12:43:53 GMT
Added uncommited change for server example


Project: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/commit/01c924db
Tree: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/tree/01c924db
Diff: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/diff/01c924db

Branch: refs/heads/master
Commit: 01c924db0a6254e50c7081ebeeebc9280da6497c
Parents: ee179e3
Author: franzdecopenhague <franzdecopenhague@outlook.com>
Authored: Thu Mar 26 23:33:12 2015 +0000
Committer: franzdecopenhague <franzdecopenhague@outlook.com>
Committed: Thu Mar 26 23:33:12 2015 +0000

----------------------------------------------------------------------
 consumers/dfwebserver/.gitignore                |  3 +-
 .../dfwebserver/examples/node/server/server.js  | 98 +++++++++++++++++++-
 2 files changed, 99 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/01c924db/consumers/dfwebserver/.gitignore
----------------------------------------------------------------------
diff --git a/consumers/dfwebserver/.gitignore b/consumers/dfwebserver/.gitignore
index e783591..3cd6f04 100644
--- a/consumers/dfwebserver/.gitignore
+++ b/consumers/dfwebserver/.gitignore
@@ -1 +1,2 @@
-pool
\ No newline at end of file
+pool
+uploads
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/01c924db/consumers/dfwebserver/examples/node/server/server.js
----------------------------------------------------------------------
diff --git a/consumers/dfwebserver/examples/node/server/server.js b/consumers/dfwebserver/examples/node/server/server.js
index 516bf26..ec79dfc 100644
--- a/consumers/dfwebserver/examples/node/server/server.js
+++ b/consumers/dfwebserver/examples/node/server/server.js
@@ -4,6 +4,102 @@ var express = require('express'),
 var path = require('path');
 var fs = require('fs');
 
-//var mime = require('mime');
+var docformats = require('../../../node/src/docformats');
+docformats.options.binaryPath = path.resolve('../../../../../build/bin');
+
+
+var app = express();
+
+var dfwebserver = path.resolve(__dirname, '../../..');
+var web = path.resolve(__dirname, '../../../../web');
+
+app.get('/upload', function(req, res){
+  res.send('<html><head></head><body>\
+               <form method="POST" enctype="multipart/form-data">\
+                <input type="file" name="filefield"><br />\
+                <input type="submit">\
+              </form>\
+            </body></html>');
+});
+
+var multerOptions = { 
+    dest: path.resolve(dfwebserver, 'uploads/'),
+    rename: function (fieldname, filename) {
+        return filename;
+    }
+};
+
+app.post('/upload',[ multer(multerOptions), function(req, res){
+    console.log(req.body) // form fields
+    console.log(req.files) // form files
+    //res.json(req.files).status(204).end()
+    
+  res.send('<html><head></head><body>\
+               <a href="/get/' + req.files.filefield.name + '/">Navigate to this link
to run dfconvert GET in the server\
+                </a>\
+            </body></html>');
+}]);
+
+
+function getInput(id) {
+    return path.resolve(dfwebserver, 'uploads', id);
+}
+
+function getOutput(id) {
+    var folder = path.resolve(dfwebserver, 'pool', id)
+    var parsed = path.parse(folder);
+    
+    return path.resolve(folder, parsed.name + '.html');
+}
+
+function getDynamicResource(id, resource) {
+    var folder = path.resolve(dfwebserver, 'pool', id)
+
+    return path.resolve(folder, resource);
+}
+
+function getStaticResource(resource) {
+    return path.resolve(web, 'client', resource);
+}
+
+function sendFile(res, filename) {
+    if (fs.existsSync(filename)) {
+        res.sendFile(filename);
+    } else {
+        res.status(404).end(); // Not Found
+    }
+}
+
+
+app.get('/get/:id', function(req, res){
+    var input = getInput(req.params.id);
+    var abstract = getOutput(req.params.id);
+    
+    try {
+        // This is required because DocFormat API throw an error
+        // when abstract is present in the filesystem
+        fs.unlinkSync(abstract); 
+    } catch (ex) {
+        // do nothing
+    }
+    docformats.get(input, abstract);
+    sendFile(res, abstract);
+});
+
+app.get('/get/:id/*', function(req, res){
+    var staticResource = getStaticResource(req.params[0]);
+    var dynamicResource = getDynamicResource(req.params.id, req.params[0]);
+    
+    if (fs.existsSync(staticResource)) {
+        res.sendFile(staticResource);
+    } else {
+        sendFile(res, dynamicResource);
+    }
+});
+
+
+
+app.listen(process.env.PORT || 8080);
+
 
 


Mime
View raw message