Skip to content

Commit

Permalink
sh: Pixeldrain: improve upload process [SQUASHED]
Browse files Browse the repository at this point in the history
For Linux Env:
To make the script globally available, run the following commands in your terminal:

sudo wget https://raw.githubusercontent.com/tanvirr007/scripts/main/scripts/pixeldrain.sh -O "/usr/local/bin/pixeldrain"
   sudo chmod +x /usr/local/bin/pixeldrain

Make sure yoy have wget and bc installed on your linux system

After that, you can use it like this:
   pixeldrain your_file_name.zip
   Example: pixeldrain SomethingOS-14-spes-20240516.zip

If you want to uninstall pixeldrain you can run:
    sudo rm "/usr/local/bin/pixeldrain"

Be careful for typos! You don't want to accidentally remove your /usr like this: MrMEEE/bumblebee-Old-and-abbandoned#123 ;)

Usage Instructions for Termux:

1. Download this script to a specific directory using curl:
   curl -O https://raw.githubusercontent.com/tanvirr007/scripts/main/scripts/pixeldrain.sh

   Or, you can use wget:
   wget https://raw.githubusercontent.com/tanvirr007/scripts/main/scripts/pixeldrain.sh

After downloading, when you run the script for the first time, you’ll be prompted to enter your PixelDrain API key.

2. Run the script with your file(s):
   bash pixeldrain.sh <your_filename>

You can upload multiple files by specifying their names separated by spaces:
   bash pixeldrain.sh <file1> <file2> <file3>

If you have long filenames like 'SomethingOS-14-spes-20240516.zip', you can use wildcards:
   bash pixeldrain.sh So*.zip
   bash pixeldrain.sh Something*.zip

Android App:
- [Download Pixeldrain]: https://raw.githubusercontent.com/tanvirr007/scripts/main/assets/Pixeldrain.v1.0.0.apk

Changelog:
- API Key prompt and secure storage
- Automatic jq Installation if missing
- Show files with basic metadata
- Sequential Uploads with error checks
- Upload Summary with success count
  • Loading branch information
tanvirr007 committed Nov 10, 2024
1 parent 85e68a5 commit b4a40ad
Showing 1 changed file with 116 additions and 14 deletions.
130 changes: 116 additions & 14 deletions scripts/pixeldrain.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,72 @@
#!/bin/bash

PDSERVER="https://pixeldrain.com"
API_KEY_FILE="$HOME/.pixeldrain_api_key"

RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
CYAN='\033[1;36m'
NC='\033[0m'

display_usage() {
echo -e "${YELLOW}Usage Instructions for Linux Environment:${NC}"
echo ""
echo -e "${CYAN}To make the script globally available, run the following commands in your terminal:${NC}"
echo -e " ${GREEN}sudo wget https://raw.githubusercontent.com/tanvirr007/scripts/main/scripts/pixeldrain.sh -O \"/usr/local/bin/pixeldrain\"${NC}"
echo -e " ${GREEN}sudo chmod +x /usr/local/bin/pixeldrain${NC}"
echo ""
echo -e "${CYAN}After installation, when you run the script for the first time, you’ll be prompted to enter your PixelDrain API key.${NC}"
echo ""
echo -e "${CYAN}To change your API key later, edit the file at:${NC}"
echo -e " ${GREEN}$API_KEY_FILE${NC}"
echo ""
echo -e "${CYAN}You can then use the script like this:${NC}"
echo -e " ${GREEN}pixeldrain <your_filename>${NC}"
echo ""
echo -e "${CYAN}You can upload multiple files by specifying their names separated by spaces:${NC}"
echo -e " ${GREEN}pixeldrain <file1> <file2> <file3>${NC}"
echo ""
echo -e "${CYAN}If you want to uninstall pixeldrain, you can run:${NC}"
echo -e " ${GREEN}sudo rm \"/usr/local/bin/pixeldrain\"${NC}"
echo ""
echo -e "${YELLOW}Be careful with typos! You don't want to accidentally remove your /usr, like this: https://github.com/MrMEEE/bumblebee-Old-and-abbandoned/issues/123 ;)${NC}"
echo ""
echo -e "${YELLOW}Usage Instructions for Termux:${NC}"
echo ""
echo -e "${CYAN}1.${NC} Download this script to a specific directory using curl:"
echo -e " ${GREEN}curl -O https://raw.githubusercontent.com/tanvirr007/scripts/main/scripts/pixeldrain.sh${NC}"
echo ""
echo -e "${CYAN} Or, you can use wget:${NC}"
echo -e " ${GREEN}wget https://raw.githubusercontent.com/tanvirr007/scripts/main/scripts/pixeldrain.sh${NC}"
echo ""
echo -e "${CYAN}After downloading, when you run the script for the first time, you’ll be prompted to enter your PixelDrain API key.${NC}"
echo ""
echo -e "${CYAN}2.${NC} Run the script with your file(s):"
echo -e " ${GREEN}bash pixeldrain.sh <your_filename>${NC}"
echo ""
echo -e "${CYAN}You can upload multiple files by specifying their names separated by spaces:${NC}"
echo -e " ${GREEN}bash pixeldrain.sh <file1> <file2> <file3>${NC}"
echo ""
echo -e "${YELLOW}Note:${NC} If you have long filenames like 'SomethingOS-14-spes-20240516.zip', you can use wildcards. When using wildcards, make sure to match the full filenames you want like:"
echo -e " ${GREEN}pixeldrain So*.zip${NC}"
echo -e " ${GREEN}pixeldrain Something*.zip${NC}"
echo ""
echo -e "${CYAN}To change or revoke your API key, edit the file in your home directory by running:${NC}"
echo -e " ${GREEN}nano .pixeldrain_api_key${NC}"
echo ""
}

