first commit eheh

main
poni 12 months ago
commit 9373c99805

@ -0,0 +1,196 @@
#!/bin/bash
domain="http://localhost:5500" # Replace with your actual domain
main_folder="test_directory_not_elaborated"
# Function to compress and generate HTML links
compress_and_generate_links() {
local folder="$1"
local compressed_folder="$folder/compressed"
mkdir -p "$compressed_folder"
declare -a text_files
declare -a picture_files
declare -a video_files
declare -a audio_files
declare -a other_files
for file in "$folder"/*; do
if [ -f "$file" ]; then
case "$file" in
*.txt|*.md|*.sh|*.html)
text_files+=("$file")
;;
*.jpeg | *.jpg|*.png|*.gif|*.heic)
picture_files+=("$file")
;;
*.mp4|*.mov|*.mkv|*.avi)
video_files+=("$file")
;;
*.mp3|*.wav|*.flac|*.aiff)
audio_files+=("$file")
;;
*index.html) # Exclude index.html files TODO spiegalo poi
continue
;;
*)
other_files+=("$file")
;;
esac
fi
done
# Create index.html file
index_file="$folder/index.html"
echo "<html>
<head>
<title>RADICAL LS! -> $folder</title>
<style>
body {
width: 100%;
height: 100%;
}
.contents{
display: flex;
}
.contents div{
display: flex;
flex-direction: column;
align-items: center;
}
img, video {
width: 50%;
}
</style>
</head>
<body>" > "$index_file"
# Add link to parent folder at the top
parent_folder="$(dirname "$folder")"
echo "<p><a class="arrow_back" href=\"$domain/$parent_folder\">⬳</a></p>" >> "$index_file"
# Add links to subfolders with their names displayed
subfolders=()
for subfolder in "$folder"/*; do
if [ -d "$subfolder" ] && [ ! "$(basename "$subfolder")" == "compressed" ]; then
subfolder_name=$(basename "$subfolder")
subfolders+=("<p><a href=\"$domain/$subfolder\"><span class="arrow_forward">⟿</span> $subfolder_name</a></p>")
fi
done
if [ ${#subfolders[@]} -gt 0 ]; then
echo "" >> "$index_file"
for subfolder_link in "${subfolders[@]}"; do
echo "$subfolder_link" >> "$index_file"
done
fi
echo "<h1>$folder</h1><div class='contents'>" >> "$index_file"
# Display text files in the subfolder
if [ ${#text_files[@]} -gt 0 ]; then
echo "<div class='text'>" >> "$index_file"
for text_file in "${text_files[@]}"; do
echo "<div><a href=\"$domain/$text_file\" target=\"_blank\">$text_file</a><br><p></p></div>" >> "$index_file"
done
echo "</div>" >> "$index_file"
fi
# Compress and display picture files in the subfolder
if [ ${#picture_files[@]} -gt 0 ]; then
echo "<div class='pics'>" >> "$index_file"
for picture_file in "${picture_files[@]}"; do
compressed_file="$compressed_folder/$(basename "$picture_file")_compressed.jpg"
if [ ! -f "$compressed_file" ]; then
convert "$picture_file" -resize 30% -dither Riemersma -colors 3 "$compressed_file" #TODO
fi
echo "<div>
<img loading=\"lazy\" src=\"$domain/$compressed_file\" alt=\"Compressed Image\">
<a href=\"$domain/$picture_file\" target=\"_blank\">Original ⇝ $picture_file</a>
</div>" >> "$index_file" #TODO: fix here the not-compressed path
done
echo "</div>" >> "$index_file"
fi
# Compress and display video files in the subfolder
if [ ${#video_files[@]} -gt 0 ]; then
echo "<div class='vids'>" >> "$index_file"
for video_file in "${video_files[@]}"; do
compressed_file="$compressed_folder/$(basename "$video_file")_compressed.mp4"
if [ ! -f "$compressed_file" ]; then
ffmpeg -i "$video_file" -vf "scale=640:480" -c:v libx264 -crf 23 -c:a aac -strict experimental "$compressed_file" -y
fi
echo "<div>
<video controls>
<source src=\"$domain/$compressed_file\" type=\"video/mp4\">
</video>
<a href=\"$domain/$video_file\" target=\"_blank\">Original ⇝ $video_file</a>
</div>" >> "$index_file"
done
echo "</div>" >> "$index_file"
fi
# Compress and display audio files in the subfolder
if [ ${#audio_files[@]} -gt 0 ]; then
echo "<div class="audios">" >> "$index_file"
for audio_file in "${audio_files[@]}"; do
compressed_file="$compressed_folder/$(basename "$audio_file")_compressed.mp3"
if [ ! -f "$compressed_file" ]; then
ffmpeg -i "$audio_file" -b:a 96k "$compressed_file" -y
fi
echo "<div>
<audio controls>
<source src=\"$domain/$compressed_file\" type=\"audio/mpeg\">
</audio>
<a href=\"$domain/$audio_file\" target=\"_blank\">Original ⇝ $audio_file</a>
</div>" >> "$index_file"
done
echo "</div>" >> "$index_file"
fi
# Display other files in the subfolder
if [ ${#other_files[@]} -gt 0 ]; then
echo "<h2>Other Files:</h2><ul>" >> "$index_file"
for other_file in "${other_files[@]}"; do
echo "<li><a href=\"$domain/$other_file\" target=\"_blank\">Original ⇝ $other_file</a></li>" >> "$index_file"
done
echo "</ul>" >> "$index_file"
fi
# Close HTML tags
echo "</div></body></html>" >> "$index_file"
}
# Function to iterate through all subfolders excluding "compressed"
function iterate_subfolders() {
local folder="$1"
# Loop through each item in the folder
for item in "$folder"/*; do
# Check if the item is a directory
if [ -d "$item" ] && [ ! "$(basename "$item")" == "compressed" ] && [ ! "$(basename "$item")" == "*" ]; then
compress_and_generate_links "$item"
iterate_subfolders "$item"
echo "$item"
fi
done
}
# Start iterating from the main folder
iterate_subfolders $main_folder
echo "HTML files created for each subfolder."
# stuff:
# imagemagick got different implementations called "delegates":
# "Delegates (built-in): bzlib fontconfig freetype gslib heic jng jp2 jpeg jxl lcms lqr ltdl lzma openexr png ps raw tiff webp xml zlib"
# you can configure it through a file called delegates.xml
Loading…
Cancel
Save