Backend: Added file reading capabilities.

This commit is contained in:
Yohan Boujon 2025-05-02 12:22:26 +02:00
parent 2f896195d5
commit 9e86258588
5 changed files with 124 additions and 14 deletions

74
backend/core/file.go Normal file
View file

@ -0,0 +1,74 @@
package core
import (
"backend/util"
"io/fs"
"os"
"strings"
"time"
)
type PictureData struct {
name string
date time.Time
album string
}
func listFiles(path string, actualdir string, picd []PictureData) []PictureData {
entries, err := os.ReadDir(path)
if err != nil {
util.Logformat(util.WARNING, "Could not read files in '%s'", path)
return picd
}
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
continue
}
if info.IsDir() {
// fmt.Printf("\uf07b %s\n", entry.Name())
picd = listFiles(path+"/"+info.Name(), info.Name(), picd)
} else {
// fmt.Printf("\uf15b %s: %do\n", entry.Name(), info.Size())
picd = getFileInfo(info, picd, actualdir)
}
}
return picd
}
func isPicture(s string) bool {
extensions := []string{".jpg", ".jpeg", ".jfif", ".pjpeg", ".pjp", ".webp", ".png", ".apng", ".svg", ".avif"}
for _, ext := range extensions {
if strings.HasSuffix(s, ext) {
return true
}
}
return false
}
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,
})
util.Logformatrn(util.INFO, "Reading %d files...", len(picd))
}
return picd
}
func ListFiles(path string) []PictureData {
var picd []PictureData
picd = listFiles(path, "", picd)
println("")
if len(picd) == 0 {
util.Logformat(util.ERROR, "Could not gather any file.\n")
return nil
} else {
util.Logformat(util.CORRECT, "Gathered %d files.\n", len(picd))
return picd
}
}

View file

@ -1,3 +1,5 @@
module backend
go 1.24.2
require github.com/joho/godotenv v1.5.1 // indirect

2
backend/go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

View file

@ -1,12 +1,28 @@
package main
import (
"backend/core"
"backend/server"
"backend/util"
"os"
"github.com/joho/godotenv"
)
func main() {
// Checking arguments
err := godotenv.Load(".env")
if err != nil {
panic("Error loading .env file")
}
// Reading files
util.Logformat(util.INFO, "Reading to folder '%s' (Recursively)...\n", os.Getenv("PHOTO_PATH"))
picd := core.ListFiles(os.Getenv("PHOTO_PATH"))
if picd == nil {
panic("Could not gather any file.")
}
// Set Log Level
util.SetLevel(util.NOLOG)
// Rest API start on port

View file

@ -11,7 +11,9 @@ type LogLevel int
const (
BLUE colorFormat = "\033[34m"
GREEN colorFormat = "\033[32m"
LIGHT_GREEN colorFormat = "\033[92m"
YELLOW colorFormat = "\033[33m"
LIGHT_YELLOW colorFormat = "\033[93m"
RED colorFormat = "\033[31m"
LIGHT_RED colorFormat = "\033[91m"
WHITE colorFormat = "\033[97m"
@ -29,9 +31,9 @@ const (
var colorPrint = map[LogLevel]colorFormat{
NOLOG: "",
INFO: BLUE + "[INFO]" + RESET,
CORRECT: GREEN,
WARNING: YELLOW + "[WARN]",
ERROR: RED + "[ERR.]" + LIGHT_RED,
CORRECT: GREEN + "[OK ]" + LIGHT_GREEN,
WARNING: YELLOW + "[WARN]" + LIGHT_YELLOW,
ERROR: RED + "[ERR ]" + LIGHT_RED,
}
var minimumlog = NOLOG
@ -47,10 +49,24 @@ func Logformat(level LogLevel, format string, args ...interface{}) {
time := fmt.Sprintf("[%02d/%02d/%d %02d:%02d:%02d]", currentTime.Day(), currentTime.Month(), currentTime.Year(), currentTime.Hour(), currentTime.Minute(), currentTime.Second())
if level == NOLOG {
fmt.Printf("%s\t\t%s", time, message)
} else if level != INFO {
fmt.Printf("%s%s\t%s%s", header, time, message, RESET)
} else {
fmt.Printf("%s%s\t%s", header, time, message)
fmt.Printf("%s%s\t%s%s", header, time, message, RESET)
}
}
func Logformatrn(level LogLevel, format string, args ...interface{}) {
if level < minimumlog {
return
}
header := colorPrint[level]
message := fmt.Sprintf(format, args...)
currentTime := time.Now()
time := fmt.Sprintf("[%02d/%02d/%d %02d:%02d:%02d]", currentTime.Day(), currentTime.Month(), currentTime.Year(), currentTime.Hour(), currentTime.Minute(), currentTime.Second())
if level == NOLOG {
fmt.Printf("\r%s\t\t%s", time, message)
} else {
fmt.Printf("\r%s%s\t%s%s", header, time, message, RESET)
}
}