xrforge/manyfold/cli/manyfold.sh
Leon van Kammen 3b6afc90ec milestone 4h: remove x day old files over X MB (temporary sharingservice for big files)
added 'cleanup_big_files' function to manyfold.sh.

Demo:

```
$ docker run xrforge
...
````

create a 10MB dummy_file.txt (and rewind date some years):

```
$ docker exec xrforge 'dd if=/dev/zero of=/mnt/models/dummy_file.txt bs=1M count=10'
10+0 records in
10+0 records out
10485760 bytes (10.0MB) copied, 0.005670 seconds, 1.7GB/s

$ touch -d "2021-01-01" /mnt/models/dummy_file.txt
$
```

Now when setting maxmb's to `5` and maxdays to `5`, we can see in the logs
that it gets deleted:

```
[/bin/infinite cleanup_big_files /mnt/models 5 5] Fri Aug  1 14:19:08 UTC 2025 executing
deleting: /mnt/models/dummy_file.txt
```

This behaviour is default, and can be disabled by docker environment-flag `NO_DISABLEBIGFILES`
2025-08-01 16:20:48 +02:00

178 lines
5.6 KiB
Bash
Executable file

#!/bin/sh
oci=$(which podman || which docker)
test -n "$APPNAME" || APPNAME=xrforge
test -n "$UPLOAD_PATH" || UPLOAD_PATH=/mnt/models
db=/config/manyfold.sqlite3
# utility funcs
error(){ echocolor "[error]" "$*"; exit 1; }
echocolor(){ printf "\033[96m%s\033[0m \033[95m%s\033[0m %s\n" "$1" "$2" "$3"; }
debug(){ set -x; "$@"; set +x; }
create_config(){
test -d ./config || mkdir config
test -d ./experiences || mkdir experiences
}
run(){
test -x ${oci} || { echo "warning: not running manyfold OCI container [install podman/docker/etc]"; exit; }
echocolor "[$APPNAME]" "$(basename ${oci}) detected..starting OCI container"
${oci} rm -f xrforge
#-e NO_OVERLAYFS=true \
#-e NO_DEFAULTDB=true \
#-e PUBLIC_HOSTNAME=localhost \
#-e PUBLIC_PORT=80 \
debug ${oci} run "$@" -p 8790:3214 --name xrforge \
-e SECRET_KEY_BASE=lkjwljlkwejrlkjek34k234l \
-e DATABASE_ADAPTER=sqlite3 \
-e SUDO_RUN_UNSAFELY=enabled \
-e MULTIUSER=enabled \
-e FEDERATION=enabled \
-e THEME=vapor \
--cap-add SYS_ADMIN --security-opt apparmor:unconfined --device /dev/fuse \
xrforge
#ghcr.io/manyfold3d/manyfold-solo:latest
}
overlayfs(){
test -d /manyfold || return 0; # nothing to override
echocolor "[$APPNAME]" "applying filesystem overlay"
cd /manyfold
rsync -rvzi * /.
}
# cron-like function using sleep (./manifold.sh infinite 3600 zip -r /backup.zip /)
infinite(){
trap 'echocolor "/bin/infinite $*: process ended..infinite does not care.."; sleep 2s' INT
interval=$1
shift
loop(){
echocolor "[/bin/infinite $*]" "$(date) started"
while sleep ${interval}; do
echocolor "[/bin/infinite $*]" "$(date) executing"
"$@"
done;
}
loop "$@"
}
db(){
default(){
test -f /manyfold/manyfold.sql && ! test -f /config/manyfold.sql && {
echocolor "[$APPNAME]" "copying default database"
test -d /config || mkdir /config
cat /manyfold/manyfold.sql | sqlite3 $db
}
}
dump(){
sqlite3 source.db ".dump" > /manyfold/manyfold.sql
}
import(){
sqlite3 $db < "$1" #/manyfold/manyfold.sql
}
"$@"
}
add_lib_to_db(){
name=$(basename $1)
debug sqlite3 $db "INSERT INTO libraries SELECT NULL, '$1', DATE('NOW'), DATE('NOW'), '', '', '$name', NULL, '', 'filesystem', '', '', '', '', '', '$name', 1 WHERE NOT EXISTS (SELECT 1 FROM libraries WHERE path = '$1');"
}
set_upload_path(){
echocolor "[$APPNAME]" "configuring upload library"
test -d $UPLOAD_PATH || mkdir $UPLOAD_PATH
add_lib_to_db $UPLOAD_PATH
id=$(sqlite3 $db "select id from libraries where path = '$UPLOAD_PATH';")
debug sqlite3 $db "UPDATE settings set value = $id where var = 'default_library';"
}
rclone_mount(){
libraries(){
rclone listremotes | while read remote; do
dir="${remote/:/}"
test -d /mnt/$dir || mkdir /mnt/$dir
echocolor "[$APPNAME]" "rclone: mounting $remote to /mnt/$dir"
debug rclone mount --daemon $remote /mnt/$dir -vv
add_lib_to_db /mnt/$dir
done
}
library(){
echocolor "[$APPNAME]" "rclone: mounting $RCLONE_REMOTE to /mnt/$RCLONE_REMOTE"
test -d /mnt/$RCLONE_REMOTE || mkdir /mnt/$RCLONE_REMOTE
debug rclone mount --daemon ${RCLONE_REMOTE}: /mnt/$RCLONE_REMOTE -vv
add_lib_to_db /mnt/$RCLONE_REMOTE
}
test -n "$RCLONE_REMOTE" && library
test -n "$RCLONE_REMOTE" || libraries
}
set_theme(){
test -z "$THEME" && return 0 # nothing to do
echocolor "[$APPNAME]" "setting theme to '$THEME'"
debug sqlite3 /config/manyfold.sqlite3 "UPDATE settings SET value = '"$THEME"' WHERE var == 'theme';"
}
set_modelpath(){
echocolor "[$APPNAME]" "enforcing modelpath"
debug sqlite3 /config/manyfold.sqlite3 "UPDATE settings SET value = replace('--- \"{creator}/{modelName}/{modelId}\"\n','\n',char(10)) WHERE var == 'model_path_template';"
}
rename_app(){
echocolor "[$APPNAME]" "renaming manyfold to $APPNAME"
sed -i 's/title: Manyfold/title: '$APPNAME'/g' /usr/src/app/config/locales/*.yml
sed -i 's|powered_by_html:.*|powered_by_html: Powered by <a href="https://forgejo.isvery.ninja/coderofsalvation/xrforge">XR Forge</a>, <a href="https://manifold.app" target="_blank">Manyfold</a>, <a href="https://xrfragment.org">XR Fragments</a> and <a href="https://nixos.org" target="_blank">NIX</a>|g' /usr/src/app/config/locales/*.yml
}
cleanup_big_files(){
local DIR=$1
local MAX_DAYS=$2
local MAX_MB=$3
test -n "$MAX_DAYS" && test -n "$MAX_MB" && {
find "$DIR" -type f -mtime +"$MAX_DAYS" -size +$(( $MAX_MB * 1024 ))k | while read file; do
echo "deleting: $file"
rm "$file"
done
}
}
delete_big_files(){
#./manyfold infinite 86400 cleanup_big_files /mnt/models 5 5 & # 5 days for files over 5MB (5 in 2025, 6 in 2026 e.g. ?)
# check every day to delete files older than 5 days and >5mb (5mb in 2025, 6mb in 2026 e.g.)
test -z "$NO_DELETEBIGFILES" && {
intervalSec=86400 # = 1 day
maxMB=$(( $(date +%Y) - 2020 ))
$0 infinite 5 cleanup_big_files /mnt/models $intervalSec $maxMB &
}
}
# The new entrypoint of the docker
boot(){
echocolor "[$APPNAME]" "booting..."
test -z "$NO_OVERLAYFS" && overlayfs
test -z "$NO_DEFAULTDB" && db default
rename_app
set_theme
set_modelpath
rclone_mount
set_upload_path
delete_big_files
exec "$@" # exec prevents error 's6-overlay-suexec: fatal: can only run as pid 1'
}
is_inside_container(){
test -d /package/admin && return 0 || return 1
}
usage(){
echocolor "Usage:" manyfold.sh "<cmd>"
echocolor "Cmds:"
echocolor " " "run [-d] " "# runs a OCI container (needs podman/docker)"
exit 0
}
test -n "$1" && "$@"
test -n "$1" || usage