From 9698bf2f43c1dc3451dc898cb63d2acd806ab291 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sun, 1 Jun 2025 22:53:14 +0200 Subject: [PATCH] Backend: config now asks for more information (root url, port, photopath...). Automatically creating compressed in the same folder. Removed FileServer. --- backend/core/file.go | 2 +- backend/core/img.go | 25 +++++------- backend/main.go | 4 +- backend/server/pictures.go | 4 +- backend/server/server.go | 4 +- backend/util/config.go | 81 ++++++++++++++++++++++++++------------ 6 files changed, 73 insertions(+), 47 deletions(-) diff --git a/backend/core/file.go b/backend/core/file.go index 201788f..c3b5538 100644 --- a/backend/core/file.go +++ b/backend/core/file.go @@ -85,7 +85,7 @@ func getFileInfo(path string, info fs.FileInfo, picd []PictureData, actualdir st Name: info.Name(), Date: info.ModTime(), Album: actualdir, - URL: "http://localhost:8085/photo/" + actualdir + "/" + info.Name(), + URL: util.GetURL() + actualdir + "/" + info.Name(), Title: title, Localisation: pictureinfo.Localisation, Description: pictureinfo.Description, diff --git a/backend/core/img.go b/backend/core/img.go index ce4cd5f..660661b 100644 --- a/backend/core/img.go +++ b/backend/core/img.go @@ -13,17 +13,12 @@ import ( "github.com/disintegration/imaging" ) -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) +func CompressImage(path string) (string, error) { + name := strings.TrimSuffix(filepath.Base(path), filepath.Ext(filepath.Base(path))) + "_compressed.jpeg" + basedir := filepath.Join(util.GetConfig().PhotoPath, ".compressed", name) // Creating folder - if err := os.MkdirAll(".temp", 0755); err != nil { + if err := os.MkdirAll(filepath.Join(util.GetConfig().PhotoPath, ".compressed"), 0755); err != nil { return "", err } @@ -62,23 +57,23 @@ func CompressImage(path string, name string) (string, error) { return name, nil } -func CompressFiles(basePath string) { +func CompressFiles() { picd := GetPictureData() + cfg := util.GetConfig() count := 0 start := time.Now() 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) + path := filepath.Join(cfg.PhotoPath, picd[i].Album, picd[i].Name) + compressed_name, err := CompressImage(path) if err != nil { if strings.Compare(err.Error(), "file already created") != 0 { 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 + picd[i].URLCompressed = util.GetURL() + "/.compressed/" + compressed_name } } else { util.Logformatrn(util.INFO, "Compressing %d files...", count) - picd[i].URLCompressed = "http://localhost:8085/photo_compressed/" + compressed_name + picd[i].URLCompressed = util.GetURL() + "/.compressed/" + compressed_name count++ } } diff --git a/backend/main.go b/backend/main.go index 6180f14..85ab9c7 100644 --- a/backend/main.go +++ b/backend/main.go @@ -33,8 +33,8 @@ func main() { } // Compress files - core.CompressFiles(cfg.PhotoPath) + core.CompressFiles() // Rest API start on port - server.Start(8085) + server.Start(int64(cfg.Port)) } diff --git a/backend/server/pictures.go b/backend/server/pictures.go index 9275012..94ed5e4 100644 --- a/backend/server/pictures.go +++ b/backend/server/pictures.go @@ -15,6 +15,7 @@ func getphotolist(w http.ResponseWriter, r *http.Request) { } } +/* 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) @@ -31,6 +32,7 @@ func getphoto_compressed(w http.ResponseWriter, r *http.Request) { 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) + http.StripPrefix("/photo_compressed/", http.FileServer(http.Dir(util_config.CompressPath))).ServeHTTP(w, r) } } +*/ diff --git a/backend/server/server.go b/backend/server/server.go index b0075dd..dcc3f80 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -10,8 +10,8 @@ import ( func Start(port int64) { // Creating endpoints http.HandleFunc("/photo_list", wrapHeader(getphotolist)) - http.HandleFunc("/photo/", wrapHeader(getphoto)) - http.HandleFunc("/photo_compressed/", wrapHeader(getphoto_compressed)) + // 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 b02434a..12156ca 100644 --- a/backend/util/config.go +++ b/backend/util/config.go @@ -5,17 +5,14 @@ import ( "fmt" "os" "path/filepath" + "strconv" ) -type ConfigJSON struct { +type Config struct { PhotoPath string `json:"photo_path"` IgnoreFolder []string `json:"ignore_folder"` -} - -type Config struct { - PhotoPath string - IgnoreFolder []string - PhotoCompressPath string + Port uint64 `json:"port"` + RootUrl string `json:"root_url"` } var main_config Config @@ -24,16 +21,15 @@ func GetConfig() Config { return main_config } -func ReadConfig(path string) (Config, error) { - var cfg ConfigJSON - - // Gathering temp folder - basedir, err := os.Getwd() - if err != nil { - return main_config, err +func GetURL() string { + if main_config.RootUrl == "http://localhost" { + return "http://localhost" + strconv.FormatUint(main_config.Port, 10) + } else { + return main_config.RootUrl } - main_config.PhotoCompressPath = filepath.Join(basedir, ".temp") +} +func ReadConfig(path string) (Config, error) { // Gathering photo path file, err := os.Open(path) if err != nil { @@ -42,14 +38,12 @@ func ReadConfig(path string) (Config, error) { defer file.Close() decoder := json.NewDecoder(file) decoder.DisallowUnknownFields() - err = decoder.Decode(&cfg) - main_config.PhotoPath = cfg.PhotoPath - main_config.IgnoreFolder = cfg.IgnoreFolder + err = decoder.Decode(&main_config) return main_config, err } -func createConfigFile(cfg ConfigJSON) { +func createConfigFile(cfg Config) { data, err := json.MarshalIndent(cfg, "", " ") if err != nil { panic(err) @@ -62,27 +56,62 @@ func createConfigFile(cfg ConfigJSON) { } func CreateConfig() { - var cfg ConfigJSON + var cfg Config println("┌ Please Configure your backend !\n│") // Checking PhotoPath - var err error = fmt.Errorf("Path") - println("├\uf15b Enter photo album path \033[90m(press Enter for './')\033[0m") - for err != nil { + println("├\uf03e Enter photo album path \033[90m(press Enter for './')\033[0m") + for { photopath := InputString("│> ") if photopath == "" { photopath = "./" } - _, err = os.ReadDir(photopath) - if err != nil { + + if _, err := os.ReadDir(photopath); err != nil { fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m") } else { fmt.Println("│\033[32m\uf00c Path correct.\033[0m") cfg.PhotoPath = photopath + fmt.Println("│\033[36m󰑓 Creating '.compressed' folder...\033[0m") + err := os.MkdirAll(filepath.Join(photopath, ".compressed"), 0755) + if err != nil { + fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m") + } else { + fmt.Println("│\033[32m\uf00c Folder created/already exists.\033[0m\n│") + } + cfg.IgnoreFolder = append(cfg.IgnoreFolder, ".compressed") + break } } + // Check port + println("├\uf233 Enter the port number for the server \033[90m(press Enter for '8085')\033[0m") + for { + port := InputString("│> ") + if port == "" { + port = "8085" + } + + portnum, err := strconv.ParseUint(port, 10, 0) + if err != nil { + fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m") + continue + } + + fmt.Println("│\033[32m\uf00c Choosen port: " + port + ".\033[0m\n│") + cfg.Port = portnum + break + } + + // Check root URL + println("├󰇧 Enter the root URL for the server \033[90m(press Enter for 'http://localhost')\033[0m") + rootUrl := InputString("│> ") + if rootUrl == "" { + rootUrl = "http://localhost" + } + cfg.RootUrl = rootUrl + // Ending configuration - println("│\n└ Configuration finished !") + println("└ Configuration finished !") createConfigFile(cfg) }