feat: Allow deletion of music files to be disabled in the config file. Implement #202.

This commit is contained in:
Terry Geng
2020-10-24 22:13:20 +08:00
parent a1e37629a5
commit 312599e8b3
4 changed files with 29 additions and 14 deletions

View File

@ -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]

View File

@ -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)

View File

@ -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();

View File

@ -201,6 +201,7 @@
</div>
<div class="card-body">
<div id="library-group" class="list-group library-group" style="overflow: auto;">
<input type="hidden" id="deleteAllowed" value="true" />
<div id="library-item-loading" class="list-group-item library-item">
<img style="margin: auto; width: 35px;" src="static/image/loading.svg"
alt="{{ tr('aria_spinner') }}" />
@ -267,7 +268,7 @@
aria-label="{{ tr('download_song_from_library') }}">
<i class="fas fa-download" aria-hidden="true"></i>
</button>
<button class="library-item-trash btn btn-danger btn-sm btn-space" type="button"
<button class="library-item-trash btn btn-danger btn-sm btn-space library-delete" type="button"
aria-label="{{ tr('remove_song_from_library') }}">
<i class="fas fa-trash-alt"></i>
</button>
@ -296,7 +297,7 @@
<button id="library-download-btn" type="button" class="btn btn-secondary mr-1">
<i class="fas fa-download" aria-hidden="true"></i>{{ tr('download_all') }}
</button>
<button type="button" class="btn btn-danger mr-1" data-toggle="modal"
<button type="button" class="btn btn-danger mr-1 library-delete" data-toggle="modal"
data-target="#deleteWarningModal">
<i class="fas fa-trash-alt" aria-hidden="true"></i>{{ tr('delete_all') }}
</button>