Backend: Added multiple endpoints to gather pictures: both uncompressed and compressed.

This commit is contained in:
Yohan Boujon 2025-06-01 12:41:59 +02:00
parent a810fcbd76
commit 64818f0970
5 changed files with 94 additions and 30 deletions

View file

@ -5,6 +5,7 @@ import (
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
)
@ -13,6 +14,11 @@ type PictureData struct {
Name string `json:"name"`
Date time.Time `json:"date"`
Album string `json:"album"`
URL string `json:"url"`
URLCompressed string `json:"urlcompressed"`
Title string `json:"title"`
Localisation string `json:"localisation"`
Description string `json:"description"`
}
var picd []PictureData
@ -56,6 +62,9 @@ func getFileInfo(info fs.FileInfo, picd []PictureData, actualdir string) []Pictu
Name: info.Name(),
Date: info.ModTime(),
Album: actualdir,
URL: "http://localhost:8085/photo/" + actualdir + "/" + info.Name(),
Title: strings.TrimSuffix(info.Name(), filepath.Ext(info.Name())),
Localisation: "Paris, France",
})
util.Logformatrn(util.INFO, "Reading %d files...", len(picd))
}

View file

@ -13,27 +13,35 @@ import (
"github.com/disintegration/imaging"
)
func CompressImage(path string, name string) error {
func CompressImage(path string, name string) (string, error) {
// Gathering base folder
basedir, err := os.Getwd()
if err != nil {
return "", err
}
name = name + "_compressed.jpeg"
basedir = filepath.Join(basedir, ".temp", name)
// Creating folder
if err := os.MkdirAll(".temp", 0755); err != nil {
return err
return "", err
}
// Checking output file
if _, err := os.Stat(".temp/" + name + "_compressed.jpeg"); err == nil {
return errors.New("file already created")
if _, err := os.Stat(basedir); err == nil {
return name, errors.New("file already created")
}
// Create file
outputFile, err := os.Create(".temp/" + name + "_compressed.jpeg")
outputFile, err := os.Create(basedir)
if err != nil {
return err
return "", err
}
defer outputFile.Close()
// Opening image + Reducing its size and quality
img, err := imaging.Open(path)
if err != nil {
return err
return "", err
}
width := float64(img.Bounds().Dx())
height := float64(img.Bounds().Dy())
@ -41,33 +49,36 @@ func CompressImage(path string, name string) error {
var imgOutput *image.NRGBA
if width < height {
ratio = height / width
imgOutput = imaging.Resize(img, 283, int(283*ratio), imaging.Lanczos)
imgOutput = imaging.Resize(img, 320, int(320*ratio), imaging.Lanczos)
} else {
ratio = width / height
imgOutput = imaging.Resize(img, int(283*ratio), 283, imaging.Lanczos)
imgOutput = imaging.Resize(img, int(320*ratio), 320, imaging.Lanczos)
}
// Encoding to jpeg afterwards
if err := jpeg.Encode(outputFile, imgOutput, &jpeg.Options{Quality: 30}); err != nil {
return err
if err := jpeg.Encode(outputFile, imgOutput, &jpeg.Options{Quality: 100}); err != nil {
return "", err
}
return nil
return name, nil
}
func CompressFiles(basePath string) {
picd := GetPictureData()
count := 0
start := time.Now()
for _, p := range picd {
path := basePath + "/" + p.Album + "/" + p.Name
name := strings.TrimSuffix(p.Name, filepath.Ext(p.Name))
err := CompressImage(path, name)
for i := range picd {
path := filepath.Join(basePath, picd[i].Album, picd[i].Name)
name := strings.TrimSuffix(picd[i].Name, filepath.Ext(picd[i].Name))
compressed_name, err := CompressImage(path, name)
if err != nil {
if strings.Compare(err.Error(), "file already created") != 0 {
util.Logformat(util.WARNING, "Failed to compress '%s' : (%v)\n", p.Name, err)
util.Logformat(util.WARNING, "Failed to compress '%s' : (%v)\n", picd[i].Name, err)
} else {
picd[i].URLCompressed = "http://localhost:8085/photo_compressed/" + compressed_name
}
} else {
util.Logformatrn(util.INFO, "Compressing %d files...", count)
picd[i].URLCompressed = "http://localhost:8085/photo_compressed/" + compressed_name
count++
}
}

View file

@ -6,7 +6,7 @@ import (
"net/http"
)
func getphoto(w http.ResponseWriter, r *http.Request) {
func getphotolist(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
util.Logformat(util.ERROR, "%s: Only GET is accepted (%s received)", util.GetFunctionName(), r.Method)
http.Error(w, "Only GET method is accepted", http.StatusBadRequest)
@ -14,3 +14,23 @@ func getphoto(w http.ResponseWriter, r *http.Request) {
sendResponse(w, core.GetPictureData())
}
}
func getphoto(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
util.Logformat(util.ERROR, "%s: Only GET is accepted (%s received)", util.GetFunctionName(), r.Method)
http.Error(w, "Only GET method is accepted", http.StatusBadRequest)
} else {
util_config := util.GetConfig()
http.StripPrefix("/photo/", http.FileServer(http.Dir(util_config.PhotoPath))).ServeHTTP(w, r)
}
}
func getphoto_compressed(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
util.Logformat(util.ERROR, "%s: Only GET is accepted (%s received)", util.GetFunctionName(), r.Method)
http.Error(w, "Only GET method is accepted", http.StatusBadRequest)
} else {
util_config := util.GetConfig()
http.StripPrefix("/photo_compressed/", http.FileServer(http.Dir(util_config.PhotoCompressPath))).ServeHTTP(w, r)
}
}

View file

@ -9,7 +9,9 @@ import (
func Start(port int64) {
// Creating endpoints
http.HandleFunc("/photo", wrapHeader(getphoto))
http.HandleFunc("/photo_list", wrapHeader(getphotolist))
http.HandleFunc("/photo/", wrapHeader(getphoto))
http.HandleFunc("/photo_compressed/", wrapHeader(getphoto_compressed))
util.Logformat(util.INFO, "Starting server on port %d...\n", port)
if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {

View file

@ -4,24 +4,46 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
type Config struct {
type ConfigJSON struct {
PhotoPath string
}
type Config struct {
PhotoPath string
PhotoCompressPath string
}
var main_config Config
func GetConfig() Config {
return main_config
}
func ReadConfig(path string) (Config, error) {
var cfg Config
var cfg ConfigJSON
// Gathering temp folder
basedir, err := os.Getwd()
if err != nil {
return main_config, err
}
main_config.PhotoCompressPath = filepath.Join(basedir, ".temp")
// Gathering photo path
file, err := os.Open(path)
if err != nil {
return cfg, err
return main_config, err
}
defer file.Close()
decoder := json.NewDecoder(file)
decoder.DisallowUnknownFields()
err = decoder.Decode(&cfg)
return cfg, err
main_config.PhotoPath = cfg.PhotoPath
return main_config, err
}
func createConfigFile(cfg Config) {