diff --git a/backend/.gitignore b/backend/.gitignore index 31ad7ca..ffbe678 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,2 +1,3 @@ config.json -.temp/** \ No newline at end of file +.temp/** +.vscode/** \ No newline at end of file diff --git a/backend/core/img.go b/backend/core/img.go index d4ccd47..ab59a16 100644 --- a/backend/core/img.go +++ b/backend/core/img.go @@ -2,30 +2,38 @@ package core import ( "backend/util" + "errors" "image" "image/jpeg" "os" "path/filepath" + "strings" + "time" "github.com/disintegration/imaging" ) -func CompressImage(path string) { - // Creating folder + output file - if err := os.MkdirAll(filepath.Dir(".temp"), 0755); err != nil { - util.Logformatrn(util.ERROR, "Failed to create temp directory (%v)", err) - return +func CompressImage(path string, name string) error { + // Creating folder + if err := os.MkdirAll(".temp", 0755); err != nil { + return err } - outputFile, err := os.Create(".temp/test.jpeg") + + // Checking output file + if _, err := os.Stat(".temp/" + name + "_compressed.jpeg"); err == nil { + return errors.New("file already created") + } + // Create file + outputFile, err := os.Create(".temp/" + name + "_compressed.jpeg") if err != nil { - util.Logformatrn(util.ERROR, "Failed to create file: %v", err) + return err } defer outputFile.Close() // Opening image + Reducing its size and quality img, err := imaging.Open(path) if err != nil { - util.Logformatrn(util.ERROR, "Failed to open image: %v", err) + return err } width := float64(img.Bounds().Dx()) height := float64(img.Bounds().Dy()) @@ -41,6 +49,30 @@ func CompressImage(path string) { // Encoding to jpeg afterwards if err := jpeg.Encode(outputFile, imgOutput, &jpeg.Options{Quality: 30}); err != nil { - util.Logformatrn(util.ERROR, "Failed to save image: %v", err) + return err + } + return 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) + 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) + } + } else { + util.Logformatrn(util.INFO, "Compressing %d files...", count) + count++ + } + } + if count > 0 { + elapsed := time.Since(start) + util.Logformatrn(util.CORRECT, "Compressed %d files in %dms.\n", count, elapsed.Milliseconds()) } } diff --git a/backend/main.go b/backend/main.go index d3b8138..6180f14 100644 --- a/backend/main.go +++ b/backend/main.go @@ -32,6 +32,9 @@ func main() { os.Exit(3) } + // Compress files + core.CompressFiles(cfg.PhotoPath) + // Rest API start on port server.Start(8085) } diff --git a/backend/util/config.go b/backend/util/config.go index 1e50275..f480fad 100644 --- a/backend/util/config.go +++ b/backend/util/config.go @@ -7,7 +7,7 @@ import ( ) type Config struct { - PhotoPath string `json:"photopath"` + PhotoPath string } func ReadConfig(path string) (Config, error) {