Changeset c96c0f8877f7062e6b90a42710f8afa79b8a2d85
- Timestamp:
- 11/27/06 21:49:06
(2 years ago)
- Author:
- Jason Michalski <armooo@armooo.net>
- git-committer:
- Jason Michalski <armooo@armooo.net> 1164685746 +0000
- git-parent:
[bea69b72eeb7caeecb147b38747a39c0f091af09]
- git-author:
- Jason Michalski <armooo@armooo.net> 1164685746 +0000
- Message:
pyTivo
- Cleaned up httpserver.py
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3bf6183 |
rc96c0f8 |
|
| 50 | 50 | if not query.has_key('Container'): |
|---|
| 51 | 51 | query['Container'] = ['/'] |
|---|
| 52 | | self.send_response(200) |
|---|
| 53 | | self.end_headers() |
|---|
| | 52 | |
|---|
| 54 | 53 | if query['Container'][0] == '/': |
|---|
| 55 | 54 | t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'root_container.tmpl')) |
|---|
| 56 | 55 | t.containers = self.server.containers |
|---|
| 57 | 56 | t.hostname = socket.gethostname() |
|---|
| | 57 | self.send_response(200) |
|---|
| | 58 | self.end_headers() |
|---|
| 58 | 59 | self.wfile.write(t) |
|---|
| 59 | 60 | else: |
|---|
| 60 | | |
|---|
| 61 | 61 | subcname = query['Container'][0] |
|---|
| 62 | 62 | cname = subcname.split('/')[0] |
|---|
| 63 | 63 | |
|---|
| 64 | | if not self.server.containers.has_key(cname): |
|---|
| | 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() |
|---|
| 65 | 67 | return |
|---|
| 66 | 68 | |
|---|
| 67 | | container = self.server.containers[cname] |
|---|
| 68 | | |
|---|
| 69 | | folders = subcname.split('/') |
|---|
| 70 | | path = container['path'] |
|---|
| 71 | | for folder in folders[1:]: |
|---|
| 72 | | if folder == '..': |
|---|
| 73 | | return |
|---|
| 74 | | path = os.path.join(path, folder) |
|---|
| 75 | | |
|---|
| 76 | | |
|---|
| 77 | | files = os.listdir(path) |
|---|
| 78 | | |
|---|
| 79 | | files = filter(lambda f: os.path.isdir(os.path.join(path, f)) or transcode.suported_format(os.path.join(path,f)), files) |
|---|
| 80 | | |
|---|
| 81 | | totalFiles = len(files) |
|---|
| 82 | | |
|---|
| | 69 | path = self.get_local_path(query) |
|---|
| 83 | 70 | def isdir(file): |
|---|
| 84 | 71 | return os.path.isdir(os.path.join(path, file)) |
|---|
| 85 | 72 | |
|---|
| 86 | | index = 0 |
|---|
| 87 | | if query.has_key('ItemCount'): |
|---|
| 88 | | count = int(query['ItemCount'] [0]) |
|---|
| 89 | | |
|---|
| 90 | | if query.has_key('AnchorItem'): |
|---|
| 91 | | anchor = unquote(query['AnchorItem'][0]) |
|---|
| 92 | | for i in range(len(files)): |
|---|
| 93 | | |
|---|
| 94 | | if isdir(files[i]): |
|---|
| 95 | | file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i] |
|---|
| 96 | | else: |
|---|
| 97 | | file_url = '/' + subcname + '/' + files[i] |
|---|
| 98 | | if file_url == anchor: |
|---|
| 99 | | index = i + 1 |
|---|
| 100 | | break |
|---|
| 101 | | if query.has_key('AnchorOffset'): |
|---|
| 102 | | index = index + int(query['AnchorOffset'][0]) |
|---|
| 103 | | files = files[index:index + count] |
|---|
| 104 | | |
|---|
| 105 | | |
|---|
| 106 | | |
|---|
| | 73 | self.send_response(200) |
|---|
| | 74 | self.end_headers() |
|---|
| 107 | 75 | t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl')) |
|---|
| 108 | 76 | t.name = subcname |
|---|
| 109 | | t.files = files |
|---|
| 110 | | t.total = totalFiles |
|---|
| 111 | | t.start = index |
|---|
| | 77 | t.files, t.total, t.start = self.get_files(query) |
|---|
| 112 | 78 | t.isdir = isdir |
|---|
| 113 | 79 | t.quote = quote |
|---|
| 114 | 80 | t.escape = escape |
|---|
| 115 | 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 | index = 0 |
|---|
| | 104 | if query.has_key('ItemCount'): |
|---|
| | 105 | count = int(query['ItemCount'] [0]) |
|---|
| | 106 | |
|---|
| | 107 | if query.has_key('AnchorItem'): |
|---|
| | 108 | anchor = unquote(query['AnchorItem'][0]) |
|---|
| | 109 | for i in range(len(files)): |
|---|
| | 110 | |
|---|
| | 111 | if os.path.isdir(os.path.join(path,files[i])): |
|---|
| | 112 | file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i] |
|---|
| | 113 | else: |
|---|
| | 114 | file_url = '/' + subcname + '/' + files[i] |
|---|
| | 115 | if file_url == anchor: |
|---|
| | 116 | index = i + 1 |
|---|
| | 117 | break |
|---|
| | 118 | if query.has_key('AnchorOffset'): |
|---|
| | 119 | index = index + int(query['AnchorOffset'][0]) |
|---|
| | 120 | files = files[index:index + count] |
|---|
| | 121 | |
|---|
| | 122 | return files, totalFiles, index |
|---|
| 116 | 123 | |
|---|
| 117 | 124 | def send_static(self, name, container): |
|---|