2019-08-28 22:37:56 +02:00
|
|
|
from __future__ import unicode_literals
|
2014-07-30 22:16:58 +02:00
|
|
|
import zipfile
|
2017-12-29 20:22:49 +01:00
|
|
|
import os.path
|
2017-01-31 15:08:00 +01:00
|
|
|
import sys
|
2019-11-25 22:48:42 +01:00
|
|
|
import xbmc
|
|
|
|
import xbmcvfs
|
|
|
|
import xbmcgui
|
2019-11-26 19:58:35 +01:00
|
|
|
from dropbox import dropbox
|
2019-08-27 21:55:22 +02:00
|
|
|
from . import utils as utils
|
2019-11-26 19:58:35 +01:00
|
|
|
from dropbox.files import WriteMode, CommitInfo, UploadSessionCursor
|
2019-09-05 21:30:06 +02:00
|
|
|
from . authorizers import DropboxAuthorizer
|
2012-10-26 16:42:10 +02:00
|
|
|
|
2019-11-25 22:56:59 +01:00
|
|
|
|
2012-10-26 16:42:10 +02:00
|
|
|
class Vfs:
|
2012-11-04 21:39:17 +01:00
|
|
|
root_path = None
|
2012-10-26 16:42:10 +02:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def __init__(self, rootString):
|
2013-01-28 22:44:22 +01:00
|
|
|
self.set_root(rootString)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-12-30 17:09:01 +01:00
|
|
|
def clean_path(self, path):
|
2019-11-25 22:19:57 +01:00
|
|
|
# fix slashes
|
2019-12-30 17:09:01 +01:00
|
|
|
path = path.replace("\\", "/")
|
2019-12-30 17:17:58 +01:00
|
|
|
|
2019-11-25 22:19:57 +01:00
|
|
|
# check if trailing slash is included
|
2019-12-30 17:09:01 +01:00
|
|
|
if(path[-1:] != '/'):
|
|
|
|
path = path + '/'
|
2019-12-30 17:17:58 +01:00
|
|
|
|
2019-12-30 17:09:01 +01:00
|
|
|
return path
|
|
|
|
|
|
|
|
def set_root(self, rootString):
|
|
|
|
old_root = self.root_path
|
|
|
|
self.root_path = self.clean_path(rootString)
|
2012-11-05 18:13:48 +01:00
|
|
|
|
2019-11-25 22:19:57 +01:00
|
|
|
# return the old root
|
2012-11-05 18:13:48 +01:00
|
|
|
return old_root
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def listdir(self, directory):
|
2012-10-26 16:42:10 +02:00
|
|
|
return {}
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def mkdir(self, directory):
|
2012-10-26 16:42:10 +02:00
|
|
|
return True
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def put(self, source, dest):
|
2012-10-26 16:42:10 +02:00
|
|
|
return True
|
2012-11-02 21:59:40 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def rmdir(self, directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
return True
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def rmfile(self, aFile):
|
2014-08-01 20:12:43 +02:00
|
|
|
return True
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def exists(self, aFile):
|
2012-11-02 21:59:40 +01:00
|
|
|
return True
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def rename(self, aFile, newName):
|
2014-07-30 22:16:58 +02:00
|
|
|
return True
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2014-07-30 22:16:58 +02:00
|
|
|
def cleanup(self):
|
|
|
|
return True
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-12-10 22:16:54 +01:00
|
|
|
def fileSize(self, filename):
|
|
|
|
return 0 # result should be in KB
|
|
|
|
|
2019-11-25 22:56:59 +01:00
|
|
|
|
2012-11-02 21:59:40 +01:00
|
|
|
class XBMCFileSystem(Vfs):
|
2013-01-28 22:44:22 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def listdir(self, directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
return xbmcvfs.listdir(directory)
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def mkdir(self, directory):
|
2020-11-18 21:07:06 +01:00
|
|
|
return xbmcvfs.mkdir(xbmcvfs.translatePath(directory))
|
2012-11-02 21:59:40 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def put(self, source, dest):
|
2020-11-18 21:07:06 +01:00
|
|
|
return xbmcvfs.copy(xbmcvfs.translatePath(source), xbmcvfs.translatePath(dest))
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def rmdir(self, directory):
|
|
|
|
return xbmcvfs.rmdir(directory, True)
|
2012-11-02 21:59:40 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def rmfile(self, aFile):
|
2014-08-01 20:12:43 +02:00
|
|
|
return xbmcvfs.delete(aFile)
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def rename(self, aFile, newName):
|
2014-07-30 22:16:58 +02:00
|
|
|
return xbmcvfs.rename(aFile, newName)
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def exists(self, aFile):
|
2012-11-02 21:59:40 +01:00
|
|
|
return xbmcvfs.exists(aFile)
|
|
|
|
|
2019-12-10 22:16:54 +01:00
|
|
|
def fileSize(self, filename):
|
2019-12-31 17:49:16 +01:00
|
|
|
with xbmcvfs.File(filename) as f:
|
|
|
|
result = f.size() / 1024 # bytes to kilobytes
|
2019-12-10 22:16:54 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2019-11-25 22:56:59 +01:00
|
|
|
|
2014-07-30 22:16:58 +02:00
|
|
|
class ZipFileSystem(Vfs):
|
|
|
|
zip = None
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def __init__(self, rootString, mode):
|
2014-07-30 22:16:58 +02:00
|
|
|
self.root_path = ""
|
2019-11-25 22:45:41 +01:00
|
|
|
self.zip = zipfile.ZipFile(rootString, mode=mode, compression=zipfile.ZIP_DEFLATED, allowZip64=True)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def listdir(self, directory):
|
|
|
|
return [[], []]
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def mkdir(self, directory):
|
2019-11-25 22:19:57 +01:00
|
|
|
# self.zip.write(directory[len(self.root_path):])
|
2014-08-01 19:57:55 +02:00
|
|
|
return False
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def put(self, source, dest):
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2020-11-18 21:07:06 +01:00
|
|
|
aFile = xbmcvfs.File(xbmcvfs.translatePath(source), 'r')
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
self.zip.writestr(dest, aFile.readBytes())
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2014-07-30 22:16:58 +02:00
|
|
|
return True
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def rmdir(self, directory):
|
2014-08-01 19:57:55 +02:00
|
|
|
return False
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def exists(self, aFile):
|
2014-08-01 19:57:55 +02:00
|
|
|
return False
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2014-07-30 22:16:58 +02:00
|
|
|
def cleanup(self):
|
|
|
|
self.zip.close()
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def extract(self, aFile, path):
|
2019-11-25 22:19:57 +01:00
|
|
|
# extract zip file to path
|
2019-11-25 22:45:41 +01:00
|
|
|
self.zip.extract(aFile, path)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2015-06-26 22:32:22 +02:00
|
|
|
def listFiles(self):
|
|
|
|
return self.zip.infolist()
|
2014-07-30 22:16:58 +02:00
|
|
|
|
2019-11-25 22:56:59 +01:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
class DropboxFileSystem(Vfs):
|
2019-11-25 22:56:59 +01:00
|
|
|
MAX_CHUNK = 50 * 1000 * 1000 # dropbox uses 150, reduced to 50 for small mem systems
|
2012-11-05 19:25:25 +01:00
|
|
|
client = None
|
2014-10-09 21:30:39 +02:00
|
|
|
APP_KEY = ''
|
|
|
|
APP_SECRET = ''
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def __init__(self, rootString):
|
2013-03-04 17:22:09 +01:00
|
|
|
self.set_root(rootString)
|
2013-01-28 22:44:22 +01:00
|
|
|
|
2017-01-31 15:08:00 +01:00
|
|
|
authorizer = DropboxAuthorizer()
|
2012-11-05 19:25:25 +01:00
|
|
|
|
2017-01-31 15:08:00 +01:00
|
|
|
if(authorizer.isAuthorized()):
|
|
|
|
self.client = authorizer.getClient()
|
2012-11-05 19:25:25 +01:00
|
|
|
else:
|
2019-11-25 22:19:57 +01:00
|
|
|
# tell the user to go back and run the authorizer
|
2019-11-25 22:45:41 +01:00
|
|
|
xbmcgui.Dialog().ok(utils.getString(30010), utils.getString(30105))
|
2017-01-31 15:08:00 +01:00
|
|
|
sys.exit()
|
2012-11-05 19:25:25 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def listdir(self, directory):
|
2017-12-04 00:32:21 +01:00
|
|
|
directory = self._fix_slashes(directory)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:56:59 +01:00
|
|
|
if(self.client is not None and self.exists(directory)):
|
2012-11-05 22:46:17 +01:00
|
|
|
files = []
|
|
|
|
dirs = []
|
2017-12-04 00:32:21 +01:00
|
|
|
metadata = self.client.files_list_folder(directory)
|
2012-11-05 22:46:17 +01:00
|
|
|
|
2017-12-04 00:32:21 +01:00
|
|
|
for aFile in metadata.entries:
|
2019-11-25 22:45:41 +01:00
|
|
|
if(isinstance(aFile, dropbox.files.FolderMetadata)):
|
2019-08-28 22:37:56 +02:00
|
|
|
dirs.append(aFile.name)
|
2012-11-05 22:46:17 +01:00
|
|
|
else:
|
2019-08-28 22:37:56 +02:00
|
|
|
files.append(aFile.name)
|
2012-11-05 22:46:17 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
return [dirs, files]
|
2012-11-05 22:46:17 +01:00
|
|
|
else:
|
2019-11-25 22:45:41 +01:00
|
|
|
return [[], []]
|
2012-11-05 22:46:17 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def mkdir(self, directory):
|
2013-03-04 17:22:09 +01:00
|
|
|
directory = self._fix_slashes(directory)
|
2019-11-25 22:56:59 +01:00
|
|
|
if(self.client is not None):
|
2019-11-25 22:19:57 +01:00
|
|
|
# sort of odd but always return true, folder create is implicit with file upload
|
2012-11-05 19:25:25 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def rmdir(self, directory):
|
2013-03-04 17:22:09 +01:00
|
|
|
directory = self._fix_slashes(directory)
|
2019-11-25 22:56:59 +01:00
|
|
|
if(self.client is not None and self.exists(directory)):
|
2019-11-25 22:19:57 +01:00
|
|
|
# dropbox is stupid and will refuse to do this sometimes, need to delete recursively
|
2019-11-25 22:45:41 +01:00
|
|
|
dirs, files = self.listdir(directory)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2014-07-29 15:43:34 +02:00
|
|
|
for aDir in dirs:
|
|
|
|
self.rmdir(aDir)
|
|
|
|
|
2019-11-25 22:19:57 +01:00
|
|
|
# finally remove the root directory
|
2017-12-04 00:32:21 +01:00
|
|
|
self.client.files_delete(directory)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2014-08-01 20:12:43 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def rmfile(self, aFile):
|
2014-08-01 20:12:43 +02:00
|
|
|
aFile = self._fix_slashes(aFile)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:56:59 +01:00
|
|
|
if(self.client is not None and self.exists(aFile)):
|
2017-12-04 00:32:21 +01:00
|
|
|
self.client.files_delete(aFile)
|
2014-08-01 20:12:43 +02:00
|
|
|
return True
|
2012-11-05 22:46:17 +01:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def exists(self, aFile):
|
2013-03-04 17:22:09 +01:00
|
|
|
aFile = self._fix_slashes(aFile)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:56:59 +01:00
|
|
|
if(self.client is not None):
|
2019-11-25 22:19:57 +01:00
|
|
|
# can't list root metadata
|
2017-12-04 00:32:21 +01:00
|
|
|
if(aFile == ''):
|
|
|
|
return True
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
try:
|
2019-11-25 22:56:59 +01:00
|
|
|
self.client.files_get_metadata(aFile)
|
2019-11-25 22:19:57 +01:00
|
|
|
# if we make it here the file does exist
|
2012-11-05 19:25:25 +01:00
|
|
|
return True
|
2012-11-04 21:39:17 +01:00
|
|
|
except:
|
2012-11-05 19:25:25 +01:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
2012-11-04 21:39:17 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def put(self, source, dest, retry=True):
|
2013-03-04 17:22:09 +01:00
|
|
|
dest = self._fix_slashes(dest)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:56:59 +01:00
|
|
|
if(self.client is not None):
|
2019-11-25 22:19:57 +01:00
|
|
|
# open the file and get its size
|
2019-11-25 22:45:41 +01:00
|
|
|
f = open(source, 'rb')
|
2017-12-29 20:22:49 +01:00
|
|
|
f_size = os.path.getsize(source)
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2012-11-05 22:46:17 +01:00
|
|
|
try:
|
2017-12-29 20:22:49 +01:00
|
|
|
if(f_size < self.MAX_CHUNK):
|
2019-11-25 22:19:57 +01:00
|
|
|
# use the regular upload
|
2019-11-25 22:56:59 +01:00
|
|
|
self.client.files_upload(f.read(), dest, mode=WriteMode('overwrite'))
|
2017-12-29 20:22:49 +01:00
|
|
|
else:
|
2019-11-25 22:19:57 +01:00
|
|
|
# start the upload session
|
2017-12-29 20:22:49 +01:00
|
|
|
upload_session = self.client.files_upload_session_start(f.read(self.MAX_CHUNK))
|
2019-11-25 22:45:41 +01:00
|
|
|
upload_cursor = UploadSessionCursor(upload_session.session_id, f.tell())
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2017-12-29 20:22:49 +01:00
|
|
|
while(f.tell() < f_size):
|
2019-11-25 22:19:57 +01:00
|
|
|
# check if we should finish the upload
|
2017-12-29 20:22:49 +01:00
|
|
|
if((f_size - f.tell()) <= self.MAX_CHUNK):
|
2019-11-25 22:19:57 +01:00
|
|
|
# upload and close
|
2019-11-25 22:45:41 +01:00
|
|
|
self.client.files_upload_session_finish(f.read(self.MAX_CHUNK), upload_cursor, CommitInfo(dest, mode=WriteMode('overwrite')))
|
2017-12-29 20:22:49 +01:00
|
|
|
else:
|
2019-11-25 22:19:57 +01:00
|
|
|
# upload a part and store the offset
|
2019-11-25 22:45:41 +01:00
|
|
|
self.client.files_upload_session_append_v2(f.read(self.MAX_CHUNK), upload_cursor)
|
2017-12-29 20:22:49 +01:00
|
|
|
upload_cursor.offset = f.tell()
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:56:59 +01:00
|
|
|
# if no errors we're good!
|
2012-11-05 22:46:17 +01:00
|
|
|
return True
|
2017-12-29 20:22:49 +01:00
|
|
|
except Exception as anError:
|
|
|
|
utils.log(str(anError))
|
2019-11-25 22:33:34 +01:00
|
|
|
|
2019-11-25 22:19:57 +01:00
|
|
|
# if we have an exception retry
|
2014-06-03 20:34:02 +02:00
|
|
|
if(retry):
|
2019-11-25 22:45:41 +01:00
|
|
|
return self.put(source, dest, False)
|
2014-06-03 20:34:02 +02:00
|
|
|
else:
|
2019-11-25 22:19:57 +01:00
|
|
|
# tried once already, just quit
|
2014-06-03 20:34:02 +02:00
|
|
|
return False
|
2012-11-05 19:25:25 +01:00
|
|
|
else:
|
|
|
|
return False
|
2012-11-06 18:37:39 +01:00
|
|
|
|
2019-12-10 22:16:54 +01:00
|
|
|
def fileSize(self, filename):
|
|
|
|
result = 0
|
|
|
|
aFile = self._fix_slashes(filename)
|
|
|
|
|
|
|
|
if(self.client is not None):
|
|
|
|
metadata = self.client.files_get_metadata(aFile)
|
|
|
|
result = metadata.size / 1024 # bytes to KB
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def get_file(self, source, dest):
|
2019-11-25 22:56:59 +01:00
|
|
|
if(self.client is not None):
|
2019-11-25 22:19:57 +01:00
|
|
|
# write the file locally
|
2019-11-25 22:56:59 +01:00
|
|
|
self.client.files_download_to_file(dest, source)
|
2013-03-04 17:27:25 +01:00
|
|
|
return True
|
2012-11-06 18:37:39 +01:00
|
|
|
else:
|
|
|
|
return False
|
2013-03-04 17:22:09 +01:00
|
|
|
|
2019-11-25 22:45:41 +01:00
|
|
|
def _fix_slashes(self, filename):
|
|
|
|
result = filename.replace('\\', '/')
|
2017-12-04 00:32:21 +01:00
|
|
|
|
2019-11-25 22:19:57 +01:00
|
|
|
# root needs to be a blank string
|
2017-12-04 00:32:21 +01:00
|
|
|
if(result == '/'):
|
|
|
|
result = ""
|
|
|
|
|
2019-11-25 22:19:57 +01:00
|
|
|
# if dir ends in slash, remove it
|
2017-12-08 15:46:12 +01:00
|
|
|
if(result[-1:] == "/"):
|
|
|
|
result = result[:-1]
|
|
|
|
|
2017-12-04 00:32:21 +01:00
|
|
|
return result
|