2015-06-16 13:00:38 +03:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
FILE="$1"
|
|
|
|
|
TYPE="$2"
|
2015-06-16 13:24:17 +03:00
|
|
|
SPLITSIZE="10M"
|
2015-06-16 13:00:38 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
function checkwait {
|
|
|
|
|
read -p "Continue? Press Ctrl+C to cancel." END
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2015-06-16 13:21:00 +03:00
|
|
|
INFO="$FOLDER/$INFO"
|
2015-06-16 13:00:38 +03:00
|
|
|
|
2015-06-16 13:22:39 +03:00
|
|
|
echo "[INFO] Calculating size and MD5 sum..."
|
2015-06-16 13:00:38 +03:00
|
|
|
BIGNAME=$(basename $FILE)
|
|
|
|
|
BIGSIZE=$(du $FILE | awk -F" " '{ print $1 }')
|
|
|
|
|
BIGMD5=$(md5sum $FILE | awk -F" " '{ print $1 }')
|
|
|
|
|
|
|
|
|
|
echo "BIGNAME=$BIGNAME" >> $INFO
|
|
|
|
|
echo "BIGSIZE=$BIGSIZE" >> $INFO
|
|
|
|
|
echo "BIGMD5=$BIGMD5" >> $INFO
|
|
|
|
|
echo >> $INFO
|
|
|
|
|
|
2015-06-16 13:21:00 +03:00
|
|
|
echo "[INFO] Splitting big file into smaller parts..."
|
|
|
|
|
split --verbose -a 4 -b $SPLITSIZE $FILE $FOLDER/dl-
|
2015-06-16 13:00:38 +03:00
|
|
|
|
|
|
|
|
# List all splitted files, measure size and md5sum
|
2015-06-16 13:21:00 +03:00
|
|
|
echo "[INFO] Creating info document with necessary specs..."
|
|
|
|
|
SMALLNO=$(ls $FOLDER | grep -v $INFO | wc -l)
|
2015-06-16 13:00:38 +03:00
|
|
|
echo "SMALLNO=$SMALLNO" >> $INFO
|
|
|
|
|
|
|
|
|
|
NO=0
|
2015-06-16 13:21:00 +03:00
|
|
|
ls $FOLDER | grep -v $INFO | while read -r line; do
|
2015-06-16 13:00:38 +03:00
|
|
|
|
|
|
|
|
SMALLNAME=$line
|
|
|
|
|
SMALLSIZE=$(du $FOLDER/$SMALLNAME | awk -F" " '{ print $1 }')
|
|
|
|
|
SMALLMD5=$(md5sum $FOLDER/$SMALLNAME | awk -F" " '{ print $1 }')
|
|
|
|
|
|
|
|
|
|
echo "SMALLNAME[$NO]=$SMALLNAME" >> $INFO
|
|
|
|
|
echo "SMALLSIZE[$NO]=$SMALLSIZE" >> $INFO
|
|
|
|
|
echo "SMALLMD5[$NO]=$SMALLMD5" >> $INFO
|
|
|
|
|
|
|
|
|
|
let NO=NO+1
|
|
|
|
|
done
|
|
|
|
|
|
2015-06-16 13:21:00 +03:00
|
|
|
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."
|
2015-06-16 13:05:45 +03:00
|
|
|
|
2015-06-16 13:00:38 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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 "$(du -s $FOLDER | awk -F" " '{ print $1 }')" of $BIGSIZE bytes."
|
|
|
|
|
echo
|
|
|
|
|
|
|
|
|
|
# File doesn't exist yet, so start fresh download
|
|
|
|
|
if [ ! -e $SMALLPATH ]; then
|
|
|
|
|
echo "[INFO] File doesn't exits yet. Starting new download."
|
|
|
|
|
wget -O $SMALLPATH $SMALLURL
|
|
|
|
|
|
|
|
|
|
# File already exists but not finished yet
|
|
|
|
|
elif [ -e $SMALLPATH ] && [ $(du $SMALLPATH | awk -F" " '{ print $1 }') != $SMALLSIZE ]; then
|
|
|
|
|
echo "[INFO] Continuing download."
|
|
|
|
|
wget -c -O $SMALLPATH $SMALLURL
|
|
|
|
|
|
|
|
|
|
# File already exists, has correct size, but has wrong MD5
|
|
|
|
|
elif [ -e $SMALLPATH ] && [ $(du $SMALLPATH | awk -F" " '{ print $1 }') == $SMALLSIZE ] && [ $(md5sum $SMALLPATH | awk -F" " '{ print $1 }') != $SMALLMD5 ]; then
|
|
|
|
|
echo "[ERROR] MD5 is different but file has same size."
|
|
|
|
|
echo "[INFO] Deleting file and starting new download."
|
2015-06-16 13:26:52 +03:00
|
|
|
checkwait
|
|
|
|
|
rm $SMALLPATH
|
2015-06-16 13:00:38 +03:00
|
|
|
wget -O $SMALLPATH $SMALLURL
|
|
|
|
|
|
|
|
|
|
# File already exists, has correct size, and has correct MD5
|
|
|
|
|
elif [ -e $SMALLPATH ] && [ $(du $SMALLPATH | awk -F" " '{ print $1 }') == $SMALLSIZE ] && [ $(md5sum $SMALLPATH | awk -F" " '{ print $1 }') == $SMALLMD5 ]; then
|
|
|
|
|
echo "[SUCCESS] File already exists and is valid."
|
|
|
|
|
if [ $NO -lt $SMALLNO ]; then
|
|
|
|
|
echo "[INFO] Starting next download."
|
|
|
|
|
else
|
|
|
|
|
echo "[SUCCESS] Downloading finished."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# This shouldn't happen...
|
|
|
|
|
else
|
|
|
|
|
echo "Dafuq?!"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
sleep 2 # In rare cases, this can prevent a wrong du size
|
|
|
|
|
SIZE=$(du $BIGNAME | awk -F" " '{ print $1 }')
|
|
|
|
|
MD5=$(md5sum $BIGNAME | awk -F" " '{ print $1 }')
|
|
|
|
|
# Compare sizes
|
|
|
|
|
if [ "$SIZE" == "$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 [ "$MD5" == "$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
|