| 47 | | |
|---|
| 48 | | def do_QueryContainer(self, query): |
|---|
| 49 | | |
|---|
| 50 | | if not query.has_key('Container'): |
|---|
| 51 | | query['Container'] = ['/'] |
|---|
| 52 | | |
|---|
| 53 | | if query['Container'][0] == '/': |
|---|
| 54 | | t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'root_container.tmpl')) |
|---|
| 55 | | t.containers = self.server.containers |
|---|
| 56 | | t.hostname = socket.gethostname() |
|---|
| 57 | | self.send_response(200) |
|---|
| 58 | | self.end_headers() |
|---|
| 59 | | self.wfile.write(t) |
|---|
| 60 | | else: |
|---|
| 61 | | subcname = query['Container'][0] |
|---|
| 62 | | cname = subcname.split('/')[0] |
|---|
| 63 | | |
|---|
| 64 | | if not self.server.containers.has_key(cname) or not self.get_local_path(query): |
|---|
| 65 | | self.send_response(404) |
|---|
| 66 | | self.end_headers() |
|---|
| 67 | | return |
|---|
| 68 | | |
|---|
| 69 | | path = self.get_local_path(query) |
|---|
| 70 | | def isdir(file): |
|---|
| 71 | | return os.path.isdir(os.path.join(path, file)) |
|---|
| 73 | | self.send_response(200) |
|---|
| 74 | | self.end_headers() |
|---|
| 75 | | t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl')) |
|---|
| 76 | | t.name = subcname |
|---|
| 77 | | t.files, t.total, t.start = self.get_files(query) |
|---|
| 78 | | t.isdir = isdir |
|---|
| 79 | | t.quote = quote |
|---|
| 80 | | t.escape = escape |
|---|
| 81 | | self.wfile.write(t) |
|---|
| 82 | | |
|---|
| 83 | | def get_local_path(self, query): |
|---|
| 84 | | |
|---|
| 85 | | subcname = query['Container'][0] |
|---|
| 86 | | container = self.server.containers[subcname.split('/')[0]] |
|---|
| 87 | | |
|---|
| 88 | | path = container['path'] |
|---|
| 89 | | for folder in subcname.split('/')[1:]: |
|---|
| 90 | | if folder == '..': |
|---|
| 91 | | return False |
|---|
| 92 | | path = os.path.join(path, folder) |
|---|
| 93 | | return path |
|---|
| 94 | | |
|---|
| 95 | | def get_files(self, query): |
|---|
| 96 | | subcname = query['Container'][0] |
|---|
| 97 | | path = self.get_local_path(query) |
|---|
| 98 | | |
|---|
| 99 | | files = os.listdir(path) |
|---|
| 100 | | files = filter(lambda f: os.path.isdir(os.path.join(path, f)) or transcode.suported_format(os.path.join(path,f)), files) |
|---|
| 101 | | totalFiles = len(files) |
|---|
| 102 | | |
|---|
| 103 | | def dir_sort(x, y): |
|---|
| 104 | | xdir = os.path.isdir(os.path.join(path, x)) |
|---|
| 105 | | ydir = os.path.isdir(os.path.join(path, y)) |
|---|
| 106 | | |
|---|
| 107 | | if xdir and ydir: |
|---|
| 108 | | return name_sort(x, y) |
|---|
| 109 | | elif xdir: |
|---|
| 110 | | return -1 |
|---|
| 111 | | elif ydir: |
|---|
| 112 | | return 1 |
|---|
| 113 | | else: |
|---|
| 114 | | return name_sort(x, y) |
|---|
| 115 | | |
|---|
| 116 | | def name_sort(x, y): |
|---|
| 117 | | numbername = re.compile(r'(\d*)(.*)') |
|---|
| 118 | | m = numbername.match(x) |
|---|
| 119 | | xNumber = m.group(1) |
|---|
| 120 | | xStr = m.group(2) |
|---|
| 121 | | m = numbername.match(y) |
|---|
| 122 | | yNumber = m.group(1) |
|---|
| 123 | | yStr = m.group(2) |
|---|
| 124 | | |
|---|
| 125 | | print xNumber, ':', xStr |
|---|
| 126 | | |
|---|
| 127 | | if xNumber and yNumber: |
|---|
| 128 | | xNumber, yNumber = int(xNumber), int(yNumber) |
|---|
| 129 | | if xNumber == yNumber: |
|---|
| 130 | | return cmp(xStr, yStr) |
|---|
| 131 | | else: |
|---|
| 132 | | return cmp(xNumber, yNumber) |
|---|
| 133 | | elif xNumber: |
|---|
| 134 | | return -1 |
|---|
| 135 | | elif yNumber: |
|---|
| 136 | | return 1 |
|---|
| 137 | | else: |
|---|
| 138 | | return cmp(xStr, yStr) |
|---|
| 139 | | |
|---|
| 140 | | files.sort(dir_sort) |
|---|
| 141 | | |
|---|
| 142 | | index = 0 |
|---|
| 143 | | if query.has_key('ItemCount'): |
|---|
| 144 | | count = int(query['ItemCount'] [0]) |
|---|
| 145 | | |
|---|
| 146 | | if query.has_key('AnchorItem'): |
|---|
| 147 | | anchor = unquote(query['AnchorItem'][0]) |
|---|
| 148 | | for i in range(len(files)): |
|---|
| 149 | | |
|---|
| 150 | | if os.path.isdir(os.path.join(path,files[i])): |
|---|
| 151 | | file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i] |
|---|
| 152 | | else: |
|---|
| 153 | | file_url = '/' + subcname + '/' + files[i] |
|---|
| 154 | | if file_url == anchor: |
|---|
| 155 | | index = i + 1 |
|---|
| 156 | | break |
|---|
| 157 | | if query.has_key('AnchorOffset'): |
|---|
| 158 | | index = index + int(query['AnchorOffset'][0]) |
|---|
| 159 | | files = files[index:index + count] |
|---|
| 160 | | |
|---|
| 161 | | return files, totalFiles, index |
|---|
| 162 | | |
|---|
| 163 | | def send_static(self, name, container): |
|---|
| 164 | | |
|---|
| 165 | | #cheep hack |
|---|
| 166 | | if self.headers.getheader('Range') and not self.headers.getheader('Range') == 'bytes=0-': |
|---|
| 167 | | self.send_response(404) |
|---|
| 168 | | self.end_headers() |
|---|
| 169 | | return |
|---|
| 170 | | |
|---|
| 171 | | o = urlparse("http://fake.host" + self.path) |
|---|
| 172 | | path = unquote_plus(o.path) |
|---|
| 173 | | self.send_response(200) |
|---|
| 174 | | self.end_headers() |
|---|
| 175 | | transcode.output_video(container['path'] + path[len(name)+1:], self.wfile) |
|---|
| 176 | | |
|---|