-
Notifications
You must be signed in to change notification settings - Fork 2
/
actions.js
96 lines (84 loc) · 2.39 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var {Response} = require('ringo/webapp/response');
var fs = require('fs');
var md = require('ringo/markdown');
var files = require('ringo/utils/files');
var strings = require('ringo/utils/strings');
var numbers = require('ringo/utils/numbers');
var log = require('ringo/logging').getLogger(module.id);
var root = fs.absolute(require("./config").root);
var welcomePages = ["index.html","index.md"];
exports.index = function (req,path)
{
var uriPath = files.resolveUri(req.rootPath, path);
var absolutePath=fs.join(root,path);
checkRequest(uriPath);
if(fs.isFile(absolutePath))
{
return serveFile(absolutePath);
}
if(fs.isDirectory(absolutePath))
{
for each(var name in welcomePages)
{
if(fs.isFile(fs.join(absolutePath,name)))
{
return serveFile(fs.join(absolutePath,name));
}
}
if (!strings.endsWith(uriPath, "/")) {
throw {redirect: uriPath + "/"};
}
return listFiles(absolutePath, uriPath);
}
throw {notfound:true};
};
function listFiles(absolutePath,uriPath)
{
var paths = fs.list(absolutePath).filter(function(file)
{
return !files.isHidden(file);
}).sort().map(function(file)
{
var filePath = fs.join(absolutePath,file);
var size;
if (fs.isDirectory(filePath)) {
size = fs.list(filePath).length + " files";
} else {
size = numbers.format(fs.size(filePath) / 1024) + " kB";
}
return {
name:file,
size: size,
lastModified: fs.lastModified(filePath),
path: files.resolveUri(uriPath,file)
};
});
var parentDir = uriPath == "/" ? "":"/../";
return Response.skin(module.resolve('skins/list.html'), {
files: paths,
title: uriPath,
parent: parentDir
});
}
function serveFile(absolutePath)
{
if(fs.extension(absolutePath)==".md")
{
var html = md.Markdown().process(fs.read(absolutePath));
return Response.skin(module.resolve('skins/page.html'), {
content: html,
});
}
return Response.static(absolutePath);
}
function checkRequest(request)
{
var path = request.split('/');
for(var i=0;i<path.length;i++)
{
if(path[i]!="" && files.isHidden(path[i]))
{
throw {notfound:true};
}
}
}