def listdir(self, dir_name): """Lists all files in a directory. Args: dir_name: str. The directory whose files should be listed. This should not start with '/' or end with '/'. Returns: list(str). A lexicographically-sorted list of filenames. """ if dir_name.endswith('/') or dir_name.startswith('/'): raise IOError( 'The dir_name should not start with / or end with / : %s' % ( dir_name)) # The trailing slash is necessary to prevent non-identical directory # names with the same prefix from matching, e.g. /abcd/123.png should # not match a query for files under /abc/. prefix = '%s' % utils.vfs_construct_path( '/', self._assets_path, dir_name) if not prefix.endswith('/'): prefix += '/' # The prefix now ends and starts with '/'. bucket_name = app_identity_services.get_gcs_resource_bucket_name() # The path entered should be of the form, /bucket_name/prefix. path = '/%s%s' % (bucket_name, prefix) path_prefix = '/%s/' % utils.vfs_construct_path( bucket_name, self._assets_path) stats = cloudstorage.listbucket(path) files_in_dir = [] for stat in stats: # Remove the asset path from the prefix of filename. files_in_dir.append(stat.filename.replace(path_prefix, '')) return files_in_dir
def _check_filepath(self, filepath): """Raises an error if a filepath is invalid.""" base_dir = utils.vfs_construct_path( '/', self.impl.exploration_id, 'assets') absolute_path = utils.vfs_construct_path(base_dir, filepath) normalized_path = utils.vfs_normpath(absolute_path) # This check prevents directory traversal. if not normalized_path.startswith(base_dir): raise IOError('Invalid filepath: %s' % filepath)
def _check_filepath(self, filepath): """Raises an error if a filepath is invalid.""" base_dir = utils.vfs_construct_path('/', self.impl.exploration_id, 'assets') absolute_path = utils.vfs_construct_path(base_dir, filepath) normalized_path = utils.vfs_normpath(absolute_path) # This check prevents directory traversal. if not normalized_path.startswith(base_dir): raise IOError('Invalid filepath: %s' % filepath)
def test_vfs_construct_path(self): """Test vfs_construct_path method.""" p = utils.vfs_construct_path('a', 'b', 'c') self.assertEqual(p, 'a/b/c') p = utils.vfs_construct_path('a/', '/b', 'c') self.assertEqual(p, '/b/c') p = utils.vfs_construct_path('a/', 'b', 'c') self.assertEqual(p, 'a/b/c') p = utils.vfs_construct_path('a', '/b', 'c') self.assertEqual(p, '/b/c') p = utils.vfs_construct_path('/a', 'b/') self.assertEqual(p, '/a/b/')
def listdir(self, dir_name): """Lists all files in a directory. Args: dir_name: str. The directory whose files should be listed. This should not start with '/' or end with '/'. Returns: list(str). A lexicographically-sorted list of filenames, each of which is prefixed with dir_name. """ # The trailing slash is necessary to prevent non-identical directory # names with the same prefix from matching, e.g. /abcd/123.png should # not match a query for files under /abc/. prefix = '%s' % utils.vfs_construct_path('/', self._assets_path, dir_name) if not prefix.endswith('/'): prefix += '/' result = set() metadata_models = file_models.FileMetadataModel.get_undeleted() for metadata_model in metadata_models: filepath = metadata_model.id if filepath.startswith(prefix): # Because the path is /<entity>/<entity_id>/assets/abc.png. result.add('/'.join(filepath.split('/')[4:])) return sorted(list(result))
def listdir(self, dir_name): """Lists all files in a directory. Args: dir_name: str. The directory whose files should be listed. This should not start with '/' or end with '/'. Returns: list(str). A lexicographically-sorted list of filenames. """ if dir_name.startswith('/') or dir_name.endswith('/'): raise IOError( 'The dir_name should not start with / or end with / : %s' % dir_name) # The trailing slash is necessary to prevent non-identical directory # names with the same prefix from matching, e.g. /abcd/123.png should # not match a query for files under /abc/. if dir_name and not dir_name.endswith('/'): dir_name += '/' assets_path = '%s/' % self._assets_path prefix = utils.vfs_construct_path(self._assets_path, dir_name) blobs_in_dir = storage_services.listdir(self._bucket_name, prefix) return [blob.name.replace(assets_path, '') for blob in blobs_in_dir]
def listdir(self, dir_name): """Lists all files in a directory. Args: dir_name: The directory whose files should be listed. This should not start with '/' or end with '/'. Returns: List of str. This is a lexicographically-sorted list of filenames, each of which is prefixed with dir_name. """ # The trailing slash is necessary to prevent non-identical directory # names with the same prefix from matching, e.g. /abcd/123.png should # not match a query for files under /abc/. prefix = '%s' % utils.vfs_construct_path( '/', self._exploration_id, 'assets', dir_name) if not prefix.endswith('/'): prefix += '/' result = set() metadata_models = file_models.FileMetadataModel.get_undeleted() for metadata_model in metadata_models: filepath = metadata_model.id if filepath.startswith(prefix): result.add('/'.join(filepath.split('/')[3:])) return sorted(list(result))
def _check_filepath(self, filepath): """Raises an error if a filepath is invalid. Args: filepath: str. The path to the relevant file within the entity's assets folder. Raises: IOError: Invalid filepath. """ base_dir = utils.vfs_construct_path('/', self.impl.assets_path, 'assets') absolute_path = utils.vfs_construct_path(base_dir, filepath) normalized_path = utils.vfs_normpath(absolute_path) # This check prevents directory traversal. if not normalized_path.startswith(base_dir): raise IOError('Invalid filepath: %s' % filepath)
def _construct_id(cls, exploration_id, filepath): """Constructs and returns an id string uniquely identifying the given exploration and filepath. Args: exploration_id: str. The id of the exploration. filepath: str. The path to the relevant file within the exploration. Returns: str. Uniquely identifying string for the given exploration and filepath. """ return utils.vfs_construct_path('/', exploration_id, filepath)
def _construct_id(cls, assets_path, filepath): """Constructs and returns an id string uniquely identifying the given assets_path and filepath. Args: assets_path: str. The path to the assets folder for an entity. filepath: str. The path to the relevant file within the assets folder of the corresponding entity. Returns: str. Uniquely identifying string for the given assets_path and filepath (concatenation of assets_path and filepath). """ return utils.vfs_construct_path('/', assets_path, filepath)
def _construct_id(cls, exploration_id, filepath): return utils.vfs_construct_path("/", exploration_id, filepath)
def _construct_id(cls, exploration_id, filepath): return utils.vfs_construct_path('/', exploration_id, filepath)