Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import logging | 5 import logging |
| 6 import mimetypes | 6 import mimetypes |
| 7 import posixpath | 7 import posixpath |
| 8 import traceback | 8 import traceback |
| 9 | 9 |
| 10 from compiled_file_system import SingleFile | 10 from compiled_file_system import SingleFile |
| 11 from directory_zipper import DirectoryZipper | 11 from directory_zipper import DirectoryZipper |
| 12 from docs_server_utils import ToUnicode | 12 from docs_server_utils import ToUnicode |
| 13 from file_system import FileNotFoundError | 13 from file_system import FileNotFoundError |
| 14 from future import Future | 14 from future import Future |
| 15 from path_canonicalizer import PathCanonicalizer | 15 from path_canonicalizer import PathCanonicalizer |
| 16 from path_util import AssertIsValid, Join, ToDirectory | 16 from path_util import AssertIsValid, IsDirectory, Join, ToDirectory |
| 17 from special_paths import SITE_VERIFICATION_FILE | 17 from special_paths import SITE_VERIFICATION_FILE |
| 18 from third_party.handlebar import Handlebar | 18 from third_party.handlebar import Handlebar |
| 19 from third_party.markdown import markdown | 19 from third_party.markdown import markdown |
| 20 | 20 |
| 21 | 21 |
| 22 _MIMETYPE_OVERRIDES = { | 22 _MIMETYPE_OVERRIDES = { |
| 23 # SVG is not supported by mimetypes.guess_type on AppEngine. | 23 # SVG is not supported by mimetypes.guess_type on AppEngine. |
| 24 '.svg': 'image/svg+xml', | 24 '.svg': 'image/svg+xml', |
| 25 } | 25 } |
| 26 | 26 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 lambda: ContentAndType(zip_future.Get(), 'application/zip', None)) | 141 lambda: ContentAndType(zip_future.Get(), 'application/zip', None)) |
| 142 | 142 |
| 143 # If there is no file extension, look for a file with one of the default | 143 # If there is no file extension, look for a file with one of the default |
| 144 # extensions. If one cannot be found, check if the path is a directory. | 144 # extensions. If one cannot be found, check if the path is a directory. |
| 145 # If it is, then check for an index file with one of the default | 145 # If it is, then check for an index file with one of the default |
| 146 # extensions. | 146 # extensions. |
| 147 if not ext: | 147 if not ext: |
| 148 new_path = self._AddExt(path) | 148 new_path = self._AddExt(path) |
| 149 # Add a trailing / to check if it is a directory and not a file with | 149 # Add a trailing / to check if it is a directory and not a file with |
| 150 # no extension. | 150 # no extension. |
| 151 if new_path is None and self.file_system.Exists(path + '/').Get(): | 151 if new_path is None and self.file_system.Exists(ToDirectory(path)).Get(): |
| 152 new_path = self._AddExt(path + '/index') | 152 new_path = self._AddExt(Join(path, 'index')) |
| 153 if new_path is not None: | 153 if new_path is not None: |
| 154 path = new_path | 154 path = new_path |
| 155 | 155 |
| 156 # If a directory hasn't been rewritten to an index file by now then it's | |
|
Jeffrey Yasskin
2014/04/16 22:44:09
"hasn't been ... by now" implies that something pr
not at google - send to devlin
2014/04/16 23:03:23
Good idea, and a check 4 lines up has more context
| |
| 157 # clearly not found, since this class only knows how to serve files. | |
| 158 if IsDirectory(path): | |
| 159 return FileNotFoundError.RaiseInFuture('"%s" is a directory' % path) | |
| 160 | |
| 156 return self._content_cache.GetFromFile(path) | 161 return self._content_cache.GetFromFile(path) |
| 157 | 162 |
| 158 def _AddExt(self, path): | 163 def _AddExt(self, path): |
| 159 '''Tries to append each of the default file extensions to path and returns | 164 '''Tries to append each of the default file extensions to path and returns |
| 160 the first one that is an existing file. | 165 the first one that is an existing file. |
| 161 ''' | 166 ''' |
| 162 for default_ext in self._default_extensions: | 167 for default_ext in self._default_extensions: |
| 163 if self.file_system.Exists(path + default_ext).Get(): | 168 if self.file_system.Exists(path + default_ext).Get(): |
| 164 return path + default_ext | 169 return path + default_ext |
| 165 return None | 170 return None |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 179 self.GetContentAndType(Join(root, base)))) | 184 self.GetContentAndType(Join(root, base)))) |
| 180 # TODO(kalman): Cache .zip files for each directory (if supported). | 185 # TODO(kalman): Cache .zip files for each directory (if supported). |
| 181 def resolve(): | 186 def resolve(): |
| 182 for label, future in futures: | 187 for label, future in futures: |
| 183 try: future.Get() | 188 try: future.Get() |
| 184 except: logging.error('%s: %s' % (label, traceback.format_exc())) | 189 except: logging.error('%s: %s' % (label, traceback.format_exc())) |
| 185 return Future(callback=resolve) | 190 return Future(callback=resolve) |
| 186 | 191 |
| 187 def __repr__(self): | 192 def __repr__(self): |
| 188 return 'ContentProvider of <%s>' % repr(self.file_system) | 193 return 'ContentProvider of <%s>' % repr(self.file_system) |
| OLD | NEW |