-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebdav_upload.sh
65 lines (51 loc) · 1.71 KB
/
webdav_upload.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
# Variables (modify these)
WEB_DAV_URL=""
WEB_DAV_USERNAME=""
WEB_DAV_PASSWORD=""
# Function to sanitize file names
sanitize() {
echo "$1" | tr -cd '[:alnum:]._-'
}
# Prompt the user to enter the download URL
read -p "Please enter the download URL: " DOWNLOAD_URL
# Check if the download URL is provided
if [ -z "$DOWNLOAD_URL" ]; then
echo "No download URL provided. Exiting."
exit 1
fi
# Extract file name from URL and sanitize it
FILE_NAME=$(basename "$DOWNLOAD_URL")
FILE_NAME_WITHOUT_QUERY="${FILE_NAME%%\?*}"
SANITIZED_FILE_NAME=$(sanitize "$FILE_NAME_WITHOUT_QUERY")
# Download the file
wget -O "$SANITIZED_FILE_NAME" "$DOWNLOAD_URL"
# Check if download was successful
if [ $? -ne 0 ]; then
echo "Failed to download the file."
exit 1
fi
# Split the file into 500MB parts
7z a -v500m "${SANITIZED_FILE_NAME}.7z" "$SANITIZED_FILE_NAME"
# Create a folder on the WebDAV server
FOLDER_NAME="${SANITIZED_FILE_NAME%.*}"
ENCODED_FOLDER_NAME=$(echo -n "$FOLDER_NAME" | jq -sRr @uri)
curl -u "$WEB_DAV_USERNAME:$WEB_DAV_PASSWORD" -X MKCOL "$WEB_DAV_URL/$ENCODED_FOLDER_NAME/"
# Check if folder creation was successful
if [ $? -ne 0 ]; then
echo "Failed to create folder on WebDAV server."
exit 1
fi
# Upload the split parts to the WebDAV server
for PART in "${SANITIZED_FILE_NAME}.7z".*; do
ENCODED_PART_NAME=$(echo -n "$PART" | jq -sRr @uri)
curl -u "$WEB_DAV_USERNAME:$WEB_DAV_PASSWORD" -T "$PART" "$WEB_DAV_URL/$ENCODED_FOLDER_NAME/$ENCODED_PART_NAME"
# Check if upload was successful
if [ $? -ne 0 ]; then
echo "Failed to upload $PART to WebDAV server."
exit 1
fi
done
# Clean up
rm "$SANITIZED_FILE_NAME" "${SANITIZED_FILE_NAME}.7z".*
echo "File downloaded, split, and uploaded successfully."