1
1
Fork 0
mirror of https://github.com/tonydamage/nux-env.git synced 2025-12-11 13:24:28 +01:00

Introduced new libraries for mime, thumbnails, labels

Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
This commit is contained in:
Tony Tkáčik 2017-07-15 12:10:44 +02:00
parent 0ab8c73e07
commit e173c4613c
4 changed files with 219 additions and 0 deletions

22
inc/labely.inc.sh Normal file
View file

@ -0,0 +1,22 @@
##
## Set of support functions for labelImg Xmls
labely.labels.get() {
local path="$1"
local filename=$(basename "$1")
local dirname=$(dirname "$1")
local labelXml="$dirname/.labely/${filename%.*}.xml"
nux.log info Looking for $labelXml
if [ -e $labelXml ] ; then
xmlstarlet sel -T -t \
-m "/annotation/object" \
-v "name" -o " " \
-m "bndbox" \
-v "/annotation/size/width" -o " " \
-v "/annotation/size/height" -o " " \
-v "xmin" -o " " \
-v "ymin" -o " " \
-v "xmax" -o " " \
-v "ymax" -n $labelXml
fi
}

View file

@ -157,6 +157,14 @@ function nux.check.nuxenv.file {
[[ "$path" =~ "^$NUX_ENV_DIR" ]] [[ "$path" =~ "^$NUX_ENV_DIR" ]]
} }
function nux.check.optional {
local function="$1"; shift;
if nux.check.function "$function" ; then
$function "$@"
fi
}
function nux.check.exec { function nux.check.exec {
local binary=$1; local binary=$1;
test -n "$(which "$binary")" test -n "$(which "$binary")"
@ -183,6 +191,16 @@ function nux.exec.optional {
fi fi
} }
function nux.exec.or {
local maybe="$1"; shift;
local to_exec="$1"; shift;
if nux.check.function "$maybe" ; then
to_exec=$maybe
fi
$to_exec "$@";
}
function nux.dirty.urlencode { function nux.dirty.urlencode {
echo -n "$1" | sed "s/ /%20/g" echo -n "$1" | sed "s/ /%20/g"
} }

22
inc/nux.mime.inc.sh Normal file
View file

@ -0,0 +1,22 @@
nux.mime() {
local type=binary/octet
local suffix="${1##*.}"
case "${suffix,,}" in
txt) type=text/plain;;
css) type=text/css;;
jpg) type=image/jpeg;;
png) type=image/png;;
zip) type=application/zip;;
cbr) type=application/x-cbz;;
cbz) type=application/x-cbr;;
pdf) type=application/pdf;;
epub) type=application/epub+zip;;
mp4) type=video/mp4;;
*)
if [ -d "$1" ]; then
type=directory
fi
esac
echo $type;
}

157
inc/thumby.inc.sh Normal file
View file

@ -0,0 +1,157 @@
nux.use nux.mime
function thumby.name.shared() {
echo $(basename "$1"|md5sum|cut -d " " -f1).png
}
function thumby.noop() {
:
}
function thumby.mimetype.supported() {
local mimetype="$1";
local base="${mimetype%%/*}";
local helper="${mimetype/\//.}";
if [ "$base" == "image" ]; then
return 0;
elif nux.check.function thumby.thumb.source.locator.$helper; then
return 0;
elif nux.check.function thumby.thumb.source.extractor.$helper; then
return 0;
fi
return 1;
}
function thumby.thumb.can.generate() {
local path="$1";
local mimetype="$2";
local helper="${mimetype/\//.}";
if ! thumby.mimetype.supported "$mimetype" ; then
return 1;
fi
if nux.check.function thumby.thumb.source.locator.$helper; then
local source=$(thumby.thumb.source.locator.$helper "$path");
if [ -z "$source" ] ; then
return 1;
fi
fi
return 0;
}
function thumby.thumb.get() {
local path="$1";
local mimetype="$2";
if [ ! -e "$path" ] ; then
return -1;
fi
if [ -z "$mimetype" ] ; then
mimetype=$(nux.mime "$path")
fi
local filename=$(basename "$1")
local dirname=$(dirname "$1")
local thumbname="$(thumby.name.shared "$filename")";
local thumbpath="$dirname/.sh_thumbnails/large/$thumbname";
if [ ! -e "$thumbpath" ]; then
helper="${mimetype/\//.}"
nux.log debug "File $path, type $mimetype does not have thumbnail. Trying to generate using $helper."
local preexec=thumby.noop;
local postexec=thumby.noop;
local source=$path;
local streamer=thumby.noop;
if nux.check.function thumby.thumb.source.locator.$helper ; then
source=$(thumby.thumb.source.locator.$helper "$path");
fi
if nux.check.function thumby.thumb.source.extractor.$helper ; then
echo "Using source helper" >&2
source="-"
streamer=thumby.thumb.source.extractor.$helper;
fi
mkdir -p "$dirname/.sh_thumbnails/large" &>/dev/null
mtime=`stat -c '%Y' "$path"`
$preexec "$path"
nux.log info "Source is : $source, Streamer is $streamer";
$streamer "$path" | convert -thumbnail '256x256>' -strip "$source" "$thumbpath" >&2
$postexec "$path"
echo $thumbpath;
fi
}
function thumby.thumb.generate() {
convert -thumbnail '256x256>' -strip "$path" "$thumbpath" >&2
}
function thumby.get.thumb() {
local path="$1";
local d="$(dirname "$1")";
local f=$(basename "$1");
local thumb_name="$(thumby.name.shared "$f")"
if [ ! -e "$path" ] ; then
return;
elif [ ! -e "$d/.sh_thumbnails/large/$thumb_name" ] ; then
#mkdir -p .sh_thumbnails/normal &>/dev/null
mkdir -p "$d/.sh_thumbnails/large" &>/dev/null
#md5=`echo $path|md5sum|cut -d" " -f1`
mtime=`stat -c '%Y' "$path"`
nux.log info "Generating thumbnails for $path $thumb_name" >&2
#convert -thumbnail '128x128>' -strip -set Thumb::MTime "$mtime" -set Thumb::URI "$path" "$path" .sh_thumbnails/normal/$md5.$THUMB_TYPE >&2
nux.log info "Command line is: " # echo convert -thumbnail '256x256>' -strip -set Thumb::MTime "$mtime" -set Thumb::URI "$path" "$path" .sh_thumbnails/large/$thumb_name
nux.log info convert -thumbnail '256x256>' --strip "$path" "$d"/.sh_thumbnails/large/$thumb_name
convert -thumbnail '256x256>' -strip "$path" "$d"/.sh_thumbnails/large/$thumb_name >&2
fi
echo "$d/.sh_thumbnails/large/$thumb_name"
}
thumby.thumb.source.locator.directory() {
nux.log info "Using find to find jpg or png"
find "$1" -maxdepth 1 -iname "*.jpg" -or -iname "*.png" | sort -n | head -n1
}
thumby.thumb.source.locator.application.pdf() {
echo "$1[0]"
}
thumby.thumb.source.extractor.application.epub+zip() {
local rootDesc=$(unzip -p "$1" META-INF/container.xml \
| xmlstarlet sel -N od="urn:oasis:names:tc:opendocument:xmlns:container" \
-t -v "/od:container/od:rootfiles/od:rootfile[@media-type='application/oebps-package+xml']/@full-path" -n)
nux.log info "Root description is in: $rootDesc";
local imgDesc=$(unzip -p "$1" "$rootDesc" \
| xmlstarlet sel -N opf="http://www.idpf.org/2007/opf" \
-t -m "/opf:package/opf:manifest/opf:item[@id=/opf:package/opf:metadata/opf:meta[@name='cover']/@content]" \
-v "@href" -o ":" -v "@media-type" -n)
IFS=":" read -r img media <<< "$imgDesc";
nux.log info "Image name is $imgDesc $img";
if [ -n "$img" ]; then
unzip -p "$1" $img
fi
}
thumby.thumb.source.extractor.application.x-cbr() {
suffix="${1##*.}"
case "$suffix" in
zip) ;&
cbz)
potential=$(unzip -l "$1" | sed -re "s/^ *[0-9]+ +[0-9\\-]+ +[0-9:]+ +//gi" | grep -E '\.((jpg)|(png)|(jpeg))$' | sort -n | head -n 1)
nux.log debug "Potential preview is: $potential";
if [ -n "$potential" ]; then
unzip -p "$1" "$potential"
nux.log debug "Preview extracted."
fi
;;
*) nux.log error "$suffix is not supported."
esac
}