if [ $# -eq 0 ]; then
echo -e "${RED}Error: No files specified.${NC}"
display_usage
exit 1
fi

prompt_api_key() {
read -p "Enter your Pixeldrain API key: " API_KEY
if [ -z "$API_KEY" ]; then
echo "Error: API key cannot be empty."
echo -e "${RED}Error: API key cannot be empty.${NC}"
exit 1
fi
echo "Saving API key to $API_KEY_FILE"
Expand All @@ -14,13 +75,13 @@ prompt_api_key() {
}

install_jq() {
echo "jq is required for JSON parsing."
echo -e "${CYAN}jq is required for JSON parsing.${NC}"
read -p "Do you want to install jq now? (y/n): " install_jq
if [ "$install_jq" = "y" ] || [ "$install_jq" = "Y" ]; then
sudo apt-get update
sudo apt-get install jq
else
echo "Exiting script as jq is required."
echo -e "${RED}Exiting script as jq is required.${NC}"
exit 1
fi
}
Expand All @@ -42,37 +103,78 @@ else
source "$API_KEY_FILE"
fi

human_readable_size() {
local size=$1
if [ "$size" -lt 1024 ]; then
echo "${size} bytes"
elif [ "$size" -lt 1048576 ]; then
echo "$(echo "scale=2; $size/1024" | bc) KB"
elif [ "$size" -lt 1073741824 ]; then
echo "$(echo "scale=2; $size/1048576" | bc) MB"
else
echo "$(echo "scale=2; $size/1073741824" | bc) GB"
fi
}

upload_files() {
local files=("$@")
success_count=0
error_occurred=false

if [ ${#files[@]} -gt 1 ]; then
counter=1
total_files=${#files[@]}
fi

for file in "${files[@]}"
do
if [ ! -f "$file" ]; then
echo "Error: File $file not found!"
continue
echo -e "${RED}Error: File $file not found!${NC}"
error_occurred=true
break
fi

filename=$(basename "$file")
filesize=$(stat -c %s "$file")
human_size=$(human_readable_size $filesize)

echo "Uploading $filename ..."
response=$(curl -# -T "$file" -u ":$PIXELDRAIN_API_KEY" "$PDSERVER/api/file/")
extension="${filename##*.}"

md5sum=$(md5sum "$file" | awk '{ print $1 }')

if [ ${#files[@]} -gt 1 ]; then
echo -e "${CYAN}$counter:${NC} Uploading Your file ${YELLOW}$filename${NC}"
counter=$((counter + 1))
else
echo -e "Uploading Your file ${YELLOW}$filename${NC}"
fi

response=$(curl -# -T "$file" -u ":$PIXELDRAIN_API_KEY" "$PDSERVER/api/file/")

if echo "$response" | grep -q '"success":false'; then
echo "Error: Failed to upload $filename"
echo -e "${RED}Error: Failed to upload $filename${NC}"
echo "Response: $response"
error_occurred=true
break
else
fileid=$(echo "$response" | jq -r '.id')
echo "Your file URL: $PDSERVER/u/$fileid"
echo -e "${CYAN}Name:${NC} ${YELLOW}$filename${NC}"
echo -e "${CYAN}File size:${NC} ${YELLOW}$human_size${NC}"
echo -e "${CYAN}File type:${NC} ${YELLOW}$extension${NC}"
echo -e "${CYAN}Md5sum:${NC} ${YELLOW}$md5sum${NC}"
echo -e "${CYAN}File URL:${NC} ${GREEN}$PDSERVER/u/$fileid${NC}"
success_count=$((success_count + 1))
fi

echo ""
done
}

if [ $# -eq 0 ]; then
echo "Usage: pdup <file1> <file2> ... <fileN>"
exit 1
fi
if $error_occurred; then
display_usage
exit 1
fi

echo -e "${YELLOW}Upload Status:${NC} ${GREEN}$success_count of ${#files[@]} files uploaded successfully.${NC}"
}

upload_files "$@"

0 comments on commit b4a40ad

Please sign in to comment.