mirror of
https://codeberg.org/h3xx/optipdf
synced 2024-08-15 00:03:23 +00:00
Compare commits
No commits in common. "44308e890f9459e303564f979d54f16f61335507" and "8dcfacd5d7a706c7c1d19577c37d1a84d8ec1e58" have entirely different histories.
44308e890f
...
8dcfacd5d7
1 changed files with 37 additions and 23 deletions
60
optipdf
60
optipdf
|
@ -105,22 +105,6 @@ if [[ ${#FILES[@]} -eq 0 ]]; then
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
check_required_binaries() {
|
|
||||||
local BIN MISSING=()
|
|
||||||
for BIN; do
|
|
||||||
if ! type -t "$BIN" &>/dev/null; then
|
|
||||||
MISSING+=("$BIN")
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
if [[ ${#MISSING[@]} -gt 0 ]]; then
|
|
||||||
printf 'Error: You are missing required programs:\n' >&2
|
|
||||||
for BIN in "${MISSING[@]}"; do
|
|
||||||
printf -- '- %s\n' "$BIN" >&2
|
|
||||||
done
|
|
||||||
exit 2
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
file_size() {
|
file_size() {
|
||||||
stat \
|
stat \
|
||||||
--format='%s' \
|
--format='%s' \
|
||||||
|
@ -132,10 +116,43 @@ file_size() {
|
||||||
|
|
||||||
# produces a human-readable size from the byte count passed to it
|
# produces a human-readable size from the byte count passed to it
|
||||||
hr_size() (
|
hr_size() (
|
||||||
numfmt \
|
declare -i BYTES=$1
|
||||||
--to=iec \
|
|
||||||
--suffix=B \
|
#UNITS=(B KB MB GB TB PB EB ZB YB) # shell math can only go so far...
|
||||||
"$@"
|
UNITS=(B KB MB GB TB)
|
||||||
|
FACT=1024
|
||||||
|
THRESH=9/10
|
||||||
|
DECIMALS=1
|
||||||
|
DECIMALS_FACTOR=$(( 10 ** DECIMALS ))
|
||||||
|
|
||||||
|
# cycle through units from largest to smallest, exiting when it finds the
|
||||||
|
# largest applicable unit
|
||||||
|
for (( EXP = ${#UNITS[@]} - 1; EXP > -1; --EXP )); do
|
||||||
|
# check if the unit is close enough to the unit's size, within the
|
||||||
|
# threshold
|
||||||
|
if [[ $BYTES -gt $((FACT ** EXP * $THRESH)) ]]; then
|
||||||
|
# we found the applicable unit
|
||||||
|
|
||||||
|
# must multiply by a factor of 10 here to not truncate
|
||||||
|
# the given number of decimal places after the point
|
||||||
|
HR_VAL=$(( BYTES * DECIMALS_FACTOR / FACT ** EXP ))
|
||||||
|
|
||||||
|
# put the decimal point in
|
||||||
|
if [[ $DECIMALS -gt 0 ]]; then
|
||||||
|
HR_VAL=$(( HR_VAL / DECIMALS_FACTOR )).$(( HR_VAL % DECIMALS_FACTOR ))
|
||||||
|
fi
|
||||||
|
|
||||||
|
HR_UNIT=${UNITS[$EXP]}
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ -z $HR_UNIT ]]; then
|
||||||
|
HR_VAL=$BYTES
|
||||||
|
HR_UNIT=${UNITS[0]}
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf '%g %s\n' "$HR_VAL" "$HR_UNIT"
|
||||||
)
|
)
|
||||||
|
|
||||||
setup_tempdir() {
|
setup_tempdir() {
|
||||||
|
@ -216,9 +233,6 @@ use_smaller() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
check_required_binaries \
|
|
||||||
qpdf
|
|
||||||
|
|
||||||
QPDF_ARGS=(
|
QPDF_ARGS=(
|
||||||
--compression-level=9
|
--compression-level=9
|
||||||
--deterministic-id
|
--deterministic-id
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue