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