Make drive letters available in file picker

This commit is contained in:
Ozzie Isaacs 2022-10-03 15:17:17 +02:00
parent e22ecda137
commit 7eef44f73c

46
cps/admin.py Executable file → Normal file
View File

@ -26,11 +26,12 @@ import base64
import json
import operator
import time
import sys
import string
from datetime import datetime, timedelta
from datetime import time as datetime_time
from functools import wraps
from flask import Blueprint, flash, redirect, url_for, abort, request, make_response, send_from_directory, g, Response
from flask_login import login_required, current_user, logout_user, confirm_login
from flask_babel import gettext as _
@ -52,7 +53,6 @@ from .services.worker import WorkerThread
from .babel import get_available_translations, get_available_locale, get_user_locale_language
from . import debug_info
log = logger.create()
feature_support = {
@ -67,12 +67,14 @@ feature_support = {
try:
import rarfile # pylint: disable=unused-import
feature_support['rar'] = True
except (ImportError, SyntaxError):
feature_support['rar'] = False
try:
from .oauth_bb import oauth_check, oauthblueprints
feature_support['oauth'] = True
except ImportError as err:
log.debug('Cannot import Flask-Dance, login with Oauth will not work: %s', err)
@ -80,7 +82,6 @@ except ImportError as err:
oauthblueprints = []
oauth_check = {}
admi = Blueprint('admin', __name__)
@ -159,6 +160,7 @@ def shutdown():
show_text['text'] = _(u'Unknown command')
return json.dumps(show_text), 400
@admi.route("/metadata_backup", methods=["POST"])
@login_required
@admin_required
@ -978,6 +980,19 @@ def prepare_tags(user, action, tags_name, id_list):
return ",".join(saved_tags_list)
def get_drives(current):
drive_letters = []
for d in string.ascii_uppercase:
if os.path.exists('{}:'.format(d)) and current[0].lower() != d.lower():
drive = "{}:\\".format(d)
data = {"name": drive, "fullpath": drive}
data["sort"] = "_" + data["fullpath"].lower()
data["type"] = "dir"
data["size"] = ""
drive_letters.append(data)
return drive_letters
def pathchooser():
browse_for = "folder"
folder_only = request.args.get('folder', False) == "true"
@ -985,40 +1000,41 @@ def pathchooser():
path = os.path.normpath(request.args.get('path', ""))
if os.path.isfile(path):
oldfile = path
old_file = path
path = os.path.dirname(path)
else:
oldfile = ""
old_file = ""
absolute = False
if os.path.isdir(path):
# if os.path.isabs(path):
cwd = os.path.realpath(path)
absolute = True
# else:
# cwd = os.path.relpath(path)
else:
cwd = os.getcwd()
cwd = os.path.normpath(os.path.realpath(cwd))
parentdir = os.path.dirname(cwd)
parent_dir = os.path.dirname(cwd)
if not absolute:
if os.path.realpath(cwd) == os.path.realpath("/"):
cwd = os.path.relpath(cwd)
else:
cwd = os.path.relpath(cwd) + os.path.sep
parentdir = os.path.relpath(parentdir) + os.path.sep
parent_dir = os.path.relpath(parent_dir) + os.path.sep
if os.path.realpath(cwd) == os.path.realpath("/"):
parentdir = ""
files = []
if os.path.realpath(cwd) == os.path.realpath("/") \
or (sys.platform == "win32" and os.path.realpath(cwd)[1:] == os.path.realpath("/")[1:]):
# we are in root
parent_dir = ""
if sys.platform == "win32":
files = get_drives(cwd)
try:
folders = os.listdir(cwd)
except Exception:
folders = []
files = []
for f in folders:
try:
data = {"name": f, "fullpath": os.path.join(cwd, f)}
@ -1051,9 +1067,9 @@ def pathchooser():
context = {
"cwd": cwd,
"files": files,
"parentdir": parentdir,
"parentdir": parent_dir,
"type": browse_for,
"oldfile": oldfile,
"oldfile": old_file,
"absolute": absolute,
}
return json.dumps(context)