Backend: Added 'CompressFiles' function. Optimised 'CompressImage' to check if the file already exists.
This commit is contained in:
parent
fc4326e715
commit
a810fcbd76
4 changed files with 47 additions and 11 deletions
1
backend/.gitignore
vendored
1
backend/.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
config.json
|
config.json
|
||||||
.temp/**
|
.temp/**
|
||||||
|
.vscode/**
|
|
@ -2,30 +2,38 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"backend/util"
|
"backend/util"
|
||||||
|
"errors"
|
||||||
"image"
|
"image"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/disintegration/imaging"
|
"github.com/disintegration/imaging"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CompressImage(path string) {
|
func CompressImage(path string, name string) error {
|
||||||
// Creating folder + output file
|
// Creating folder
|
||||||
if err := os.MkdirAll(filepath.Dir(".temp"), 0755); err != nil {
|
if err := os.MkdirAll(".temp", 0755); err != nil {
|
||||||
util.Logformatrn(util.ERROR, "Failed to create temp directory (%v)", err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
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 {
|
if err != nil {
|
||||||
util.Logformatrn(util.ERROR, "Failed to create file: %v", err)
|
return err
|
||||||
}
|
}
|
||||||
defer outputFile.Close()
|
defer outputFile.Close()
|
||||||
|
|
||||||
// Opening image + Reducing its size and quality
|
// Opening image + Reducing its size and quality
|
||||||
img, err := imaging.Open(path)
|
img, err := imaging.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.Logformatrn(util.ERROR, "Failed to open image: %v", err)
|
return err
|
||||||
}
|
}
|
||||||
width := float64(img.Bounds().Dx())
|
width := float64(img.Bounds().Dx())
|
||||||
height := float64(img.Bounds().Dy())
|
height := float64(img.Bounds().Dy())
|
||||||
|
@ -41,6 +49,30 @@ func CompressImage(path string) {
|
||||||
|
|
||||||
// Encoding to jpeg afterwards
|
// Encoding to jpeg afterwards
|
||||||
if err := jpeg.Encode(outputFile, imgOutput, &jpeg.Options{Quality: 30}); err != nil {
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,9 @@ func main() {
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compress files
|
||||||
|
core.CompressFiles(cfg.PhotoPath)
|
||||||
|
|
||||||
// Rest API start on port
|
// Rest API start on port
|
||||||
server.Start(8085)
|
server.Start(8085)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
PhotoPath string `json:"photopath"`
|
PhotoPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadConfig(path string) (Config, error) {
|
func ReadConfig(path string) (Config, error) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue