Add option to upload or browse for a database file to import

This commit is contained in:
JonnyWong16 2020-05-03 14:33:25 -07:00
commit 980c4f7618
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
6 changed files with 316 additions and 79 deletions

View file

@ -42,6 +42,7 @@ import os
import re
import shlex
import socket
import string
import sys
import time
import unicodedata
@ -1197,3 +1198,64 @@ def user_page(user_id=None, user=None):
params['user'] = user
return params
def browse_path(path=None, include_hidden=False, filter_ext=''):
output = []
if not os.path.isdir(path):
return output
if path != os.path.dirname(path):
parent_path = os.path.dirname(path)
out = {
'key': base64.b64encode(parent_path.encode('UTF-8')),
'path': parent_path,
'title': '..',
'type': 'folder',
'icon': 'level-up-alt'
}
output.append(out)
elif os.name == 'nt':
drives = ['%s:\\' % d for d in string.ascii_uppercase if os.path.exists('%s:' % d)]
for drive in drives:
out = {
'key': base64.b64encode(drive.encode('UTF-8')),
'path': drive,
'title': drive,
'type': 'folder',
'icon': 'level-up-alt'
}
output.append(out)
for root, dirs, files in os.walk(path):
for d in dirs:
if not include_hidden and d.startswith('.'):
continue
dir_path = os.path.join(root, d)
out = {
'key': base64.b64encode(dir_path.encode('UTF-8')),
'path': dir_path,
'title': d,
'type': 'folder',
'icon': 'folder'
}
output.append(out)
for f in files:
if not include_hidden and f.startswith('.'):
continue
if filter_ext and not f.endswith(filter_ext):
continue
file_path = os.path.join(root, f)
out = {
'key': base64.b64encode(file_path.encode('UTF-8')),
'path': file_path,
'title': f,
'type': 'file',
'icon': 'file'
}
output.append(out)
break
return output

View file

@ -21,6 +21,7 @@ from future.builtins import object
from future.builtins import str
from io import open
import base64
import json
import linecache
import os
@ -3740,13 +3741,15 @@ class WebInterface(object):
@cherrypy.expose
@requireAuth(member_of("admin"))
@addtoapi()
def import_database(self, app=None, database_path=None, method=None, backup=True,
def import_database(self, app=None, database_file=None, database_path=None, method=None, backup=True,
table_name=None, import_ignore_interval=0, **kwargs):
""" Import a Tautulli, PlexWatch, or Plexivity database into Tautulli.
```
Required parameters:
app (str): "tautulli" or "plexwatch" or "plexivity"
database_file (file): The database file to import (multipart/form-data)
or
database_path (str): The full path to the plexwatch database file
method (str): For Tautulli only, "merge" or "overwrite"
table_name (str): For PlexWatch or Plexivity only, "processed" or "grouped"
@ -3765,6 +3768,20 @@ class WebInterface(object):
if not app:
return 'No app specified for import'
if database_file:
database_path = os.path.join(plexpy.CONFIG.CACHE_DIR, database_file.filename)
logger.info("Received database file '%s' for import. Saving to cache '%s'.",
database_file.filename, database_path)
with open(database_path, 'wb') as f:
while True:
data = database_file.file.read(8192)
if not data:
break
f.write(data)
if not database_path:
return 'No database specified for import'
if app.lower() == 'tautulli':
db_check_msg = database.validate_database(database=database_path)
if db_check_msg == 'success':
@ -3816,6 +3833,21 @@ class WebInterface(object):
logger.warn("No app specified for import.")
return
@cherrypy.expose
@cherrypy.tools.json_out()
@requireAuth(member_of("admin"))
def browse_path(self, key=None, path=None, filter_ext=''):
if key:
path = base64.b64decode(key)
if not path:
path = plexpy.DATA_DIR
data = helpers.browse_path(path=path, filter_ext=filter_ext)
if data:
return {'result': 'success', 'path': path, 'data': data}
else:
return {'result': 'error', 'message': 'Invalid path.'}
@cherrypy.expose
@cherrypy.tools.json_out()
@requireAuth(member_of("admin"))