Try a different approach to getting Nodejs to find
[r-node:r-node.git] / server / handlers / 400-r-webvis.js
1 /*
2     Copyright 2010 Jamie Love
3
4     This file is part of the "R-Node Server".
5
6     R-Node Server is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 2.1 of the License, or
9     (at your option) any later version.
10
11     R-Node Server is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with R-Node Server.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 var SYS     = require("sys");
21 var QUERY   = require ("querystring");
22 var URL     = require("url");
23 var FS      = require("fs");
24 var MPART   = require(__dirname + "/../lib/multipart/multipart.js")
25 var UTILS   = require("../rnodeUtils");
26
27 exports.name = "/R/...webvis";
28
29 var ourTempDirectory;
30 var rTempDirectory;
31
32
33 exports.init = function (rNodeApi) {
34     var config = rNodeApi.config.features.fileUpload;
35     rNodeApi.addRestrictedUrl (/^\/R\/.*webvis/);
36
37         ourTempDirectory = rNodeApi.config.R.tempDirectoryFromOurPerspective;
38         rTempDirectory = rNodeApi.config.R.tempDirectoryFromRperspective;               
39 }
40
41 /**
42  * Supports streaming webvis code to the client.
43  * This webvis code is protovis code to draw SVG images.
44  */
45 exports.handle = function (req, resp, sid, rNodeApi) {
46     
47         var url = URL.parse (req.url, true);
48     var parts = url.href.split(/\?/)[0].split(/\//);
49     var request = QUERY.unescape(parts[2]);
50             
51         var r = rNodeApi.getRConnection(sid, false);
52         
53         r.request (request, function (rResp) {
54                 // Lets see if we get a file -
55                 if (rResp && rResp.length) { // is an array
56                         // get first element, should be a file path 
57                         if (rResp[0].search ('\.html$') > 0) {
58                                 var f = rResp[0].replace(rTempDirectory, ourTempDirectory)
59                                 
60                                 // Here, remove the html stuff off - we just want the core javascript code.
61                                 var htmlFile = FS.readFileSync (f, 'utf8');
62                                 var lines = htmlFile.split("\n");
63                                 lines = lines.splice(9, lines.length - 12); // Bit of a naff way to do it - but we remove the html wrapper.
64                                 jsFile = lines.join ("\n");
65                                 FS.writeFileSync(f + '.js', jsFile);
66                                 
67                                 var key = rNodeApi.addPagerFile({
68                                         path: f + '.js'
69                                         , mimeType: 'text/javascript+protovis'
70                                         , toDelete: true 
71                                 });
72                         
73                                 resp.writeHeader(200, { "Content-Type": "text/plain" });
74                 resp.write (JSON.stringify ({
75                     values: [key],
76                     attributes: {
77                         class:["RWebvisGraph"],
78                         "title":["R Plot"]
79                     }
80                 }));
81                 resp.end();                                     
82                         }       
83                 } else {
84                         var str = JSON.stringify(rResp);
85                 resp.writeHeader(200, {
86                   "Content-Length": str.length,
87                   "Content-Type": "text/plain" // Change to application/json TODO
88                 });
89                 resp.write (str);
90                 resp.end();
91                 }
92         })
93         
94     return true;
95 }
96
97 exports.canHandle = function (req, rNodeApi) {
98     return req.url.search ('/R/.*webvis') == 0 && req.url.search('/R/\s*library') != 0;
99 }