Files
split-dl/split-dl.sh

323 lines
7.8 KiB
Bash
Executable File

#!/bin/bash
FILE="$1"
TYPE="$2"
SPLITSIZE="10M"
INFO="info.cfg"
# Check if 2 params are given
if [ "$#" -lt "2" ]; then
echo "This script requires two arguments: File or Link AND server or client."
exit 1
fi
# # # #
# FUNCTIONS
# # # #
function checkwait {
read -p "Continue? Press Ctrl+C to cancel." END
}
function getmd5 {
md5sum $1 | awk -F" " '{ print $1 }'
}
function compmd5 {
HASH1=$(getmd5 $1)
HASH2=$2
if [ "$HASH1" == "$HASH2" ]; then
echo "true"
else
echo "false"
fi
}
function getdu {
du $1 | awk -F" " '{ print $1 }'
}
function compdu {
DU1=$(getdu $1)
DU2=$2
if [ "$DU1" == "$DU2" ]; then
echo "true"
else
echo "false"
fi
}
function compall {
FILE=$1
SIZE=$2
MD5=$3
if $(compdu $FILE $SIZE) && $(compmd5 $FILE $MD5); then
echo="true"
else
echo="false"
fi
}
function checkfolder {
FOLDER=$1
# Check if folder already exists. If not, create it
if [ -d $FOLDER ]; then
read -p "Destination folder \"$FOLDER\" already exists. Should it be emptied? [y/n]: " YN
if [ $YN == "y" ]; then
rm -rf $FOLDER
mkdir $FOLDER
else
if [ "$TYPE" == "server" ]; then
echo "In server-mode, this isn't recommended. Aborting."
exit 1
fi
fi
elif [ -e $FOLDER ] && [ ! -d $FOLDER ]; then
echo "Destination \"$FOLDER\" already exists but is not a folder. Please check."
exit 1
else
mkdir $FOLDER
fi
}
# # # #
# SERVER MODE
# # # #
if [ $TYPE == "server" ]; then
# Check if file has spaces
if [ $(echo "$FILE" | grep -q " "; echo $?) == "0" ]; then
read -p "Filename has spaces. Should it be renamed? [y/n]: " YN
if [ $YN == "y" ]; then
# replace spaces by underscores
TMPFILENAME=$(echo "$FILE" | sed "s/ /_/g")
mv "$FILE" "$TMPFILENAME"
FILE=$TMPFILENAME
else
echo "Aborting."
exit 1
fi
fi
# Check if command is executed in directory of file or not
if [ $(basename "$FILE") != "$FILE" ]; then
echo "Please execute command in the same directory than the file itself."
exit 1
fi
# Check if file exists
if [ ! -e "$FILE" ]; then
echo "File does not exist. Aborting."
exit 1
fi
# Check if it's a file or a directory
if [ ! -f "$FILE" ]; then
echo "File is not a file. This script doesn't work with directories."
exit 1
fi
FOLDER="dl-$FILE"
checkfolder $FOLDER
INFO="$FOLDER/$INFO"
echo "[INFO] Calculating size and MD5 sum..."
BIGNAME=$(basename $FILE)
BIGSIZE=$(getdu $FILE)
BIGMD5=$(getmd5 $FILE)
echo "BIGNAME=$BIGNAME" >> $INFO
echo "BIGSIZE=$BIGSIZE" >> $INFO
echo "BIGMD5=$BIGMD5" >> $INFO
echo >> $INFO
echo "[INFO] Splitting big file into smaller parts..."
split --verbose -a 4 -b $SPLITSIZE $FILE $FOLDER/dl-
# List all splitted files, measure size and md5sum
echo "[INFO] Creating info document with necessary specs..."
SMALLNO=$(ls $FOLDER | grep -v $INFO | wc -l)
echo "SMALLNO=$SMALLNO" >> $INFO
NO=0
ls $FOLDER | grep -v $INFO | while read -r line; do
SMALLNAME=$line
SMALLSIZE=$(getdu $FOLDER/$SMALLNAME)
SMALLMD5=$(getmd5 $FOLDER/$SMALLNAME)
echo "SMALLNAME[$NO]=$SMALLNAME" >> $INFO
echo "SMALLSIZE[$NO]=$SMALLSIZE" >> $INFO
echo "SMALLMD5[$NO]=$SMALLMD5" >> $INFO
let NO=NO+1
done
echo "[SUCCESS] You can now download the splitted files with the client function of this program!"
echo "[INFO] Please move the folder \"$FOLDER\" to a web-accessible directory and use this folder as the first argument for this script."
fi # /SERVER MODE
# # # #
# CLIENT MODE
# # # #
if [ $TYPE == "client" ]; then
URL=$FILE
# Check if remote directory is valid and has an info.cfg file
if [ $(wget -q --spider $URL/$INFO ; echo $?) != "0" ]; then
echo "Remote directory does not exists or has invalid info.cfg file. Aborting."
exit 1
fi
if [ -e $INFO ]; then rm $INFO; fi
wget -q $URL/$INFO
source $INFO
FOLDER="dl-$BIGNAME"
# Print basic status
echo
echo "Total filesize: $BIGSIZE"
echo "MD5: $BIGMD5"
echo "Number of splitted files: $SMALLNO"
echo "Download folder: "$(readlink -f $FOLDER)""
echo
checkwait
echo
checkfolder $FOLDER # Checks and creates folder
until [ "$STATUS" == "F" ]; do
for ((i = 0; i < ${#SMALLNAME[*]}; i++)); do
SMALLNAME=${SMALLNAME[$i]}
SMALLSIZE=${SMALLSIZE[$i]}
SMALLMD5=${SMALLMD5[$i]}
SMALLURL=$URL/$SMALLNAME
SMALLPATH=$FOLDER/$SMALLNAME
let NO=i+1
echo
echo "------------------"
echo "Starting file: $SMALLNAME ($SMALLSIZE bytes)"
echo "This is file $NO of total $SMALLNO files."
echo "Already downloaded "$(getdu $FOLDER)" of $BIGSIZE bytes."
echo
# File doesn't exist yet, so start fresh download
if [ ! -e $SMALLPATH ]; then
echo "[INFO] File doesn't exist yet. Starting new download."
wget -O $SMALLPATH $SMALLURL
if $(compall $SMALLPATH $SMALLSIZE $SMALLMD5); then
STATUS="C"
else
# Downloaded file is not valid. Restart for-loop
echo "[ERROR] Downloaded file is corrupt. Restarting."
STATUS="E"
break
fi
# File already exists but not finished yet
elif [ -e $SMALLPATH ] && ! $(compdu $SMALLPATH $SMALLSIZE); then
echo "[INFO] Continuing download."
wget -c -O $SMALLPATH $SMALLURL
if $(compall $SMALLPATH $SMALLSIZE $SMALLMD5); then
STATUS="C"
else
# Downloaded file is not valid. Restart for-loop
echo "[ERROR] Downloaded file is corrupt. Restarting."
STATUS="E"
break
fi
# File already exists, has correct size, but has wrong MD5
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH SMALLSIZE) && ! $(compmd5 $SMALLPATH $SMALLMD5); then
echo "[ERROR] MD5 is different but file has same size."
echo "[INFO] Deleting file and starting new download."
checkwait
rm $SMALLPATH
wget -O $SMALLPATH $SMALLURL
if $(compall $SMALLPATH $SMALLSIZE $SMALLMD5); then
STATUS="C"
else
# Downloaded file is not valid. Restart for-loop
echo "[ERROR] Downloaded file is corrupt. Restarting."
STATUS="E"
break
fi
# File already exists, has correct size, and has correct MD5
elif [ -e $SMALLPATH ] && $(compdu $SMALLPATH $SMALLSIZE) && $(compmd5 $SMALLPATH $SMALLMD5); then
echo "[SUCCESS] File already exists and is valid."
# This shouldn't happen...
else
echo "Dafuq?!"
STATUS="E"
fi
# Check if download is finished
if [ $NO -lt $SMALLNO ]; then
echo "[INFO] Starting next download."
STATUS="C"
elif [ $NO -lt $SMALLNO ] && [ "$STATUS" == "C" ]; then
echo "[SUCCESS] Downloading finished."
STATUS="F"
else
echo "Dafuq^2"
fi
done # /for smallnames
done
read -p "Download seems to be finished. Should the splitted files be rebuilt to the original big file again? [y/n] " YN
if [ $YN == "y" ]; then
# Destination file already exists
if [ -e $BIGNAME ]; then
read -p "Destination file already exists. Should it be overwritten? [y/n] " YN
if [ $YN == "y" ]; then
cat $FOLDER/dl-* > $BIGNAME
else
echo "[INFO] Skipping rebuilding."
fi
# Destination file doesn't exist yet
else
cat $FOLDER/dl-* > $BIGNAME
fi
fi
# Check big file for MD5 and size
sleep 2 # In rare cases, this can prevent a wrong du size
echo "[INFO] Checking correct size and MD5 hashsum for rebuilt big file..."
# Compare sizes
if $(compdu $BIGNAME $BIGSIZE); then
echo "[SUCCESS] The size of the completed file is corrent."
else
echo "[ERROR] The size of the completed file is incorrect."
fi
# Compare MD5 sum
if $(compmd5 $BIGNAME $BIGMD5); then
echo "[SUCCESS] The MD5 sum of the completed file is corrent."
else
echo "[ERROR] The MD5 sum of the completed file is incorrect."
fi
# Clean directories
read -p "Clean the download directory and info.cfg file? [y/n] " YN
if [ $YN == "y" ]; then
rm $INFO
rm -r $FOLDER
fi
fi # /CLIENT MODE