|
|
| |||||||
| |||||||||
巻き戻し中。
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2024-01-03(水) 冬休みの [長年日記]
_
宿題
写真アルバムの自動生成。
長男氏とNASに置いてる写真の共有方法を考えたのは良いが、apacheの自動インデックスだとサムネとか見えないので困る。
と言うことでhtmlのインデックス自動生成ツールを作っていたのだけど、ようやく完了。
とりあえず生成するだけのものは数日で出来て、長男には公開してた。
でも、ファイル追加したら自動で生成とか重複起動防止とか、体裁をちゃんとするのは冬休みの宿題だったのだ。
ちなみに、表示ファイルのサムネを作っているのではなくて縮小表示で並べてるだけなので表示時間は掛かる。
いわゆる手抜きである(゚∀゚)
#!/bin/sh
#################################################
# Auto HTML index generator #
# By H.Uekusa 20240101 #
# Ver 1.00 #
#################################################
#
#################################################
# Location of commands #
#################################################
LS="/bin/ls"
PWD="/bin/pwd"
AWK="/usr/bin/awk"
GREP="/usr/bin/grep"
FIND="/usr/bin/find"
DIFF="/usr/bin/diff"
LOGGER="/usr/bin/logger"
SORT="/usr/bin/sort"
CAT="/bin/cat"
#################################################
# Location/Name of files and directories #
#################################################
PIDFILE="/var/run/make_thumb_index.pid"
PIDFILE2="/var/run/make_thumb_index-oneexec.pid"
BASE_DIR="/HOGEHOGE/HOGEHOGE"
PHOTO_DIR="HOGEHOGEHOGE"
TMP_DIR="/var/tmp"
SELF="/HOGEHOGE/make_thumb_index.sh"
SELF_NAME="make_thumb_index.sh"
HTML_icon="/img/HTML-file.png"
File_icon="/img/simple-file.png"
Folder_icon="/img/simple-folder-b.png"
#################################################
# Parameters for html "table" #
#################################################
NUM_FOLD="5"
NUM_FILE="4"
#################################################
# Flags for debug #
#################################################
TST_FLAG="0"
LOG_FLAG="0"
#################################################
# Main procedure #
#################################################
OPTION=`echo $1`
if [ $TST_FLAG != "0" ];then
echo $OPTION
fi
#################################################
# Force exec oprion "-f" #
#################################################
if [ "$OPTION" == "-f" ];then
rm -f $TMP_DIR/Photo_LS-LR.prev
if [ -f "$PIDFILE" ];then
PIDKILL=`$CAT $PIDFILE`
$LOGGER -is -t $SELF_NAME "Killing $PIDKILL"
kill -9 $PIDKILL
rm -f $PIDFILE
fi
if [ -f "$PIDFILE2" ];then
PIDKILL2=`$CAT $PIDFILE2`
$LOGGER -is -t $SELF_NAME "Killing $PIDKILL2"
kill -9 $PIDKILL2
rm -f $PIDFILE2
fi
$SELF
exit 4
#################################################
# Normal execute mode #
#################################################
elif [ "$OPTION" == "" ];then
if [ -f "$PIDFILE" ];then #Exist PID file?
$LOGGER -is -t $SELF_NAME "There is PID file at $PIDFILE,I am exiting"
$LOGGER -is -t $SELF_NAME "If you would like to start forcibly, Use -f option first"
exit 2
fi
PID=`echo $$`
echo $PID > $PIDFILE
$FIND $BASE_DIR/$PHOTO_DIR -ls \
|$AWK 'NR > 0 \
{printf $3" "$7" "$8" "$9" "$10" "; printf "\"" ; \
for (i = 11; i < NF; i++) { printf("%s ", $i) } printf $NF; print "\"" }' \
| $GREP $PHOTO_DIR \
> $TMP_DIR/Photo_LS-LR
if [ ! -f $TMP_DIR/Photo_LS-LR.prev ];then
touch $TMP_DIR/Photo_LS-LR.prev
fi
$DIFF $TMP_DIR/Photo_LS-LR $TMP_DIR/Photo_LS-LR.prev \
>/dev/null 2>&1
DIFF_STAT=`echo $?`
if [ $TST_FLAG != "0" ];then
echo "diff-stat $DIFF_STAT"
fi
if [ "$DIFF_STAT" == "1" ];then
$DIFF $TMP_DIR/Photo_LS-LR $TMP_DIR/Photo_LS-LR.prev | $GREP "^< d" \
|$AWK 'NR > 0 {for ( i = 7 ; i < NF ; i++ ) { printf ( "%s ", $i ) } printf $NF ; print "/" }' \
| sed s/'"\/$'/'\/"'/g \
> $TMP_DIR/Photo-DIR.0
$DIFF $TMP_DIR/Photo_LS-LR $TMP_DIR/Photo_LS-LR.prev | $GREP -v "^< d" \
|$AWK 'NR > 0 {for ( i = 7 ; i < NF ; i++ ) { printf ( "%s ", $i ) } printf $NF ; print "/" }' \
| sed s/'"\/$'/'\/"'/g \
| $AWK -F/ '{for (i = 1; i <= NF - 2 ; i++){printf $i"/"};print "\"" }' \
>> $TMP_DIR/Photo-DIR.0
$SORT -u $TMP_DIR/Photo-DIR.0 | $GREP -v '^\"$' > $TMP_DIR/Photo-DIR
DIFF_COUNT=`$CAT $TMP_DIR/Photo-DIR | wc -l | $AWK '{print $1}'`
if [ "$LOG_FLAG" == "0" ];then
$LOGGER -is -t $SELF_NAME "make_thumb_index.sh" "There is $DIFF_COUNT diff"
$LOGGER -is -t $SELF_NAME "make_thumb_index.sh" "Remaking index..."
else
echo "There is $DIFF_COUNT diff"
fi
$AWK -v SELF=$SELF '{system ("cd "$0"; "SELF" -oneexec"); \
printf("%100s\r"," "); \
printf $0"\r"}; END {printf "\n"}' $TMP_DIR/Photo-DIR
$FIND $BASE_DIR/$PHOTO_DIR -ls |$AWK 'NR > 0 \
{printf $3" "$7" "$8" "$9" "$10" "; printf "\"" ; \
for (i = 11; i < NF; i++) { printf("%s ", $i) } printf $NF; print "\"" }' \
| $GREP $PHOTO_DIR \
> $TMP_DIR/Photo_LS-LR.prev
$LOGGER -is -t $SELF_NAME "make_thumb_index.sh" "Remaking $DIFF_COUNT indexes done"
rm -f $PIDFILE
exit 1
else
if [ "$LOG_FLAG" == "0" ];then
$LOGGER -is -t $SELF_NAME "make_thumb_index.sh" "There is no diff"
else
echo "There is no diff"
fi
fi
rm -f $PIDFILE
exit 0
#################################################
# One exec mode for each directories #
# (For child process) #
#################################################
elif [ "$OPTION" == "-oneexec" ];then
PID2=`echo $$`
echo $PID2 > $PIDFILE2
if [ $TST_FLAG == "0" ];then
echo "<html>" > index.html
echo "<head></head>" >> index.html
echo "<body>" >> index.html
$PWD | $AWK -F$BASE_DIR/ '{print "<font size=+3><b>Index of /"$2"</b></font><br>"}' \
>> index.html
echo "<font size=+2><b><a href='"'../'"' > Parent Directory </a></b></font><br><br><b>" \
>> index.html
echo -n `$LS -lp | $GREP -v ^d | $GREP -v ^total |wc -l` \
>> index.html
echo -n " Files " >> index.html
echo -n `$LS -lp | $GREP ^d | $GREP -v ^total |wc -l` \
>> index.html
echo " Directories</b><br>" >> index.html
echo "<table border=1><tr>" >> index.html
$LS -lp | $GREP ^d \
| $AWK '{c="";for(i=9;i<=NF;i++) c=c $i" "; print c}' \
| $AWK '{print $0}' | sort \
| $AWK -F. -v Folder_icon=$Folder_icon -v NUM_FOLD=$NUM_FOLD '{\
if ($0 ~ /\/ $/) \
{print "<td><a href=\"./"$0"\"><img width=120 src=\""Folder_icon"\"><br>"$0"</a></td>"} \
if (NR % NUM_FOLD == 0) {print "</tr><tr>"} }\
END{if (NR % NUM_FOLD != 0) {print "</tr>"} \
}' >> index.html
$LS -lp | $GREP -v ^d | $GREP -v ^total \
| $AWK '{c="";for(i=9;i<=NF;i++) c=c $i" "; print c}' \
| $AWK '{print $0}' | sort \
| $AWK -F. -v HTML_icon=$HTML_icon -v File_icon=$File_icon -v NUM_FILE=$NUM_FILE '{\
if ($2 == "JPG " || $2 == "jpg " || $2 == "jpeg " || $2 == "gif " || $2 == "BMP " || $2 == "bmp " || $2 == "png ") \
{print "<td><a target=\"_blank\" href=\"./"$0"\"><img width=240 src=\"./"$0"\"><br>"$0"</a></td>"} \
else if($2 == "html " || $2 == "HTML " || $2 == "htm " ||$2 == "HTM ") \
{print "<td><a href=\"./"$0"\"><img width=80 src=\""HTML_icon"\"><br>"$0"</a></td>"} \
else {print "<td><a href=\"./"$0"\"><img width=80 src=\""File_icon"\"><br>"$0"</a></td>"} \
if (NR % NUM_FILE == 0) {print "</tr><tr>"} }\
END{print "</tr>" }' \
>> index.html
echo "</table>" >> index.html
echo "</body>" >> index.html
echo "</html>" >> index.html
fi
rm -f $PIDFILE2
exit 3
fi
#################################################
# Error handling for unknown option #
#################################################
echo "Unknown option"
echo "Usage: \"make_thumb_index.sh\" for all recurcive directories from BASE_DIR"
echo " or \"make_thumb_index.sh -oneexec\" for current directory"
echo " or \"make_thumb_index.sh -f\" for execute forcibly"
exit 9
*1*2*3
_
使い方
冒頭の編集を環境に合わせたらcronで適当に。
生成はディレクトリ数が多いと10分以上掛かるけど、重複起動は排除してるので時間間隔はあまり気にしなくて良いはず。
あるのは起動中に再度やり直したり、途中で死んでPIDファイルが残った時用の-fオプションぐらい。
[ツッコミを入れる]
| Tweets by RC31E | |||||||||
| |||||||||
| |||||||||




