1 |
import settings
|
2 |
from django.http import HttpResponse
|
3 |
import os.path
|
4 |
|
5 |
|
6 |
# removes all junk from file path ('..', prepended '/' or duplicate '/')
|
7 |
# !!! Warning: also used for cleaning app file list (where '/' is a list separator)
|
8 |
def _file_path_safe_parse(path):
|
9 |
if path:
|
10 |
path = path.lstrip('/')
|
11 |
path = path.replace('..', '')
|
12 |
path = os.path.normpath(path)
|
13 |
return path
|
14 |
|
15 |
|
16 |
# returns file list (dirs, files and their mtimes, sizes) in json
|
17 |
# ?dir=some/path&order=name_asc
|
18 |
# dir is prepended with settings.PROLATIO_AUDIO_DIR
|
19 |
# order can be: name_asc (default), name_desc, mtime_asc, mtime_desc
|
20 |
def file_list(request):
|
21 |
from os import listdir
|
22 |
|
23 |
dir = request.GET.get('path', '')
|
24 |
order = request.GET.get('order', '')
|
25 |
|
26 |
dir = _file_path_safe_parse(dir)
|
27 |
dir = os.path.join(settings.PROLATIO_AUDIO_DIR, dir)
|
28 |
|
29 |
# FIXME: check if dir exists?
|
30 |
# FIXME: check if dir is readable?
|
31 |
files = listdir(dir)
|
32 |
subdirs = list()
|
33 |
audio_files = list() # (filename, mtime, size)
|
34 |
|
35 |
for file in files:
|
36 |
full_path = os.path.join(dir, file)
|
37 |
if os.path.isdir(full_path) and not file.startswith('.'):
|
38 |
subdirs.append(file)
|
39 |
elif os.path.splitext(file)[1].lower()[1:] in settings.PROLATIO_TAGGER_AUDIO_EXTS:
|
40 |
mtime = 0
|
41 |
size = 0
|
42 |
try:
|
43 |
mtime = os.path.getmtime(full_path)
|
44 |
size = os.path.getsize(full_path)
|
45 |
except:
|
46 |
pass
|
47 |
audio_files.append((file, mtime, size))
|
48 |
|
49 |
# order the lists
|
50 |
subdirs.sort()
|
51 |
if order == 'name_desc':
|
52 |
audio_files = sorted(audio_files, key=lambda f: f[0], reverse=True)
|
53 |
elif order.startswith('mtime'):
|
54 |
reverse = order.endswith('desc')
|
55 |
audio_files = sorted(audio_files, key=lambda f: f[1], reverse=reverse)
|
56 |
else:
|
57 |
audio_files = sorted(audio_files, key=lambda f: f[0])
|
58 |
|
59 |
json = '[['
|
60 |
for subdir in subdirs:
|
61 |
json += '{"dir_name":"%s"}, ' % subdir
|
62 |
json = json.rstrip(', ') + '], ['
|
63 |
for audio_file in audio_files:
|
64 |
json += '{"file_name":"%s","modification_date":%d,"size":%d}, ' % audio_file
|
65 |
json = json.rstrip(', ') + ']]'
|
66 |
return HttpResponse(json)
|
67 |
|
68 |
def file_list_extra(request):
|
69 |
from os import listdir
|
70 |
import os.path
|
71 |
|
72 |
dir = request.GET.get('path', '')
|
73 |
order = request.GET.get('order', '')
|
74 |
|
75 |
dir = settings.PROLATIO_AUDIO_DIR + '/' + dir
|
76 |
|
77 |
files = listdir(dir) # FIXME: check if dir is readable?
|
78 |
subdirs = list()
|
79 |
audio_files = list() # (filename, mtime, size)
|
80 |
|
81 |
for file in files:
|
82 |
full_path = os.path.join(dir, file)
|
83 |
if os.path.isdir(full_path):
|
84 |
subdirs.append(file)
|
85 |
elif os.path.splitext(file)[1].lower()[1:] in settings.PROLATIO_TAGGER_AUDIO_EXTS:
|
86 |
mtime = 0
|
87 |
size = 0
|
88 |
try:
|
89 |
mtime = os.path.getmtime(full_path)
|
90 |
size = os.path.getsize(full_path)
|
91 |
except:
|
92 |
pass
|
93 |
audio_files.append((file, mtime, size))
|
94 |
|
95 |
# order the lists
|
96 |
subdirs.sort()
|
97 |
if order == 'name_desc':
|
98 |
audio_files = sorted(audio_files, key=lambda f: f[0], reverse=True)
|
99 |
elif order.startswith('mtime'):
|
100 |
reverse = order.endswith('desc')
|
101 |
audio_files = sorted(audio_files, key=lambda f: f[1], reverse=reverse)
|
102 |
else:
|
103 |
audio_files = sorted(audio_files, key=lambda f: f[0])
|
104 |
|
105 |
json = '['
|
106 |
for audio_file in audio_files:
|
107 |
json += '{"extra_test":"%s","modification_date":%d,"size":%d},' % audio_file
|
108 |
json = json.rstrip(',') + ']'
|
109 |
return HttpResponse(json)
|
110 |
|
111 |
|
112 |
# returns tags of audio files (all common tags, as well as some audio format properties) in json
|
113 |
# ?dir=some/path&files=one_file/another_file/third_file
|
114 |
# dir is prepended with settings.PROLATIO_AUDIO_DIR
|
115 |
# files is list of filenames (relative to dir) separated by '/'
|
116 |
def load_tags(request):
|
117 |
dir = request.GET.get('dir', '')
|
118 |
files_str = request.GET.get('files', '')
|
119 |
|
120 |
files_str = _file_path_safe_parse(files_str)
|
121 |
files = files_str.split('/')
|
122 |
dir = _file_path_safe_parse(dir)
|
123 |
dir = os.path.join(settings.PROLATIO_AUDIO_DIR, dir)
|
124 |
|
125 |
for i, file in enumerate(files):
|
126 |
files[i] = os.path.join(dir, file)
|
127 |
|
128 |
|
129 |
|