Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(backup): use ./data/media when a leftover ./uploads was never made

Yudhi Armyndhariseef371ba8a6f
diff --git a/scripts/lib-env.sh b/scripts/lib-env.sh
index e1e2b8cb2..b36664d07 100644
--- a/scripts/lib-env.sh
+++ b/scripts/lib-env.sh
@@ -66,14 +66,17 @@ openwa_writable() {
# openwa_media_dir - STORAGE_LOCAL_PATH as the app settles it (src/config/storage-root.ts). v0.2.0 to
# v0.7.3 persisted ./uploads into .env.generated; where that cannot be created, as under the image's
# root-owned /app, the app keeps media in ./data/media instead, so the scripts have to look there too.
+# Writability alone cannot tell: `docker exec` runs these as root, which can create /app/uploads while
+# the app's own user cannot. The app creates a ./uploads it uses at boot, so a missing one beside an
+# existing ./data/media means ./data/media is in use.
openwa_media_dir() {
local dir
dir="$(openwa_resolve STORAGE_LOCAL_PATH "$DATA_DIR/media")"
case "$dir" in
./uploads | uploads)
- if ! openwa_writable "$dir"; then
- echo "[config] WARN: STORAGE_LOCAL_PATH=$dir cannot be created here, so the app keeps media in" >&2
- echo "[config] ./data/media; using that. Remove the leftover line from .env.generated." >&2
+ if ! openwa_writable "$dir" || { [ ! -d "$dir" ] && [ -d ./data/media ]; }; then
+ echo "[config] WARN: STORAGE_LOCAL_PATH=$dir is a leftover the app does not use here, so it keeps" >&2
+ echo "[config] media in ./data/media; using that. Remove the line from .env.generated." >&2
dir=./data/media
fi
;;
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index f7ee33c3a..2b8a0b464 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -37,6 +37,8 @@
# (x) a relocated BOOTSTRAP_KEY_FILE is archived and restored there, and an unwritable one is refused
# before any database is written (that half skipped as root)
# (y) plugin packages in the legacy ./plugins, which the archive does not carry, are reported
+# (z) a leftover ./uploads that was never created, beside an existing ./data/media, resolves there in
+# both scripts whatever the uid
#
# Usage: ./scripts/smoke-test-backup-restore.sh
# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).
@@ -1132,5 +1134,28 @@ if printf '%s' "$OUT_Y" | grep -q 'WARN: ./plugins'; then
fi
pass "(y) packages in the legacy ./plugins are reported when PLUGINS_DIR is unset"
+echo ""
+echo "==> (z) a leftover ./uploads that was never created follows the app to an existing ./data/media"
+# docker exec runs the scripts as root, which can create /app/uploads, while the app runs as openwa,
+# which cannot and keeps media in ./data/media. Deciding by writability alone archived no media there
+# and restored it into the container layer. The app creates a ./uploads it uses at boot, so a missing
+# ./uploads beside an existing ./data/media means ./data/media is the one in use, whatever the uid.
+Z="$WORK/z"
+mkdir -p "$Z/app/data/media" "$Z/dst/data/media"
+make_fixture "$Z/app/data/main.sqlite" "zulu-main"
+make_fixture "$Z/app/data/openwa.sqlite" "zulu-data"
+printf 'zulu-media\n' >"$Z/app/data/media/a.jpg"
+printf 'STORAGE_LOCAL_PATH=./uploads\n' >"$Z/app/data/.env.generated"
+OUT_Z="$(cd "$Z/app" && BACKUP_DIR="$Z/out" "$BACKUP" 2>&1)"
+ARCHIVE_Z="$(ls "$Z"/out/openwa-backup-*.tar.gz)"
+if ! tar -tzf "$ARCHIVE_Z" | grep -qx './media/a.jpg'; then
+ fail "(z) backup left out the media in ./data/media: $OUT_Z"
+fi
+OUT_Z="$(cd "$Z/dst" && "$RESTORE" "$ARCHIVE_Z" 2>&1)"
+if [ "$(cat "$Z/dst/data/media/a.jpg" 2>/dev/null || true)" != "zulu-media" ] || [ -e "$Z/dst/uploads" ]; then
+ fail "(z) restore did not put the media back under ./data/media: $OUT_Z"
+fi
+pass "(z) a never-created ./uploads beside ./data/media resolves to ./data/media without a uid check"
+
echo ""
echo "All smoke tests passed!"