New import methodology, done from static directory mapped in FireFly

This commit is contained in:
2026-03-06 20:49:37 -05:00
parent 0c39445d71
commit 7e17ec1fea
3 changed files with 75 additions and 86 deletions

112
import.sh
View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash
# Firefly III Data Importer - batch auto-import
# Pairs .json + .csv files by base name and POSTs each to FIDI /autoimport
# Firefly III Data Importer - trigger FIDI directory auto-import
# POSTs to FIDI /autoimport with the container-side import directory path.
# FIDI scans that directory for JSON+CSV pairs and imports them all.
#
# Usage: ./import.sh [--dry-run|-n]
@@ -30,7 +31,8 @@ fi
FIDI_URL="${FIDI_URL:?Set FIDI_URL in .env or environment (e.g. http://localhost:8080)}"
FIDI_SECRET="${FIDI_SECRET:-}"
FIDI_ACCESS_TOKEN="${FIDI_ACCESS_TOKEN:-}"
IMPORT_DIR="${IMPORT_DIR:-$SCRIPT_DIR/imports}"
# Path to the imports directory as seen by FIDI inside its container
FIDI_IMPORT_DIR="${FIDI_IMPORT_DIR:-/imports}"
# ---------------------------------------------------------------------------
# Helpers
@@ -48,95 +50,43 @@ dry() { echo -e "${CYAN}[DRY]${NC} $*"; }
build_url() {
local url="${FIDI_URL%/}/autoimport"
[[ -n "$FIDI_SECRET" ]] && url="${url}?secret=${FIDI_SECRET}"
echo "$url"
}
import_pair() {
local json_file="$1"
local csv_file="$2"
local base
base="$(basename "$json_file" .json)"
local url
url="$(build_url)"
if $DRY_RUN; then
dry "$base"
dry " POST $url"
dry " csv → $csv_file"
dry " json → $json_file"
[[ -n "$FIDI_ACCESS_TOKEN" ]] && dry " auth → Bearer ***"
return 0
fi
info "Importing: $base"
# Build auth args as an array to safely handle spaces/special chars
local curl_args=(-s -w "\n%{http_code}")
[[ -n "$FIDI_ACCESS_TOKEN" ]] && curl_args+=(-H "Authorization: Bearer $FIDI_ACCESS_TOKEN")
curl_args+=(
-F "csv=@${csv_file};type=text/csv"
-F "json=@${json_file};type=application/json"
"$url"
)
local response http_code body
response=$(curl "${curl_args[@]}")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n -1)
if [[ "$http_code" =~ ^2 ]]; then
pass "$base (HTTP $http_code)"
return 0
else
fail "$base (HTTP $http_code)"
echo " Response: $body"
return 1
fi
local qs="directory=${FIDI_IMPORT_DIR}"
[[ -n "$FIDI_SECRET" ]] && qs="${qs}&secret=${FIDI_SECRET}"
echo "${url}?${qs}"
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
if [[ ! -d "$IMPORT_DIR" ]]; then
echo "Import directory not found: $IMPORT_DIR"
exit 1
fi
url="$(build_url)"
$DRY_RUN && info "Dry-run mode — no requests will be sent"
mapfile -t json_files < <(find "$IMPORT_DIR" -maxdepth 1 -name '*.json' | sort)
if [[ ${#json_files[@]} -eq 0 ]]; then
info "No .json files found in $IMPORT_DIR"
if $DRY_RUN; then
info "Dry-run mode — no requests will be sent"
dry "POST ${url//secret=*/secret=***}"
[[ -n "$FIDI_ACCESS_TOKEN" ]] && dry "auth → Bearer ***"
exit 0
fi
success=0
skipped=0
failed=0
info "Triggering FIDI auto-import from ${FIDI_IMPORT_DIR}..."
for json_file in "${json_files[@]}"; do
base="$(basename "$json_file" .json)"
csv_file="${IMPORT_DIR}/${base}.csv"
curl_args=(-s -w "\n%{http_code}" -X POST)
[[ -n "$FIDI_ACCESS_TOKEN" ]] && curl_args+=(-H "Authorization: Bearer $FIDI_ACCESS_TOKEN")
curl_args+=("$url")
if [[ ! -f "$csv_file" ]]; then
info "Skipping $base — no matching .csv found"
((skipped++))
continue
fi
if import_pair "$json_file" "$csv_file"; then
((success++))
else
((failed++))
fi
done
response=$(curl "${curl_args[@]}")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n -1)
echo ""
echo "----------------------------------------"
$DRY_RUN && echo " (dry run — nothing was imported)"
echo " Done: ${success} succeeded, ${failed} failed, ${skipped} skipped"
echo "----------------------------------------"
[[ "$failed" -eq 0 ]]
if [[ "$http_code" =~ ^2 ]]; then
pass "Import triggered (HTTP $http_code)"
echo " Response: $body"
echo "----------------------------------------"
exit 0
else
fail "Import failed (HTTP $http_code)"
echo " Response: $body"
echo "----------------------------------------"
exit 1
fi