mkv_check.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/bash
  2. # ARGUMENTS:
  3. # ----------
  4. # $1 = Name of the file
  5. # $2 = Actual path to file
  6. # $3 = CRC code of the file
  7. # $PWD = Current Path.
  8. # EXIT CODES:
  9. # -----------
  10. # 0 - Good: Give credit and add to user stats.
  11. # 2 - Bad: No credit / No stats & file is removed.
  12. # 10-1010 - Same as 2, but glftpd will sleep for exitcode-10 seconds
  13. # note: 127 is reserved, so don't use that. 127 is treated like 1,
  14. # causing glftpd to spit out "script could not be executed".
  15. ### SETTINGS
  16. #
  17. # NOTE: entries in the PATHS_CHECK and PATHS_EXCLUDE lists are only separated by space
  18. #
  19. PATH_NEXT_POST_CHECK="/bin/zipscript-c"
  20. PATH_GLFTPD_LOG="/ftp-data/logs/glftpd.log"
  21. PATH_MKVINFO="/bin/mkvinfo"
  22. PATHS_CHECK=("sample" "proof"); # case insensitive
  23. PATHS_EXCLUDE=("/site/private/"); # case insensitive
  24. DELETE_MKV_FAIL=0 # set to 1 in order to delete broken mkv files automatically
  25. #
  26. ###
  27. chain_post_check()
  28. {
  29. $PATH_NEXT_POST_CHECK $1 $2 $3
  30. exit $?
  31. }
  32. # file must end in 'mkv'
  33. if [[ $1 != *mkv ]]; then
  34. # echo "Not an MKV file, ignoring"
  35. # exit 0
  36. chain_post_check $1 $2 $3
  37. fi
  38. # remove empty files right away
  39. if [ ! -s "$2/$1" ]; then
  40. # echo "File is 0 bytes, deleting..."
  41. exit 2
  42. fi
  43. # check if the last component in the path is one of directores
  44. # we want to check
  45. pathLc=`echo "$2" | tr '[A-Z]' '[a-z]'`
  46. element_count=${#PATHS_CHECK[@]}
  47. index=0
  48. bDoCheck=0
  49. while [ "$index" -lt "$element_count" ]
  50. do
  51. strDir=${PATHS_CHECK[$index]}
  52. strDirLc=`echo "$strDir" | tr '[A-Z]' '[a-z]'`
  53. if [[ $pathLc == */$strDirLc ]] || [[ $pathLc == */$strDirLc/ ]]; then
  54. bDoCheck=1
  55. fi
  56. let "index = $index + 1"
  57. done
  58. if [[ $bDoCheck == 0 ]]; then
  59. # echo "Directory will not be checked"
  60. # exit 0
  61. chain_post_check $1 $2 $3
  62. fi
  63. # check if we are in an excluded directory
  64. element_count=${#PATHS_EXCLUDE[@]}
  65. index=0
  66. bDoCheck=1
  67. while [ "$index" -lt "$element_count" ]
  68. do
  69. strDir=${PATHS_EXCLUDE[$index]}
  70. strDirLc=`echo "$strDir" | tr '[A-Z]' '[a-z]'`
  71. if [[ $pathLc == $strDirLc* ]] ; then
  72. bDoCheck=0
  73. fi
  74. let "index = $index + 1"
  75. done
  76. if [[ $bDoCheck == 0 ]]; then
  77. # echo "Directory is excluded"
  78. # exit 0
  79. chain_post_check $1 $2 $3
  80. fi
  81. # mkvinfo needs LC_ALL to be set
  82. export LC_ALL=C
  83. EXPECTED=`$PATH_MKVINFO -z $2/$1 | grep '^+' | awk '{total += $NF} END{print total}'`
  84. # deal with mkv files with broken header information
  85. if [[ $EXPECTED -lt 10000 ]]; then
  86. EXPECTED=0
  87. fi
  88. # stat might not be portable
  89. # ACTUAL=$(stat -c%s "$2/$1")
  90. ACTUAL=$(wc -c <"$2/$1")
  91. TIMESTAMP=`date +"%a %b %-d %T %Y"`
  92. if [[ $EXPECTED == $ACTUAL ]]; then
  93. echo "$TIMESTAMP MKV_PASS: \"$2\" \"$1\" \"$EXPECTED\" \"$ACTUAL\"" >> $PATH_GLFTPD_LOG
  94. exit 0
  95. fi
  96. echo "$TIMESTAMP MKV_FAIL: \"$2\" \"$1\" \"$EXPECTED\" \"$ACTUAL\"" >> $PATH_GLFTPD_LOG;
  97. if [[ $DELETE_MKV_FAIL == 1 ]] && [[ $EXPECTED -gt 0 ]]; then
  98. exit 2
  99. fi
  100. exit 0