Backend: Added file reading capabilities.
This commit is contained in:
parent
2f896195d5
commit
9e86258588
5 changed files with 124 additions and 14 deletions
74
backend/core/file.go
Normal file
74
backend/core/file.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
module backend
|
module backend
|
||||||
|
|
||||||
go 1.24.2
|
go 1.24.2
|
||||||
|
|
||||||
|
require github.com/joho/godotenv v1.5.1 // indirect
|
||||||
|
|
2
backend/go.sum
Normal file
2
backend/go.sum
Normal 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=
|
|
@ -1,12 +1,28 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"backend/core"
|
||||||
"backend/server"
|
"backend/server"
|
||||||
"backend/util"
|
"backend/util"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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)
|
util.SetLevel(util.NOLOG)
|
||||||
|
|
||||||
// Rest API start on port
|
// Rest API start on port
|
||||||
|
|
|
@ -11,7 +11,9 @@ type LogLevel int
|
||||||
const (
|
const (
|
||||||
BLUE colorFormat = "\033[34m"
|
BLUE colorFormat = "\033[34m"
|
||||||
GREEN colorFormat = "\033[32m"
|
GREEN colorFormat = "\033[32m"
|
||||||
|
LIGHT_GREEN colorFormat = "\033[92m"
|
||||||
YELLOW colorFormat = "\033[33m"
|
YELLOW colorFormat = "\033[33m"
|
||||||
|
LIGHT_YELLOW colorFormat = "\033[93m"
|
||||||
RED colorFormat = "\033[31m"
|
RED colorFormat = "\033[31m"
|
||||||
LIGHT_RED colorFormat = "\033[91m"
|
LIGHT_RED colorFormat = "\033[91m"
|
||||||
WHITE colorFormat = "\033[97m"
|
WHITE colorFormat = "\033[97m"
|
||||||
|
@ -29,9 +31,9 @@ const (
|
||||||
var colorPrint = map[LogLevel]colorFormat{
|
var colorPrint = map[LogLevel]colorFormat{
|
||||||
NOLOG: "",
|
NOLOG: "",
|
||||||
INFO: BLUE + "[INFO]" + RESET,
|
INFO: BLUE + "[INFO]" + RESET,
|
||||||
CORRECT: GREEN,
|
CORRECT: GREEN + "[OK ]" + LIGHT_GREEN,
|
||||||
WARNING: YELLOW + "[WARN]",
|
WARNING: YELLOW + "[WARN]" + LIGHT_YELLOW,
|
||||||
ERROR: RED + "[ERR.]" + LIGHT_RED,
|
ERROR: RED + "[ERR ]" + LIGHT_RED,
|
||||||
}
|
}
|
||||||
|
|
||||||
var minimumlog = NOLOG
|
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())
|
time := fmt.Sprintf("[%02d/%02d/%d %02d:%02d:%02d]", currentTime.Day(), currentTime.Month(), currentTime.Year(), currentTime.Hour(), currentTime.Minute(), currentTime.Second())
|
||||||
if level == NOLOG {
|
if level == NOLOG {
|
||||||
fmt.Printf("%s\t\t%s", time, message)
|
fmt.Printf("%s\t\t%s", time, message)
|
||||||
} else if level != INFO {
|
|
||||||
fmt.Printf("%s%s\t%s%s", header, time, message, RESET)
|
|
||||||
} else {
|
} 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue