/
browser.py
91 lines (72 loc) · 2.67 KB
/
browser.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
86
87
88
89
90
91
import cherrypy
import template
import os
from item import Item
import urllib
class Browser(object):
@cherrypy.expose
def index(self):
raise cherrypy.InternalRedirect('/browser/list')
@cherrypy.expose
@template.output('view.html')
def list(self, *path):
path = tuple([unicode(x) for x in path])
root = unicode(cherrypy.request.app.config['video']['path'])
fullpath = os.path.join(root, *path)
# list all files and directories
try:
content = os.listdir(fullpath)
except OSError:
traceback.print_exc()
return 'No such file or directory: %s' % os.path.join('/', *path)
# want to include a '..' back link
if len(path) > 0:
content.insert(0, '..')
# create wrappers for each object
item = [Item.create(root, os.path.join(path + (x,)), x) for x in content]
# sort
item.sort(cmp=Item.compare)
return template.render(path=os.path.join('/', *path), item=item)
@cherrypy.expose
@template.output('info.html')
def get(self, *path):
root = cherrypy.request.app.config['video']['path']
fullpath = os.path.join(root, *path)
f = Item.create(root, path, path[-1])
url = urllib.quote(os.path.join(*path).encode('utf-8'))
refs = []
for x in cherrypy.request.app.config['refs']:
ref = x
href = '#'
try:
href = cherrypy.request.app.config['refs'][x].format(id=f[x])
except KeyError:
pass
refs.append((x, href))
# meta data which is always visible
meta = {
'Genre': None,
'Tags': None,
'Year': None
}
return template.render(url=url, file=f, refs=refs, meta=meta)
@cherrypy.expose
def put(self, *path, **post):
root = cherrypy.request.app.config['video']['path']
fullpath = os.path.join(root, *path)
f = Item.create(root, path, path[-1])
id = post['id']
value = unicode(post['value'], encoding='utf-8')
# title is a special case, as it is normalized in the database
if id == 'title':
f.set_title(value)
else:
raise NotImplementedError, 'setting metadata is not yet implemented'
return value
@cherrypy.expose
def rescan(self, *path):
root = cherrypy.request.app.config['video']['path']
fullpath = os.path.join(root, *path)
f = Item.create(root, path, path[-1])
f.rescan()
raise cherrypy.HTTPRedirect('/browser/get/' + os.path.join(*path))