Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
feat: Allow deletion of music files to be disabled in the config file…
Browse files Browse the repository at this point in the history
…. Implement #202.
  • Loading branch information
TerryGeng committed Oct 24, 2020
1 parent a1e3762 commit 312599e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
2 changes: 2 additions & 0 deletions configuration.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
28 changes: 16 additions & 12 deletions interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
))
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions web/js/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions web/templates/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ <h3 class="card-title">{{ tr('filters') }}</h3>
</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') }}" />
Expand Down Expand Up @@ -267,7 +268,7 @@ <h3 class="card-title">{{ tr('filters') }}</h3>
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>
Expand Down Expand Up @@ -296,7 +297,7 @@ <h3 class="card-title">{{ tr('filters') }}</h3>
<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>
Expand Down

0 comments on commit 312599e

Please sign in to comment.