def browse(self, uri): logger.debug("Browsing files at: %s", uri) result = [] local_path = path.uri_to_path(uri) if str(local_path) == "root": return list(self._get_media_dirs_refs()) if not self._is_in_basedir(local_path): logger.warning( "Rejected attempt to browse path (%s) outside dirs defined " "in file/media_dirs config.", uri, ) return [] if path.uri_to_path(uri).is_file(): logger.error("Rejected attempt to browse file (%s)", uri) return [] for dir_entry in local_path.iterdir(): child_path = dir_entry.resolve() uri = path.path_to_uri(child_path) if not self._show_dotfiles and dir_entry.name.startswith("."): continue if (self._excluded_file_extensions and dir_entry.suffix in self._excluded_file_extensions): continue if child_path.is_symlink() and not self._follow_symlinks: logger.debug("Ignoring symlink: %s", uri) continue if not self._is_in_basedir(child_path): logger.debug("Ignoring symlink to outside base dir: %s", uri) continue if child_path.is_dir(): result.append( models.Ref.directory(name=dir_entry.name, uri=uri)) elif child_path.is_file(): result.append(models.Ref.track(name=dir_entry.name, uri=uri)) def order(item): return (item.type != models.Ref.DIRECTORY, item.name) result.sort(key=order) return result
def local_uri_to_path(uri, media_dir): """Convert local track or directory URI to absolute path.""" if (not uri.startswith('local:directory:') and not uri.startswith('local:track:')): raise ValueError('Invalid URI.') file_path = path.uri_to_path(uri).split(b':', 1)[1] return os.path.join(media_dir, file_path)
def local_uri_to_path(uri, media_dir): """Convert local track or directory URI to absolute path.""" if ( not uri.startswith('local:directory:') and not uri.startswith('local:track:')): raise ValueError('Invalid URI.') file_path = path.uri_to_path(uri).split(b':', 1)[1] return os.path.join(media_dir, file_path)
def lookup(self, uri): logger.debug("Looking up file URI: %s", uri) local_path = path.uri_to_path(uri) try: result = self._scanner.scan(uri) track = tags.convert_tags_to_track(result.tags).replace( uri=uri, length=result.duration) except exceptions.ScannerError as e: logger.warning("Failed looking up %s: %s", uri, e) track = models.Track(uri=uri) if not track.name: track = track.replace(name=local_path.name) return [track]
def browse(self, uri): logger.debug('Browsing files at: %s', uri) result = [] local_path = path.uri_to_path(uri) if local_path == 'root': return list(self._get_media_dirs_refs()) if not self._is_in_basedir(os.path.realpath(local_path)): logger.warning( 'Rejected attempt to browse path (%s) outside dirs defined ' 'in file/media_dirs config.', uri) return [] for dir_entry in os.listdir(local_path): child_path = os.path.join(local_path, dir_entry) uri = path.path_to_uri(child_path) if not self._show_dotfiles and dir_entry.startswith(b'.'): continue if (self._excluded_file_extensions and dir_entry.endswith(self._excluded_file_extensions)): continue if os.path.islink(child_path) and not self._follow_symlinks: logger.debug('Ignoring symlink: %s', uri) continue if not self._is_in_basedir(os.path.realpath(child_path)): logger.debug('Ignoring symlink to outside base dir: %s', uri) continue name = dir_entry.decode(FS_ENCODING, 'replace') if os.path.isdir(child_path): result.append(models.Ref.directory(name=name, uri=uri)) elif os.path.isfile(child_path): result.append(models.Ref.track(name=name, uri=uri)) def order(item): return (item.type != models.Ref.DIRECTORY, item.name) result.sort(key=order) return result
def lookup(self, uri): logger.debug('Looking up file URI: %s', uri) local_path = path.uri_to_path(uri) try: result = self._scanner.scan(uri) track = tags.convert_tags_to_track(result.tags).copy( uri=uri, length=result.duration) except exceptions.ScannerError as e: logger.warning('Failed looking up %s: %s', uri, e) track = models.Track(uri=uri) if not track.name: filename = os.path.basename(local_path) name = urllib2.unquote(filename).decode(FS_ENCODING, 'replace') track = track.copy(name=name) return [track]
def playlist_uri_to_path(uri, playlists_dir): if not uri.startswith('m3u:'): raise ValueError('Invalid URI %s' % uri) file_path = path.uri_to_path(uri) return os.path.join(playlists_dir, file_path)
def uri_to_path(uri): """Convert URI to file path.""" return path.uri_to_path(uri)
def test_latin1_in_uri(self): result = path.uri_to_path('file:///tmp/%E6%F8%E5') self.assertEqual(result, '/tmp/æøå'.encode('latin-1'))
def test_unicode_in_uri(self): result = path.uri_to_path('file:///tmp/%C3%A6%C3%B8%C3%A5') self.assertEqual(result, '/tmp/æøå'.encode('utf-8'))
def test_space_in_uri(self): result = path.uri_to_path('file:///tmp/test%20this') self.assertEqual(result, '/tmp/test this'.encode('utf-8'))
def test_simple_uri(self): result = path.uri_to_path('file:///etc/fstab') self.assertEqual(result, '/etc/fstab'.encode('utf-8'))
def test_latin1_in_uri(self): result = path.uri_to_path("file:///tmp/%E6%F8%E5") assert bytes(result) == b"/tmp/\xe6\xf8\xe5"
def test_unicode_in_uri(self): result = path.uri_to_path("file:///tmp/%C3%A6%C3%B8%C3%A5") assert result == pathlib.Path("/tmp/æøå")
def test_space_in_uri(self): result = path.uri_to_path("file:///tmp/test%20this") assert result == pathlib.Path("/tmp/test this")
def test_simple_uri(self): result = path.uri_to_path("file:///etc/fstab") assert result == pathlib.Path("/etc/fstab")