-
Notifications
You must be signed in to change notification settings - Fork 0
/
libDocs.py
86 lines (70 loc) · 2.45 KB
/
libDocs.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import libView;
from libView import toHtml, toUrl;
import os
import json
def validPath(path):
return(re.match("^[A-Za-z0-9/]+$", path) != None and path[0] == "/")
def basePath():
return("/var/fb/docs")
def getMeta(path):
data = {}
if(validPath(path)):
filename = basePath() + "/" + "meta.json"
if os.path.exists(filename):
with open(filename, "r") as f:
try:
data = json.load(f)
except ValueError:
data = {}
return(data)
def storeMeta(path, data):
if(validPath(path)):
filename = basePath() + "/" + "meta.json"
f = open(filename, "w")
if(f):
json.dump(data, f)
f.close()
return(True)
return(False)
def folderHeader():
print "<p><a href='addmodule.py></a></p>"
def showFile(filename):
print "Is a file"
def showFolder(path, fullPath):
libView.doHeader("Documents: " + path)
folderHeader()
if(not os.path.exists(fullPath)):
print "<p>Invalid path</p>"
else:
files = os.listdir(fullPath)
for f in files:
ff = os.path.join(fullPath, f)
if(os.path.isdir(ff)):
libView.printf( "<p>Subdir: <a href=\"docs.py?path=%s%s/\">%s</a></p>", (toUrl(path), toUrl(f),toHtml(f),))
for f in files:
if(f != "meta.json"):
ff = os.path.join(fullPath, f)
if(not os.path.isdir(ff)):
libView.printf( "<p>File: <a href=\"docs.py?path=%s%s\">%s</a></p>", (toUrl(path), toUrl(f),toHtml(f),))
meta = getMeta(path)
meta["count"] = meta.get("count",0) + 1
storeMeta(path, meta)
libView.printf("<p>Folder has %d views</p>", meta["count"])
if(1):
print "<h2>Upload</h2>"
print "<form action=\"docs.py\" enctype=\"multipart/form-data\" method=\"post\">"
libView.printf("<input type=\"file\" name=\"uploaded_file\" max_size=\"%s\">", (80 * 1024*1024,))
print "<input type=\"hidden\" name=\"action\" value=\"upload\">"
libView.printf("<input type=\"hidden\" name=\"path\" value=\"%s\">", (toHtml(path),))
if(0): # option to require mime-type declaration on upload (otherwise just ask user-agent)
filetypes = ("text/plain", "image/jpeg", "image/png", "application/pdf")
print "<select>"
for filetype in filetypes:
libView.printf("<option name=\"%s\">%s</option>", (toHtml(filetype), toHtml(filetype),))
print "</select> "
print "<input type=\"submit\" value=\"Upload\">"
print "</form>"
libView.doFooter();