From 312599e8b36720ac5ef8c663eaedfba922c403b5 Mon Sep 17 00:00:00 2001 From: Terry Geng Date: Sat, 24 Oct 2020 22:13:20 +0800 Subject: [PATCH] feat: Allow deletion of music files to be disabled in the config file. Implement #202. --- configuration.example.ini | 2 ++ interface.py | 28 ++++++++++++++++------------ web/js/main.mjs | 8 ++++++++ web/templates/index.template.html | 5 +++-- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/configuration.example.ini b/configuration.example.ini index 8677e531..27b68afd 100644 --- a/configuration.example.ini +++ b/configuration.example.ini @@ -151,8 +151,10 @@ port = 64738 # 'upload_enabled': Enable the upload function of the web interface. # 'maximum_upload_file_size': Unit can be 'B', 'KB', 'MB', 'GB', 'TB'. +# 'delete_allowed': Allow users to delete a file from the library(hard disk). #upload_enabled = True #max_upload_file_size = 30MB +#delete_allowded = True # [debug] stores some debug settings. [debug] diff --git a/interface.py b/interface.py index 7b57c221..064b127a 100644 --- a/interface.py +++ b/interface.py @@ -562,6 +562,7 @@ def library_info(): return jsonify(dict( dirs=get_all_dirs(), upload_enabled=var.config.getboolean("webinterface", "upload_enabled", fallback=True), + delete_allowed=var.config.getboolean("webinterface", "delete_allowed", fallback=True), tags=tags, max_upload_file_size=max_upload_file_size )) @@ -605,22 +606,25 @@ def library(): return redirect("./", code=302) elif request.form['action'] == 'delete': - items = dicts_to_items(var.music_db.query_music(condition)) - for item in items: - var.playlist.remove_by_id(item.id) - item = var.cache.get_item_by_id(item.id) + if var.config.getboolean("webinterface", "delete_allowed", fallback=True): + items = dicts_to_items(var.music_db.query_music(condition)) + for item in items: + var.playlist.remove_by_id(item.id) + item = var.cache.get_item_by_id(item.id) - if os.path.isfile(item.uri()): - log.info("web: delete file " + item.uri()) - os.remove(item.uri()) + if os.path.isfile(item.uri()): + log.info("web: delete file " + item.uri()) + os.remove(item.uri()) - var.cache.free_and_delete(item.id) + var.cache.free_and_delete(item.id) - if len(os.listdir(var.music_folder + request.form['dir'])) == 0: - os.rmdir(var.music_folder + request.form['dir']) + if len(os.listdir(var.music_folder + request.form['dir'])) == 0: + os.rmdir(var.music_folder + request.form['dir']) - time.sleep(0.1) - return redirect("./", code=302) + time.sleep(0.1) + return redirect("./", code=302) + else: + abort(403) else: page_count = math.ceil(total_count / ITEM_PER_PAGE) diff --git a/web/js/main.mjs b/web/js/main.mjs index 2fb708da..b6e51efe 100644 --- a/web/js/main.mjs +++ b/web/js/main.mjs @@ -528,6 +528,14 @@ function displayLibraryControls(data){ $("#upload").hide(); } + if (data.delete_allowed) { + $("#deleteAllowed").val("true"); + $(".library-delete").show(); + } else { + $("#uploadDisabled").val("false"); + $(".library-delete").hide(); + } + let select = $("#filter-dir"); let dataList = $("#upload-target-dirs"); select.find("option").remove(); diff --git a/web/templates/index.template.html b/web/templates/index.template.html index 5f4e5d8c..85da6f53 100644 --- a/web/templates/index.template.html +++ b/web/templates/index.template.html @@ -201,6 +201,7 @@

{{ tr('filters') }}

+
{{ tr('aria_spinner') }} @@ -267,7 +268,7 @@

{{ tr('filters') }}

aria-label="{{ tr('download_song_from_library') }}"> - @@ -296,7 +297,7 @@

{{ tr('filters') }}

-