Last active
March 17, 2023 20:55
-
-
Save DarremMolko/ecc7aa7a1b1f172e5d8fb0be55c8e1a0 to your computer and use it in GitHub Desktop.
Upload a file using Alist V3 and prints its download link for easy sharing.
This file contains 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
#!/bin/bash | |
# Set default UPLOAD_PATH value | |
UPLOAD_PATH="/path/to/upload/to" | |
# Use getopts to check for the -d switch and set the UPLOAD_PATH value accordingly | |
while getopts "d:" opt; do | |
case $opt in | |
d) | |
UPLOAD_PATH=$OPTARG | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
echo "Usage: $0 [-d /path/to/upload/to] /path/to/local/file" | |
exit 1 | |
;; | |
esac | |
done | |
# Shift the getopts arguments so $1 contains the file path argument | |
shift $((OPTIND-1)) | |
if [ $# -ne 1 ]; then | |
echo "Usage: $0 [-d /path/to/upload/to] /path/to/local/file" | |
exit 1 | |
fi | |
FILE_PATH="$1" | |
if [ ! -f "$FILE_PATH" ]; then | |
echo "Error: file '$FILE_PATH' not found." | |
exit 1 | |
fi | |
# Define API endpoint | |
BASE_URL="https://example.com/api/fs" | |
# Define your access token | |
TOKEN="alist-XXXXXXX" | |
# Extract the filename from the local file path | |
FILE_NAME="$(basename "${FILE_PATH}")" | |
# Perform the file upload | |
curl -s -X PUT "${BASE_URL}/form" \ | |
-H "Authorization: ${TOKEN}" \ | |
-H "File-Path: ${UPLOAD_PATH}/${FILE_NAME}" \ | |
-F "file=@${FILE_PATH}" > /dev/null | |
# Refresh the file list | |
curl -s -X POST "${BASE_URL}/list" \ | |
-H "Authorization: ${TOKEN}" \ | |
-H "Content-Type: application/json" \ | |
-d "{\"path\":\"${UPLOAD_PATH}\", \"refresh\": true, \"password\":\"\"}" \ | |
-o /dev/null | |
# Keep retrying until a non-null download link is obtained | |
MAX_RETRIES=5 | |
RETRY_COUNT=0 | |
DOWNLOAD_LINK=$(curl -s -X POST "${BASE_URL}/get" \ | |
-H "Authorization: ${TOKEN}" \ | |
-H "Content-Type: application/json" \ | |
-d "{\"path\":\"${UPLOAD_PATH}/${FILE_NAME}\", \"password\":\"\"}" \ | |
| jq -r '.data.raw_url') | |
while [ "$DOWNLOAD_LINK" = "null" ] && [ "$RETRY_COUNT" -lt "$MAX_RETRIES" ]; do | |
# Refresh the file list | |
curl -s -X POST "${BASE_URL}/list" \ | |
-H "Authorization: ${TOKEN}" \ | |
-H "Content-Type: application/json" \ | |
-d "{\"path\":\"${UPLOAD_PATH}\", \"refresh\": true, \"password\":\"\"}" \ | |
-o /dev/null | |
# Increment retry count | |
RETRY_COUNT=$((RETRY_COUNT + 1)) | |
# Retrieve the download link for the file | |
DOWNLOAD_LINK=$(curl -s -X POST "${BASE_URL}/get" \ | |
-H "Authorization: ${TOKEN}" \ | |
-H "Content-Type: application/json" \ | |
-d "{\"path\":\"${UPLOAD_PATH}/${FILE_NAME}\", \"password\":\"\"}" \ | |
| jq -r '.data.raw_url') | |
done | |
# Print the download link | |
if [ "$DOWNLOAD_LINK" = "null" ]; then | |
echo "Failed to obtain download link after $MAX_RETRIES retries." | |
else | |
echo "File uploaded successfully!" | |
echo "Download link: ${DOWNLOAD_LINK}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment