fichiers.breizh.pm/generator.sh

214 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
# Récupération du chemin (passé en argument, sinon le dossier courant)
# et transformatio en chemin web (/ correspondra donc au docroot).
webpath="$(echo "${1:-$PWD}" | sed "s#^$root/##g")"
# Si jamais le path obtenu est celui de la racine, on vide la variable
# (pas très safe en cas de répétition de l'arborescence)
[[ "$webpath" =~ ^$root ]] && unset webpath
# Couleur de logs juste parce que c'est joli
if [[ -n "$TERM" ]]
then
blue=$(tput setaf 4 2>/dev/null)
green=$(tput setaf 2 2>/dev/null)
reset=$(tput sgr0 2>/dev/null)
fi
# Annonce du début du traitement
echo "${blue}Start${reset} /$webpath"
# Déplacement dans le dossier à traiter
cd "${root}/${webpath}"
# Encode certains caractères posant problème dans le HTML
encodeUrl() {
encoded="${*//\&/%26}"
encoded="${encoded//\?/%3F}"
encoded="${encoded//\#/%23}"
encoded="${encoded//\"/%22}"
printf "%s" "${encoded}"
}
# Définition des différentes parties de la page
header() {
cat <<DELIM
<!DOCTYPE html>
<html lang=fr>
<head>
<title>Index de ${1:-fichiers.breizh.pm}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/.assets/default.css">
</head>
<body>
<h2>Index de ${2}</h2>
<table id="maintable">
<tr>
<th onclick="sortTable(0)">Nom</th>
<th onclick="sortTable(1)">Taille</th>
<th onclick="sortTable(2)">Dernière modification</th>
<th onclick="sortTable(3)">Type / Aperçu</th>
</tr>
DELIM
}
row() {
cat <<DELIM
<tr>
<td><a href="$(encodeUrl "${1}")">${1}</a></td>
<td class="right-align" data-kbytes="${2}">${3}</td>
<td class="right-align">${4}</td>
<td>${5}</td>
</tr>
DELIM
}
footer() {
cat <<DELIM
</table>
<script src="/.assets/sort.js"></script>
<script src="/.assets/player.js"></script>
</body>
</html>
DELIM
}
# Génération du header
# Le navpath est le chemin indiqué en haut de la page, qui permet de remonter
# dans l'arborescence
navpath="<a href=\"/\">fichiers.breizh.pm</a>/"
tmp="/"
IFS=/
for folder in $webpath
do
tmp+="$(encodeUrl "$folder")/"
navpath+="<a href=\"$tmp\">$folder</a>/"
done
unset IFS
# Insertion du header dans le fichier
header "$webpath" "$navpath" > index.html
# Génération du tableau
preload=metadata # type de préchargement des fichiers audio
mkdir -p "$cache" # création du dossier de cache
# Pour chaque dossier
for i in */
do
if [[ ! -d "$i" ]]
then continue
fi
# Initialisation
unset lastmodif kbytes size thumbname mimetype
# Exclusion(s), relative(s) ou absolue(s)
if [[ "$i" == "index.html" ]] \
|| [[ "$(realpath "$i")" =~ ^$root/p$ ]] \
|| [[ "$(realpath "$i")" =~ ^$root/robots.txt ]]
then
continue
fi
# Date de dernière modif
lastmodif="$(stat -c "%y" "$i" | cut -d':' -f-2)"
# Insertion de la ligne du tableau correspondant au fichier en cours de traitement
row "${i::-1}" "0" "-" "$lastmodif" "inode/directory" >> index.html
done
# Pour chaque fichier
for i in *
do
if [[ -d "$i" ]]
then continue
fi
# Initialisation
unset lastmodif kbytes size thumbname mimetype
# Exclusion(s), relative(s) ou absolue(s)
if [[ "$i" == "index.html" ]] \
|| [[ "$(realpath "$i")" =~ ^$root/p$ ]] \
|| [[ "$(realpath "$i")" =~ ^$root/robots.txt ]]
then
continue
fi
# Ne pas précharger Musique/Vidéos qui est trop volumineux
if [[ "$(realpath "$i")" =~ ^$root/Musique/Vidéos || "$(realpath "$i")" =~ ^$root/Musique.opus/Vidéos ]]
then
preload=none
fi
# Date de dernière modif
lastmodif="$(stat -c "%y" "$i" | cut -d':' -f-2)"
# Taille des fichier (ignoré pour les répertoires)
if [[ -d "$i" ]]
then
kbytes=0
size="-"
else
kbytes="$(du "$i" | cut -f1)"
size="$(du -h "$i" | cut -f1)"
fi
# Hardcodage de certains types MIME
[[ "$i" =~ \.mp3$ ]] && mimetype="audio/mpeg"
[[ "$i" =~ \.mp4$ ]] && mimetype="video/mp4"
[[ "$i" =~ \.flac$ ]] && mimetype="audio/flac"
[[ "$i" =~ \.opus$ ]] && mimetype="audio/ogg"
# Détection automatique du type MIME si non hardcodé
[[ -z "$mimetype" ]] && mimetype="$(file --mime-type -b -e ascii -e compress -e tar -e cdf "$i")"
# Traitements spécifiques selon le type
case $mimetype in
audio/*)
# Pour l'audio, ajout d'un lecteur
type="<audio controls preload=\"$preload\"><source src=\"$(encodeUrl "${i}")\" type=\"$mimetype\">$mimetype</audio>";;
image/*)
# Pour les images, une miniature de 60px de haut est créée
thumbname="$(md5sum <<<$(readlink -f "$i") | cut -d' ' -f1).${i##*.}" # Nom sur base d'un hash pour éviter les conflits
# Si la miniature n'est pas déjà présente et que l'image est assez petite
if [[ ! -f "${cache}/${thumbname}" ]] && [[ ${kbytes} -le 20480 ]] && [[ ! ${i} =~ \.v2m$ ]]
then
# Création de la miniature
convert "${i}" -strip -thumbnail 'x60>' "${cache}/${thumbname}"
fi
# Si une miniature est présente, on l'affiche
if [[ -f "${cache}/${thumbname}" ]]
then
type="<img src=\"/.thumbnails/${thumbname}\" alt=\"$mimetype\" />"
# Sinon on affiche uniquement le type
else
type="$mimetype"
fi
;;
*)
# Pour les autres fichiers, le type est affiché
type="$mimetype";;
esac
# Insertion de la ligne du tableau correspondant au fichier en cours de traitement
row "$i" "$kbytes" "$size" "$lastmodif" "$type" >> index.html
done
# Insertion du pied de page, pour terminer la page
footer >> index.html
# Annonce de la fin du traitement
echo "${green}Done${reset} /$webpath"