diff --git a/scripts/pixeldrain.sh b/scripts/pixeldrain.sh index a9ad2ad..6bfb9e7 100644 --- a/scripts/pixeldrain.sh +++ b/scripts/pixeldrain.sh @@ -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 ${NC}" + echo "" + echo -e "${CYAN}You can upload multiple files by specifying their names separated by spaces:${NC}" + echo -e " ${GREEN}pixeldrain ${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 ${NC}" + echo "" + echo -e "${CYAN}You can upload multiple files by specifying their names separated by spaces:${NC}" + echo -e " ${GREEN}bash pixeldrain.sh ${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" @@ -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 } @@ -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 ... " - 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 "$@"