57 lines
1.1 KiB
Text
57 lines
1.1 KiB
Text
package html
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"piekarnikgosi/utils"
|
|
)
|
|
|
|
func imageSrcDst(input string) (src, dst string) {
|
|
src = "img/" + input
|
|
hash := sha256.Sum256([]byte(input))
|
|
dst = "www/images/" + hex.EncodeToString(hash[:])[:16] + ".avif"
|
|
return src, dst
|
|
}
|
|
|
|
func processImage(fileName string) string {
|
|
dst, err := utils.CopyIfNotExists(imageSrcDst(fileName))
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return "/" + dst[4:]
|
|
}
|
|
|
|
func thumbnailPath(fullPath string) string {
|
|
ext := filepath.Ext(fullPath)
|
|
return fullPath[:len(fullPath)-len(ext)] + "_thumb.avif"
|
|
}
|
|
|
|
templ AvifAlbumImage(srcPath, alt string) {
|
|
<img src={ processImage(albumImageSrc(srcPath)) }
|
|
data-fullsrc={ processImage(srcPath) }
|
|
alt={ alt }
|
|
loading="lazy" />
|
|
}
|
|
|
|
func albumImageSrc(srcPath string) string {
|
|
thumb := thumbnailPath(srcPath)
|
|
if _, err := os.Stat("img/" + thumb); err == nil {
|
|
return thumb
|
|
}
|
|
return srcPath
|
|
}
|
|
|
|
templ AvifImage(srcPath, alt string, lazy bool) {
|
|
<img src={ processImage(srcPath) } alt={ alt }
|
|
if lazy {
|
|
loading="lazy"
|
|
} else {
|
|
fetchpriority="high"
|
|
}
|
|
/>
|
|
}
|