Houskeeping

This commit is contained in:
2025-09-08 11:02:19 +02:00
parent e390363b75
commit 9b86b0886e

View File

@@ -255,28 +255,22 @@ print(f\"Indexed {n} document(s).\")"
if [ -z "$file" ] || [ ! -f "$file" ]; then echo "Usage: owui-upload </abs/path/file>" >&2; exit 1; fi if [ -z "$file" ] || [ ! -f "$file" ]; then echo "Usage: owui-upload </abs/path/file>" >&2; exit 1; fi
_require "OPENWEBUI_URL" "$OPENWEBUI_URL" _require "OPENWEBUI_URL" "$OPENWEBUI_URL"
_require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY"
tmp="$(_mktemp)"
code="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)"
# write response body to tmp_body; write HTTP code to tmp_code; capture exit code from $?
curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \
-F "file=@$file" \ -F "file=@$file" \
-w "%{http_code}" --output "$tmp" \ -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/files/" >"$tmp_code"
"$(_owui_url)/api/v1/files/"; echo $?)" curl_exit=$?
# If curl failed to write (e.g., (23)), fall back to capturing to a variable http_code="$(cat "$tmp_code" 2>/dev/null || echo 0)"
curl_exit="${code##*$'\n'}"; http_code="$(echo "$code" | head -n1)"
if [ "$curl_exit" != "0" ]; then cat "$tmp_body" | ppjson
RESP="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -F "file=@$file" "$(_owui_url)/api/v1/files/")" rm -f "$tmp_body" "$tmp_code"
echo "$RESP" | ppjson
echo "Upload: curl exited with code $curl_exit" >&2 if [ $curl_exit -ne 0 ]; then
# Try to read an HTTP code if present echo "Upload failed: curl exit $curl_exit" >&2
http_code="$(python3 -c 'import sys,json; import sys exit $curl_exit
try:
d=json.load(sys.stdin)
print(200)
except Exception:
print(0)' <<<"$RESP")"
else
cat "$tmp" | ppjson
fi fi
rm -f "$tmp"
if [ "$http_code" != "200" ]; then if [ "$http_code" != "200" ]; then
echo "Upload failed (HTTP $http_code)" >&2 echo "Upload failed (HTTP $http_code)" >&2
exit 1 exit 1
@@ -291,47 +285,45 @@ except Exception:
fi fi
_require "OPENWEBUI_URL" "$OPENWEBUI_URL" _require "OPENWEBUI_URL" "$OPENWEBUI_URL"
_require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY" _require "OPENWEBUI_API_KEY" "$OPENWEBUI_API_KEY"
tmp_up="$(_mktemp)"
code_up_raw="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ # 1) Upload
tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)"
curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \
-F "file=@$file" \ -F "file=@$file" \
-w "%{http_code}" --output "$tmp_up" \ -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/files/" >"$tmp_code"
"$(_owui_url)/api/v1/files/"; echo $?)" curl_exit=$?; http_code="$(cat "$tmp_code" 2>/dev/null || echo 0)"
curl_up_exit="${code_up_raw##*$'\n'}"; code_up="$(echo "$code_up_raw" | head -n1)" FILE_JSON="$(cat "$tmp_body")"
if [ "$curl_up_exit" != "0" ]; then rm -f "$tmp_body" "$tmp_code"
FILE_JSON="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -F "file=@$file" "$(_owui_url)/api/v1/files/")"
echo "Upload: curl exited with code $curl_up_exit" >&2 echo "$FILE_JSON" | ppjson
else if [ $curl_exit -ne 0 ]; then
FILE_JSON="$(cat "$tmp_up")" echo "Upload failed: curl exit $curl_exit" >&2; exit $curl_exit
fi fi
rm -f "$tmp_up" if [ "$http_code" != "200" ]; then
if [ "$code_up" != "200" ]; then echo "Upload failed (HTTP $http_code)" >&2; exit 1
echo "Upload failed (HTTP $code_up)" >&2
echo "$FILE_JSON" | ppjson
exit 1
fi fi
FILE_ID="$(python3 -c 'import sys,json; d=json.loads(sys.stdin.read()); print(d.get("id") or (d.get("data") or {}).get("id",""))' <<<"$FILE_JSON")"
if [ -z "$FILE_ID" ]; then echo "Upload failed (no file id)"; echo "$FILE_JSON" | ppjson; exit 1; fi FILE_ID="$(python3 -c 'import sys,json; d=json.load(sys.stdin); print(d.get("id") or (d.get("data") or {}).get("id",""))' <<<"$FILE_JSON")"
if [ -z "$FILE_ID" ]; then echo "Upload failed (no file id)"; exit 1; fi
# 2) Attach
KB_ID="$(_kb_id_by_name "$kb_name")" KB_ID="$(_kb_id_by_name "$kb_name")"
if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found"; exit 1; fi if [ -z "$KB_ID" ]; then echo "KB '$kb_name' not found"; exit 1; fi
tmp_att="$(_mktemp)"
code_att_raw="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \ tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)"
curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \
-d "{\"file_id\":\"$FILE_ID\"}" \ -d "{\"file_id\":\"$FILE_ID\"}" \
-w "%{http_code}" --output "$tmp_att" \ -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/knowledge/$KB_ID/file/add" >"$tmp_code"
"$(_owui_url)/api/v1/knowledge/$KB_ID/file/add"; echo $?)" curl_exit=$?; http_code="$(cat "$tmp_code" 2>/dev/null || echo 0)"
curl_att_exit="${code_att_raw##*$'\n'}"; code_att="$(echo "$code_att_raw" | head -n1)" RESP="$(cat "$tmp_body")"
if [ "$curl_att_exit" != "0" ]; then rm -f "$tmp_body" "$tmp_code"
RESP="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \
-d "{\"file_id\":\"$FILE_ID\"}" \
"$(_owui_url)/api/v1/knowledge/$KB_ID/file/add")"
echo "Attach: curl exited with code $curl_att_exit" >&2
else
RESP="$(cat "$tmp_att")"
fi
rm -f "$tmp_att"
echo "$RESP" | ppjson echo "$RESP" | ppjson
if [ "$code_att" != "200" ]; then if [ $curl_exit -ne 0 ]; then
echo "Attach failed (HTTP $code_att)" >&2 echo "Attach failed: curl exit $curl_exit" >&2; exit $curl_exit
exit 1 fi
if [ "$http_code" != "200" ]; then
echo "Attach failed (HTTP $http_code)" >&2; exit 1
fi fi
;; ;;
@@ -363,41 +355,31 @@ except Exception:
if [ ${#matched[@]} -eq 0 ]; then echo "No files match: $glob_pat"; exit 1; fi if [ ${#matched[@]} -eq 0 ]; then echo "No files match: $glob_pat"; exit 1; fi
for f in "${matched[@]}"; do for f in "${matched[@]}"; do
echo "[owui] uploading: $f" echo "[owui] uploading: $f"
tmp_up="$(_mktemp)" tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)"
code_up_raw="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \ curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" \
-F "file=@$f" \ -F "file=@$f" \
-w "%{http_code}" --output "$tmp_up" \ -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/files/" >"$tmp_code"
"$(_owui_url)/api/v1/files/"; echo $?)" curl_up_exit=$?; code_up="$(cat "$tmp_code" 2>/dev/null || echo 0)"
curl_up_exit="${code_up_raw##*$'\n'}"; code_up="$(echo "$code_up_raw" | head -n1)" FILE_JSON="$(cat "$tmp_body")"
if [ "$curl_up_exit" != "0" ]; then rm -f "$tmp_body" "$tmp_code"
FILE_JSON="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -F "file=@$f" "$(_owui_url)/api/v1/files/")"
echo " upload: curl exited with code $curl_up_exit" >&2 if [ $curl_up_exit -ne 0 ] || [ "$code_up" != "200" ]; then
else echo " upload failed (curl=$curl_up_exit http=$code_up), skipping"
FILE_JSON="$(cat "$tmp_up")"
fi
rm -f "$tmp_up"
if [ "$code_up" != "200" ]; then
echo " upload failed (HTTP $code_up), skipping"
echo "$FILE_JSON" | ppjson echo "$FILE_JSON" | ppjson
continue continue
fi fi
FILE_ID="$(python3 -c 'import sys,json; d=json.loads(sys.stdin.read()); print(d.get("id") or (d.get("data") or {}).get("id",""))' <<<"$FILE_JSON")" FILE_ID="$(python3 -c 'import sys,json; d=json.loads(sys.stdin.read()); print(d.get("id") or (d.get("data") or {}).get("id",""))' <<<"$FILE_JSON")"
if [ -z "$FILE_ID" ]; then echo " upload failed (no id), skipping"; echo "$FILE_JSON" | ppjson; continue; fi if [ -z "$FILE_ID" ]; then echo " upload failed (no id), skipping"; echo "$FILE_JSON" | ppjson; continue; fi
tmp_att="$(_mktemp)"
code_att_raw="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \ tmp_body="$(_mktemp)"; tmp_code="$(_mktemp)"
curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \
-d "{\"file_id\":\"$FILE_ID\"}" \ -d "{\"file_id\":\"$FILE_ID\"}" \
-w "%{http_code}" --output "$tmp_att" \ -w "%{http_code}" --output "$tmp_body" "$(_owui_url)/api/v1/knowledge/$KB_ID/file/add" >"$tmp_code"
"$(_owui_url)/api/v1/knowledge/$KB_ID/file/add"; echo $?)" curl_att_exit=$?; code_att="$(cat "$tmp_code" 2>/dev/null || echo 0)"
curl_att_exit="${code_att_raw##*$'\n'}"; code_att="$(echo "$code_att_raw" | head -n1)" RESP="$(cat "$tmp_body")"
if [ "$curl_att_exit" != "0" ]; then rm -f "$tmp_body" "$tmp_code"
RESP="$(curl -sS -H "Authorization: Bearer $OPENWEBUI_API_KEY" -H "Content-Type: application/json" \
-d "{\"file_id\":\"$FILE_ID\"}" \
"$(_owui_url)/api/v1/knowledge/$KB_ID/file/add")"
echo " attach: curl exited with code $curl_att_exit" >&2
else
RESP="$(cat "$tmp_att")"
fi
rm -f "$tmp_att"
echo "$RESP" | ppjson echo "$RESP" | ppjson
if [ "$code_att" != "200" ]; then if [ "$code_att" != "200" ]; then
echo " attach failed (HTTP $code_att)" echo " attach failed (HTTP $code_att)"