Created
August 18, 2025 12:33
-
-
Save LYK-love/0c64936cde110a50b00719c1dea93a72 to your computer and use it in GitHub Desktop.
Upload file(s) to Tsinghua Cloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| echo "---------------------------------------------------------" | |
| echo " Seafile Upload Script (Tsinghua Cloud compatible)" | |
| echo | |
| echo " Reference: https://cloud.seafile.com/published/web-api/v2.1/file-upload.md" | |
| echo | |
| echo " Steps:" | |
| echo " 1) Paste an Upload Link (must be in form https://cloud.tsinghua.edu.cn/u/d/<token>/)" | |
| echo " 2) Choose a parent_dir (default is /, often your username like /louis)" | |
| echo " 3) Enter local file paths one by one, empty line to finish" | |
| echo "---------------------------------------------------------" | |
| echo | |
| # Ask for upload link | |
| echo "Paste your Seafile upload link:" | |
| read -r LINK | |
| # Extract token | |
| TOKEN=$(echo "$LINK" | sed -E 's#.*/u/d/([A-Za-z0-9]+)/?.*#\1#;t;d') | |
| if [ -z "$TOKEN" ]; then | |
| echo "❌ Could not extract token. Make sure the link looks like /u/d/<token>/" | |
| exit 1 | |
| fi | |
| # Fetch upload endpoint | |
| UPLOAD_URL=$(curl -fsS "https://cloud.tsinghua.edu.cn/api/v2.1/upload-links/$TOKEN/upload/" \ | |
| | python3 -c 'import sys, json; print(json.load(sys.stdin).get("upload_link",""))' 2>/dev/null || true) | |
| if [ -z "$UPLOAD_URL" ]; then | |
| UPLOAD_URL=$(curl -fsS "https://cloud.tsinghua.edu.cn/api2/upload-link/?t=$TOKEN" | tr -d '"') | |
| fi | |
| if [ -z "$UPLOAD_URL" ]; then | |
| echo "❌ Failed to get upload endpoint. Maybe this is not a valid upload link." | |
| exit 1 | |
| fi | |
| echo "✅ Upload endpoint: $UPLOAD_URL" | |
| echo | |
| # Parent dir | |
| read -r -p "Enter target parent_dir (default is /): " PARENT_DIR | |
| PARENT_DIR=${PARENT_DIR:-/} | |
| echo "Using parent_dir: $PARENT_DIR" | |
| echo | |
| # Collect files | |
| echo "Enter full file paths one per line. Press Enter on empty line to start uploading:" | |
| FILES=() | |
| while true; do | |
| read -r FILEPATH || true | |
| [ -z "$FILEPATH" ] && break | |
| if [ -f "$FILEPATH" ]; then | |
| FILES+=("$FILEPATH") | |
| else | |
| echo "⚠️ File not found: $FILEPATH" | |
| fi | |
| done | |
| if [ ${#FILES[@]} -eq 0 ]; then | |
| echo "No files to upload. Exit." | |
| exit 0 | |
| fi | |
| # Upload loop | |
| for f in "${FILES[@]}"; do | |
| echo "→ Uploading $f to $PARENT_DIR ..." | |
| curl -fsS \ | |
| -F "parent_dir=${PARENT_DIR}" \ | |
| -F "file=@${f}" \ | |
| "$UPLOAD_URL?ret-json=1" \ | |
| || echo "❌ Failed to upload $f" | |
| done | |
| echo "All done ✅" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment