diff --git a/backend/core/file.go b/backend/core/file.go index 051eb58..1de15ce 100644 --- a/backend/core/file.go +++ b/backend/core/file.go @@ -10,11 +10,13 @@ import ( ) type PictureData struct { - name string - date time.Time - album string + Name string `json:"name"` + Date time.Time `json:"date"` + Album string `json:"album"` } +var picd []PictureData + func listFiles(path string, actualdir string, picd []PictureData) []PictureData { entries, err := os.ReadDir(path) if err != nil { @@ -51,9 +53,9 @@ 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, }) util.Logformatrn(util.INFO, "Reading %d files...", len(picd)) } @@ -61,16 +63,19 @@ func getFileInfo(info fs.FileInfo, picd []PictureData, actualdir string) []Pictu return picd } -func ListFiles(path string) ([]PictureData, error) { - var picd []PictureData +func ListFiles(path string) error { start := time.Now() picd = listFiles(path, "", picd) if len(picd) == 0 { - return nil, errors.New("ListFiles(): Could not gather any file") + return errors.New("ListFiles(): Could not gather any file") } else { elapsed := time.Since(start) print("\n") util.Logformat(util.CORRECT, "Gathered %d files in %dms.\n", len(picd), elapsed.Milliseconds()) - return picd, nil + return nil } } + +func GetPictureData() []PictureData { + return picd +} diff --git a/backend/main.go b/backend/main.go index 0d9947c..d3b8138 100644 --- a/backend/main.go +++ b/backend/main.go @@ -26,7 +26,7 @@ func main() { // Reading files util.Logformat(util.INFO, "Reading folder '%s'\n", cfg.PhotoPath) - _, err = core.ListFiles(cfg.PhotoPath) + err = core.ListFiles(cfg.PhotoPath) if err != nil { util.Logformat(util.ERROR, "%s\n", err.Error()) os.Exit(3) diff --git a/backend/server/pictures.go b/backend/server/pictures.go index 6611ea1..37b2b30 100644 --- a/backend/server/pictures.go +++ b/backend/server/pictures.go @@ -1,16 +1,16 @@ package server import ( + "backend/core" "backend/util" "net/http" ) func getphoto(w http.ResponseWriter, r *http.Request) { - idStr := r.URL.Query().Get("id") - if idStr == "" { - util.Logformat(util.ERROR, "%s: Missing 'id' parameter.", util.GetFunctionName()) - http.Error(w, "Missing or invalid parameters : id", http.StatusBadRequest) + 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 { - sendResponse(w, 0) + sendResponse(w, core.GetPictureData()) } } diff --git a/backend/server/server.go b/backend/server/server.go index 9613c74..2f0a642 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -9,7 +9,7 @@ import ( func Start(port int64) { // Creating endpoints - http.HandleFunc("/getphoto", wrapHeader(getphoto)) + http.HandleFunc("/photo", wrapHeader(getphoto)) util.Logformat(util.INFO, "Starting server on port %d...\n", port) if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {