mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
MTP: Implement get_file()
This commit is contained in:
parent
4785359e6a
commit
b9737b9659
@ -130,6 +130,7 @@ class MTP_DEVICE(BASE):
|
||||
del relpath_cache[relpath]
|
||||
if cached_metadata.size == mtp_file.size:
|
||||
cached_metadata.datetime = mtp_file.last_modified.timetuple()
|
||||
cached_metadata.path = mtp_file.mtp_id_path
|
||||
debug('Using cached metadata for',
|
||||
'/'.join(mtp_file.full_path))
|
||||
continue # No need to update metadata
|
||||
@ -150,6 +151,7 @@ class MTP_DEVICE(BASE):
|
||||
traceback.print_exc()
|
||||
book.size = mtp_file.size
|
||||
book.datetime = mtp_file.last_modified.timetuple()
|
||||
book.path = mtp_file.mtp_id_path
|
||||
|
||||
# Remove books in the cache that no longer exist
|
||||
for idx in sorted(relpath_cache.itervalues(), reverse=True):
|
||||
@ -197,6 +199,10 @@ class MTP_DEVICE(BASE):
|
||||
|
||||
# }}}
|
||||
|
||||
def get_file(self, path, outfile, end_session=True):
|
||||
f = self.filesystem_cache.resolve_mtp_id_path(path)
|
||||
self.get_mtp_file(f, outfile)
|
||||
|
||||
def create_upload_path(self, path, mdata, fname):
|
||||
from calibre.devices import create_upload_path
|
||||
from calibre.utils.filenames import ascii_filename as sanitize
|
||||
|
@ -7,7 +7,7 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import weakref, sys
|
||||
import weakref, sys, json
|
||||
from collections import deque
|
||||
from operator import attrgetter
|
||||
from future_builtins import map
|
||||
@ -166,6 +166,10 @@ class FileOrFolder(object):
|
||||
def mtp_relpath(self):
|
||||
return tuple(x.lower() for x in self.full_path[1:])
|
||||
|
||||
@property
|
||||
def mtp_id_path(self):
|
||||
return 'mtp:::' + json.dumps(self.object_id) + ':::' + '/'.join(self.full_path)
|
||||
|
||||
class FilesystemCache(object):
|
||||
|
||||
def __init__(self, all_storage, entries):
|
||||
@ -215,4 +219,19 @@ class FilesystemCache(object):
|
||||
if x.storage_id == storage_id and x.is_ebook:
|
||||
yield x
|
||||
|
||||
def resolve_mtp_id_path(self, path):
|
||||
if not path.startswith('mtp:::'):
|
||||
raise ValueError('%s is not a valid MTP path'%path)
|
||||
parts = path.split(':::')
|
||||
if len(parts) < 3:
|
||||
raise ValueError('%s is not a valid MTP path'%path)
|
||||
try:
|
||||
object_id = json.loads(parts[1])
|
||||
except:
|
||||
raise ValueError('%s is not a valid MTP path'%path)
|
||||
try:
|
||||
return self.id_map[object_id]
|
||||
except KeyError:
|
||||
raise ValueError('No object found with MTP path: %s'%path)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user