Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/content_provider.py |
| diff --git a/chrome/common/extensions/docs/server2/content_provider.py b/chrome/common/extensions/docs/server2/content_provider.py |
| index d62ba4f753354e58518cdab52a0a3484f6639ded..4c206493bd91d4d8c4be7a26d3dbb49b88b7876e 100644 |
| --- a/chrome/common/extensions/docs/server2/content_provider.py |
| +++ b/chrome/common/extensions/docs/server2/content_provider.py |
| @@ -11,7 +11,7 @@ from compiled_file_system import SingleFile |
| from directory_zipper import DirectoryZipper |
| from docs_server_utils import ToUnicode |
| from file_system import FileNotFoundError |
| -from future import Future |
| +from future import All, Future |
| from path_canonicalizer import PathCanonicalizer |
| from path_util import AssertIsValid, IsDirectory, Join, ToDirectory |
| from special_paths import SITE_VERIFICATION_FILE |
| @@ -129,35 +129,54 @@ class ContentProvider(object): |
| return self._path_canonicalizer.Canonicalize(path) |
| def GetContentAndType(self, path): |
| - '''Returns the ContentAndType of the file at |path|. |
| + '''Returns a Future to the ContentAndType of the file at |path|. |
| ''' |
| AssertIsValid(path) |
| base, ext = posixpath.splitext(path) |
| - |
| - # Check for a zip file first, if zip is enabled. |
| if self._directory_zipper and ext == '.zip': |
| zip_future = self._directory_zipper.Zip(ToDirectory(base)) |
| return Future(callback= |
| lambda: ContentAndType(zip_future.Get(), 'application/zip', None)) |
| + return self._content_cache.GetFromFile(self._FindFileForPath(path)) |
| + |
| + def GetVersion(self, path): |
| + '''Returns a Future to the version of the file at |path|. |
| + ''' |
| + AssertIsValid(path) |
| + base, ext = posixpath.splitext(path) |
| + if self._directory_zipper and ext == '.zip': |
| + stat_future = self.file_system.StatAsync(ToDirectory(base)) |
| + else: |
| + stat_future = self.file_system.StatAsync(self._FindFileForPath(path)) |
| + return Future(callback=lambda: stat_future.Get().version) |
|
Yoyo Zhou
2014/08/12 22:32:57
Is this a Then?
not at google - send to devlin
2014/08/13 02:14:28
ooh, yes it is.
Sorry I went a little overboard h
|
| + |
| + def _FindFileForPath(self, path): |
| + '''Finds the real file backing |path|. This may require looking for the |
| + correct file extension, or looking for an 'index' file if it's a directory. |
| + Returns None if no path is found. |
| + ''' |
| + AssertIsValid(path) |
| + _, ext = posixpath.splitext(path) |
| + |
| + if ext: |
| + # There was already an extension, trust that it's a path. Elsewhere |
| + # up the stack this will be caught if it's not. |
| + return path |
| + |
| + # Look for a file with one of the default extensions. |
| + new_path = self._AddExt(path) |
| + if new_path is not None: |
| + return new_path |
| - # If there is no file extension, look for a file with one of the default |
| - # extensions. If one cannot be found, check if the path is a directory. |
| - # If it is, then check for an index file with one of the default |
| - # extensions. |
| - if not ext: |
| - new_path = self._AddExt(path) |
| - # Add a trailing / to check if it is a directory and not a file with |
| - # no extension. |
| - if new_path is None and self.file_system.Exists(ToDirectory(path)).Get(): |
| - new_path = self._AddExt(Join(path, 'index')) |
| - # If an index file wasn't found in this directly then we're never going |
| - # to find a file. |
| - if new_path is None: |
| - return FileNotFoundError.RaiseInFuture('"%s" is a directory' % path) |
| + # Check if the path is really a directory, and if so look for an index file. |
| + if self.file_system.Exists(ToDirectory(path)).Get(): |
| + new_path = self._AddExt(Join(path, 'index')) |
| if new_path is not None: |
| - path = new_path |
| + return new_path |
| - return self._content_cache.GetFromFile(path) |
| + # Nothing found. Just return the original path and let logic up the stack |
| + # detect this as a 404 or whatever. |
| + return path |
| def _AddExt(self, path): |
| '''Tries to append each of the default file extensions to path and returns |