Backend: config now asks for more information (root url, port, photopath...). Automatically creating compressed in the same folder. Removed FileServer.
This commit is contained in:
parent
3cf03c9b8c
commit
9698bf2f43
6 changed files with 73 additions and 47 deletions
|
@ -85,7 +85,7 @@ func getFileInfo(path string, info fs.FileInfo, picd []PictureData, actualdir st
|
||||||
Name: info.Name(),
|
Name: info.Name(),
|
||||||
Date: info.ModTime(),
|
Date: info.ModTime(),
|
||||||
Album: actualdir,
|
Album: actualdir,
|
||||||
URL: "http://localhost:8085/photo/" + actualdir + "/" + info.Name(),
|
URL: util.GetURL() + actualdir + "/" + info.Name(),
|
||||||
Title: title,
|
Title: title,
|
||||||
Localisation: pictureinfo.Localisation,
|
Localisation: pictureinfo.Localisation,
|
||||||
Description: pictureinfo.Description,
|
Description: pictureinfo.Description,
|
||||||
|
|
|
@ -13,17 +13,12 @@ import (
|
||||||
"github.com/disintegration/imaging"
|
"github.com/disintegration/imaging"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CompressImage(path string, name string) (string, error) {
|
func CompressImage(path string) (string, error) {
|
||||||
// Gathering base folder
|
name := strings.TrimSuffix(filepath.Base(path), filepath.Ext(filepath.Base(path))) + "_compressed.jpeg"
|
||||||
basedir, err := os.Getwd()
|
basedir := filepath.Join(util.GetConfig().PhotoPath, ".compressed", name)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
name = name + "_compressed.jpeg"
|
|
||||||
basedir = filepath.Join(basedir, ".temp", name)
|
|
||||||
|
|
||||||
// Creating folder
|
// Creating folder
|
||||||
if err := os.MkdirAll(".temp", 0755); err != nil {
|
if err := os.MkdirAll(filepath.Join(util.GetConfig().PhotoPath, ".compressed"), 0755); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,23 +57,23 @@ func CompressImage(path string, name string) (string, error) {
|
||||||
return name, nil
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CompressFiles(basePath string) {
|
func CompressFiles() {
|
||||||
picd := GetPictureData()
|
picd := GetPictureData()
|
||||||
|
cfg := util.GetConfig()
|
||||||
count := 0
|
count := 0
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
for i := range picd {
|
for i := range picd {
|
||||||
path := filepath.Join(basePath, picd[i].Album, picd[i].Name)
|
path := filepath.Join(cfg.PhotoPath, picd[i].Album, picd[i].Name)
|
||||||
name := strings.TrimSuffix(picd[i].Name, filepath.Ext(picd[i].Name))
|
compressed_name, err := CompressImage(path)
|
||||||
compressed_name, err := CompressImage(path, name)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.Compare(err.Error(), "file already created") != 0 {
|
if strings.Compare(err.Error(), "file already created") != 0 {
|
||||||
util.Logformat(util.WARNING, "Failed to compress '%s' : (%v)\n", picd[i].Name, err)
|
util.Logformat(util.WARNING, "Failed to compress '%s' : (%v)\n", picd[i].Name, err)
|
||||||
} else {
|
} else {
|
||||||
picd[i].URLCompressed = "http://localhost:8085/photo_compressed/" + compressed_name
|
picd[i].URLCompressed = util.GetURL() + "/.compressed/" + compressed_name
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
util.Logformatrn(util.INFO, "Compressing %d files...", count)
|
util.Logformatrn(util.INFO, "Compressing %d files...", count)
|
||||||
picd[i].URLCompressed = "http://localhost:8085/photo_compressed/" + compressed_name
|
picd[i].URLCompressed = util.GetURL() + "/.compressed/" + compressed_name
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compress files
|
// Compress files
|
||||||
core.CompressFiles(cfg.PhotoPath)
|
core.CompressFiles()
|
||||||
|
|
||||||
// Rest API start on port
|
// Rest API start on port
|
||||||
server.Start(8085)
|
server.Start(int64(cfg.Port))
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ func getphotolist(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func getphoto(w http.ResponseWriter, r *http.Request) {
|
func getphoto(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" {
|
if r.Method != "GET" {
|
||||||
util.Logformat(util.ERROR, "%s: Only GET is accepted (%s received)", util.GetFunctionName(), r.Method)
|
util.Logformat(util.ERROR, "%s: Only GET is accepted (%s received)", util.GetFunctionName(), r.Method)
|
||||||
|
@ -31,6 +32,7 @@ func getphoto_compressed(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "Only GET method is accepted", http.StatusBadRequest)
|
http.Error(w, "Only GET method is accepted", http.StatusBadRequest)
|
||||||
} else {
|
} else {
|
||||||
util_config := util.GetConfig()
|
util_config := util.GetConfig()
|
||||||
http.StripPrefix("/photo_compressed/", http.FileServer(http.Dir(util_config.PhotoCompressPath))).ServeHTTP(w, r)
|
http.StripPrefix("/photo_compressed/", http.FileServer(http.Dir(util_config.CompressPath))).ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
func Start(port int64) {
|
func Start(port int64) {
|
||||||
// Creating endpoints
|
// Creating endpoints
|
||||||
http.HandleFunc("/photo_list", wrapHeader(getphotolist))
|
http.HandleFunc("/photo_list", wrapHeader(getphotolist))
|
||||||
http.HandleFunc("/photo/", wrapHeader(getphoto))
|
// http.HandleFunc("/photo/", wrapHeader(getphoto))
|
||||||
http.HandleFunc("/photo_compressed/", wrapHeader(getphoto_compressed))
|
// http.HandleFunc("/photo_compressed/", wrapHeader(getphoto_compressed))
|
||||||
|
|
||||||
util.Logformat(util.INFO, "Starting server on port %d...\n", port)
|
util.Logformat(util.INFO, "Starting server on port %d...\n", port)
|
||||||
if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {
|
if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {
|
||||||
|
|
|
@ -5,17 +5,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfigJSON struct {
|
type Config struct {
|
||||||
PhotoPath string `json:"photo_path"`
|
PhotoPath string `json:"photo_path"`
|
||||||
IgnoreFolder []string `json:"ignore_folder"`
|
IgnoreFolder []string `json:"ignore_folder"`
|
||||||
}
|
Port uint64 `json:"port"`
|
||||||
|
RootUrl string `json:"root_url"`
|
||||||
type Config struct {
|
|
||||||
PhotoPath string
|
|
||||||
IgnoreFolder []string
|
|
||||||
PhotoCompressPath string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var main_config Config
|
var main_config Config
|
||||||
|
@ -24,16 +21,15 @@ func GetConfig() Config {
|
||||||
return main_config
|
return main_config
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadConfig(path string) (Config, error) {
|
func GetURL() string {
|
||||||
var cfg ConfigJSON
|
if main_config.RootUrl == "http://localhost" {
|
||||||
|
return "http://localhost" + strconv.FormatUint(main_config.Port, 10)
|
||||||
// Gathering temp folder
|
} else {
|
||||||
basedir, err := os.Getwd()
|
return main_config.RootUrl
|
||||||
if err != nil {
|
|
||||||
return main_config, err
|
|
||||||
}
|
}
|
||||||
main_config.PhotoCompressPath = filepath.Join(basedir, ".temp")
|
}
|
||||||
|
|
||||||
|
func ReadConfig(path string) (Config, error) {
|
||||||
// Gathering photo path
|
// Gathering photo path
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -42,14 +38,12 @@ func ReadConfig(path string) (Config, error) {
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
decoder := json.NewDecoder(file)
|
decoder := json.NewDecoder(file)
|
||||||
decoder.DisallowUnknownFields()
|
decoder.DisallowUnknownFields()
|
||||||
err = decoder.Decode(&cfg)
|
err = decoder.Decode(&main_config)
|
||||||
main_config.PhotoPath = cfg.PhotoPath
|
|
||||||
main_config.IgnoreFolder = cfg.IgnoreFolder
|
|
||||||
|
|
||||||
return main_config, err
|
return main_config, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func createConfigFile(cfg ConfigJSON) {
|
func createConfigFile(cfg Config) {
|
||||||
data, err := json.MarshalIndent(cfg, "", " ")
|
data, err := json.MarshalIndent(cfg, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -62,27 +56,62 @@ func createConfigFile(cfg ConfigJSON) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateConfig() {
|
func CreateConfig() {
|
||||||
var cfg ConfigJSON
|
var cfg Config
|
||||||
println("┌ Please Configure your backend !\n│")
|
println("┌ Please Configure your backend !\n│")
|
||||||
|
|
||||||
// Checking PhotoPath
|
// Checking PhotoPath
|
||||||
var err error = fmt.Errorf("Path")
|
println("├\uf03e Enter photo album path \033[90m(press Enter for './')\033[0m")
|
||||||
println("├\uf15b Enter photo album path \033[90m(press Enter for './')\033[0m")
|
for {
|
||||||
for err != nil {
|
|
||||||
photopath := InputString("│> ")
|
photopath := InputString("│> ")
|
||||||
if photopath == "" {
|
if photopath == "" {
|
||||||
photopath = "./"
|
photopath = "./"
|
||||||
}
|
}
|
||||||
_, err = os.ReadDir(photopath)
|
|
||||||
if err != nil {
|
if _, err := os.ReadDir(photopath); err != nil {
|
||||||
fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m")
|
fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("│\033[32m\uf00c Path correct.\033[0m")
|
fmt.Println("│\033[32m\uf00c Path correct.\033[0m")
|
||||||
cfg.PhotoPath = photopath
|
cfg.PhotoPath = photopath
|
||||||
|
fmt.Println("│\033[36m Creating '.compressed' folder...\033[0m")
|
||||||
|
err := os.MkdirAll(filepath.Join(photopath, ".compressed"), 0755)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m")
|
||||||
|
} else {
|
||||||
|
fmt.Println("│\033[32m\uf00c Folder created/already exists.\033[0m\n│")
|
||||||
|
}
|
||||||
|
cfg.IgnoreFolder = append(cfg.IgnoreFolder, ".compressed")
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check port
|
||||||
|
println("├\uf233 Enter the port number for the server \033[90m(press Enter for '8085')\033[0m")
|
||||||
|
for {
|
||||||
|
port := InputString("│> ")
|
||||||
|
if port == "" {
|
||||||
|
port = "8085"
|
||||||
|
}
|
||||||
|
|
||||||
|
portnum, err := strconv.ParseUint(port, 10, 0)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("│\033[31m\u2a2f " + err.Error() + "\033[0m")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("│\033[32m\uf00c Choosen port: " + port + ".\033[0m\n│")
|
||||||
|
cfg.Port = portnum
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check root URL
|
||||||
|
println("├ Enter the root URL for the server \033[90m(press Enter for 'http://localhost')\033[0m")
|
||||||
|
rootUrl := InputString("│> ")
|
||||||
|
if rootUrl == "" {
|
||||||
|
rootUrl = "http://localhost"
|
||||||
|
}
|
||||||
|
cfg.RootUrl = rootUrl
|
||||||
|
|
||||||
// Ending configuration
|
// Ending configuration
|
||||||
println("│\n└ Configuration finished !")
|
println("└ Configuration finished !")
|
||||||
createConfigFile(cfg)
|
createConfigFile(cfg)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue