#!/bin/sh . /lib/functions.sh . /usr/share/libubox/jshn.sh readonly bg_path="/www/luci-static/argon/background" readonly tmp_path="/tmp/argon_background.tmp" valid_filename() { case "$1" in ""|"."|".."|*/*|*\\*) return 1 ;; esac return 0 } result() { printf '{ "result": %s }\n' "$1" } case "$1" in "list") json_init json_add_object "avail" json_close_object json_add_object "remove" json_add_string "filename" "filename" json_close_object json_add_object "rename" json_add_string "newname" "filename" json_close_object json_dump json_cleanup ;; "call") case "$2" in "avail") json_init json_add_int "avail" "$(df | grep -E '/$' | awk '{print $4}')" json_dump json_cleanup ;; "remove") read -r input json_load "$input" || { result 255; exit 0; } json_get_var filename "filename" json_cleanup if ! valid_filename "$filename"; then result 255 exit 0 fi if [ -d "$bg_path/$filename" ]; then result 255 elif rm -f "$bg_path/$filename" 2>/dev/null; then result 0 else result 1 fi ;; "rename") read -r input json_load "$input" || { result 255; exit 0; } json_get_var newname "newname" json_cleanup if ! valid_filename "$newname" || [ -L "$bg_path/$newname" ] || { [ -e "$bg_path/$newname" ] && [ ! -f "$bg_path/$newname" ]; }; then result 255 exit 0 fi case "$(printf '%s' "$newname" | tr 'A-Z' 'a-z')" in *.jpg|*.jpeg|*.png|*.gif|*.webp|*.mp4|*.webm) ;; *) result 255; exit 0 ;; esac if [ -f "$tmp_path" ] && [ ! -L "$tmp_path" ] && mkdir -p "$bg_path" && chmod 0644 "$tmp_path" && mv -f "$tmp_path" "$bg_path/$newname" 2>/dev/null; then result 0 else result 1 fi ;; esac ;; esac