Compare commits
2 commits
a810fcbd76
...
7549aa7bea
Author | SHA1 | Date | |
---|---|---|---|
7549aa7bea | |||
64818f0970 |
7 changed files with 128 additions and 31 deletions
|
@ -5,14 +5,20 @@ import (
|
|||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PictureData struct {
|
||||
Name string `json:"name"`
|
||||
Date time.Time `json:"date"`
|
||||
Album string `json:"album"`
|
||||
Name string `json:"name"`
|
||||
Date time.Time `json:"date"`
|
||||
Album string `json:"album"`
|
||||
URL string `json:"url"`
|
||||
URLCompressed string `json:"urlcompressed"`
|
||||
Title string `json:"title"`
|
||||
Localisation string `json:"localisation"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
var picd []PictureData
|
||||
|
@ -53,9 +59,12 @@ func isPicture(s string) bool {
|
|||
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,
|
||||
Name: info.Name(),
|
||||
Date: info.ModTime(),
|
||||
Album: actualdir,
|
||||
URL: "http://localhost:8085/photo/" + actualdir + "/" + info.Name(),
|
||||
Title: strings.TrimSuffix(info.Name(), filepath.Ext(info.Name())),
|
||||
Localisation: "Paris, France",
|
||||
})
|
||||
util.Logformatrn(util.INFO, "Reading %d files...", len(picd))
|
||||
}
|
||||
|
|
|
@ -13,27 +13,35 @@ import (
|
|||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
func CompressImage(path string, name string) error {
|
||||
func CompressImage(path string, name string) (string, error) {
|
||||
// Gathering base folder
|
||||
basedir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
name = name + "_compressed.jpeg"
|
||||
basedir = filepath.Join(basedir, ".temp", name)
|
||||
|
||||
// Creating folder
|
||||
if err := os.MkdirAll(".temp", 0755); err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Checking output file
|
||||
if _, err := os.Stat(".temp/" + name + "_compressed.jpeg"); err == nil {
|
||||
return errors.New("file already created")
|
||||
if _, err := os.Stat(basedir); err == nil {
|
||||
return name, errors.New("file already created")
|
||||
}
|
||||
// Create file
|
||||
outputFile, err := os.Create(".temp/" + name + "_compressed.jpeg")
|
||||
outputFile, err := os.Create(basedir)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
defer outputFile.Close()
|
||||
|
||||
// Opening image + Reducing its size and quality
|
||||
img, err := imaging.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
width := float64(img.Bounds().Dx())
|
||||
height := float64(img.Bounds().Dy())
|
||||
|
@ -41,33 +49,36 @@ func CompressImage(path string, name string) error {
|
|||
var imgOutput *image.NRGBA
|
||||
if width < height {
|
||||
ratio = height / width
|
||||
imgOutput = imaging.Resize(img, 283, int(283*ratio), imaging.Lanczos)
|
||||
imgOutput = imaging.Resize(img, 320, int(320*ratio), imaging.Lanczos)
|
||||
} else {
|
||||
ratio = width / height
|
||||
imgOutput = imaging.Resize(img, int(283*ratio), 283, imaging.Lanczos)
|
||||
imgOutput = imaging.Resize(img, int(320*ratio), 320, imaging.Lanczos)
|
||||
}
|
||||
|
||||
// Encoding to jpeg afterwards
|
||||
if err := jpeg.Encode(outputFile, imgOutput, &jpeg.Options{Quality: 30}); err != nil {
|
||||
return err
|
||||
if err := jpeg.Encode(outputFile, imgOutput, &jpeg.Options{Quality: 100}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return nil
|
||||
return name, 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)
|
||||
for i := range picd {
|
||||
path := filepath.Join(basePath, picd[i].Album, picd[i].Name)
|
||||
name := strings.TrimSuffix(picd[i].Name, filepath.Ext(picd[i].Name))
|
||||
compressed_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)
|
||||
util.Logformat(util.WARNING, "Failed to compress '%s' : (%v)\n", picd[i].Name, err)
|
||||
} else {
|
||||
picd[i].URLCompressed = "http://localhost:8085/photo_compressed/" + compressed_name
|
||||
}
|
||||
} else {
|
||||
util.Logformatrn(util.INFO, "Compressing %d files...", count)
|
||||
picd[i].URLCompressed = "http://localhost:8085/photo_compressed/" + compressed_name
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
func getphoto(w http.ResponseWriter, r *http.Request) {
|
||||
func getphotolist(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
util.Logformat(util.ERROR, "%s: Only GET is accepted (%s received)", util.GetFunctionName(), r.Method)
|
||||
http.Error(w, "Only GET method is accepted", http.StatusBadRequest)
|
||||
|
@ -14,3 +14,23 @@ func getphoto(w http.ResponseWriter, r *http.Request) {
|
|||
sendResponse(w, core.GetPictureData())
|
||||
}
|
||||
}
|
||||
|
||||
func getphoto(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
util.Logformat(util.ERROR, "%s: Only GET is accepted (%s received)", util.GetFunctionName(), r.Method)
|
||||
http.Error(w, "Only GET method is accepted", http.StatusBadRequest)
|
||||
} else {
|
||||
util_config := util.GetConfig()
|
||||
http.StripPrefix("/photo/", http.FileServer(http.Dir(util_config.PhotoPath))).ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func getphoto_compressed(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
util.Logformat(util.ERROR, "%s: Only GET is accepted (%s received)", util.GetFunctionName(), r.Method)
|
||||
http.Error(w, "Only GET method is accepted", http.StatusBadRequest)
|
||||
} else {
|
||||
util_config := util.GetConfig()
|
||||
http.StripPrefix("/photo_compressed/", http.FileServer(http.Dir(util_config.PhotoCompressPath))).ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,9 @@ import (
|
|||
|
||||
func Start(port int64) {
|
||||
// Creating endpoints
|
||||
http.HandleFunc("/photo", wrapHeader(getphoto))
|
||||
http.HandleFunc("/photo_list", wrapHeader(getphotolist))
|
||||
http.HandleFunc("/photo/", wrapHeader(getphoto))
|
||||
http.HandleFunc("/photo_compressed/", wrapHeader(getphoto_compressed))
|
||||
|
||||
util.Logformat(util.INFO, "Starting server on port %d...\n", port)
|
||||
if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {
|
||||
|
|
|
@ -4,24 +4,46 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
type ConfigJSON struct {
|
||||
PhotoPath string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
PhotoPath string
|
||||
PhotoCompressPath string
|
||||
}
|
||||
|
||||
var main_config Config
|
||||
|
||||
func GetConfig() Config {
|
||||
return main_config
|
||||
}
|
||||
|
||||
func ReadConfig(path string) (Config, error) {
|
||||
var cfg Config
|
||||
var cfg ConfigJSON
|
||||
|
||||
// Gathering temp folder
|
||||
basedir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return main_config, err
|
||||
}
|
||||
main_config.PhotoCompressPath = filepath.Join(basedir, ".temp")
|
||||
|
||||
// Gathering photo path
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
return main_config, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
decoder := json.NewDecoder(file)
|
||||
decoder.DisallowUnknownFields()
|
||||
err = decoder.Decode(&cfg)
|
||||
return cfg, err
|
||||
main_config.PhotoPath = cfg.PhotoPath
|
||||
|
||||
return main_config, err
|
||||
}
|
||||
|
||||
func createConfigFile(cfg Config) {
|
||||
|
|
31
frontend/src/routes/+page.server.js
Normal file
31
frontend/src/routes/+page.server.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Server Side Rendering
|
||||
|
||||
export async function load(context) {
|
||||
async function fetchData(data) {
|
||||
try {
|
||||
const resTemp = await context.fetch(`http://0.0.0.0:8085/${data}`);
|
||||
if (resTemp.ok == false) {
|
||||
return {
|
||||
status: resTemp.status,
|
||||
}
|
||||
}
|
||||
return {
|
||||
status: 0, data: await resTemp.json(),
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 500,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const res = (await fetchData("photo_list"));
|
||||
if (res.status == 500) return {
|
||||
status: res.status
|
||||
}
|
||||
|
||||
return {
|
||||
status: 0,
|
||||
content: res.data,
|
||||
};
|
||||
}
|
|
@ -18,7 +18,9 @@
|
|||
import SvgIcon from "@jamescoyle/svelte-icon/src/svg-icon.svelte";
|
||||
import { mdiChevronRight, mdiChevronLeft } from "@mdi/js";
|
||||
|
||||
let photoList = filterPhotos(hubjson.photos);
|
||||
export let data;
|
||||
const photoData = data.status == 0 ? data : undefined;
|
||||
let photoList = filterPhotos(photoData.content);
|
||||
$: index = 0;
|
||||
let photoDiv;
|
||||
let tempPhotoDiv;
|
||||
|
|
Loading…
Add table
Reference in a new issue