From 64818f0970b4afa614b15dc64fa68817923d34ba Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sun, 1 Jun 2025 12:41:59 +0200 Subject: [PATCH] Backend: Added multiple endpoints to gather pictures: both uncompressed and compressed. --- backend/core/file.go | 21 +++++++++++++----- backend/core/img.go | 45 ++++++++++++++++++++++++-------------- backend/server/pictures.go | 22 ++++++++++++++++++- backend/server/server.go | 4 +++- backend/util/config.go | 32 ++++++++++++++++++++++----- 5 files changed, 94 insertions(+), 30 deletions(-) diff --git a/backend/core/file.go b/backend/core/file.go index 1de15ce..5564764 100644 --- a/backend/core/file.go +++ b/backend/core/file.go @@ -5,14 +5,20 @@ import ( "errors" "io/fs" "os" + "path/filepath" "strings" "time" ) type PictureData struct { - Name string `json:"name"` - Date time.Time `json:"date"` - Album string `json:"album"` + 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 @@ -53,9 +59,12 @@ func isPicture(s string) bool { func getFileInfo(info fs.FileInfo, picd []PictureData, actualdir string) []PictureData { if isPicture(info.Name()) { picd = append(picd, PictureData{ - Name: info.Name(), - Date: info.ModTime(), - Album: actualdir, + 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)) } diff --git a/backend/core/img.go b/backend/core/img.go index ab59a16..ce4cd5f 100644 --- a/backend/core/img.go +++ b/backend/core/img.go @@ -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++ } } diff --git a/backend/server/pictures.go b/backend/server/pictures.go index 37b2b30..9275012 100644 --- a/backend/server/pictures.go +++ b/backend/server/pictures.go @@ -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) + } +} diff --git a/backend/server/server.go b/backend/server/server.go index 2f0a642..b0075dd 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -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 { diff --git a/backend/util/config.go b/backend/util/config.go index f480fad..589586f 100644 --- a/backend/util/config.go +++ b/backend/util/config.go @@ -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) {