2023-08-01 19:16:46 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-13 14:38:42 -04:00
|
|
|
"bytes"
|
2023-09-11 21:16:10 -04:00
|
|
|
"context"
|
2023-08-31 20:27:43 -04:00
|
|
|
"embed"
|
2023-08-28 23:35:32 -04:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
2023-08-01 19:16:46 -04:00
|
|
|
"fmt"
|
2023-08-28 23:35:32 -04:00
|
|
|
"io"
|
2023-08-31 20:27:43 -04:00
|
|
|
"io/fs"
|
2023-08-28 23:35:32 -04:00
|
|
|
"net/http"
|
2024-08-02 23:39:49 -04:00
|
|
|
"net/url"
|
2023-08-01 19:16:46 -04:00
|
|
|
"os"
|
2023-08-28 23:35:32 -04:00
|
|
|
"path"
|
2023-09-13 14:38:42 -04:00
|
|
|
"regexp"
|
2023-08-28 23:35:32 -04:00
|
|
|
"time"
|
2023-08-03 19:33:05 -04:00
|
|
|
|
2023-08-28 23:35:32 -04:00
|
|
|
"log/slog"
|
|
|
|
|
2023-09-11 21:16:10 -04:00
|
|
|
"go.balki.me/anyhttp"
|
|
|
|
"go.balki.me/anyhttp/idle"
|
2023-08-28 23:35:32 -04:00
|
|
|
"go.balki.me/collage-maker/collage"
|
2023-08-01 19:16:46 -04:00
|
|
|
)
|
|
|
|
|
2023-08-28 23:35:32 -04:00
|
|
|
var (
|
2024-08-02 23:39:49 -04:00
|
|
|
collageDir string
|
2024-08-06 23:14:06 -04:00
|
|
|
photosDir string
|
2024-08-02 23:39:49 -04:00
|
|
|
devMode bool
|
|
|
|
collageNameGen *nameGen
|
|
|
|
imagesDirFs fs.FS
|
|
|
|
listenAddr string
|
2024-08-06 23:14:06 -04:00
|
|
|
photoPrismURL *url.URL
|
2024-08-02 23:39:49 -04:00
|
|
|
photoPrismToken string
|
2023-09-01 15:13:54 -04:00
|
|
|
|
2023-09-13 14:38:42 -04:00
|
|
|
//go:embed web
|
2023-09-01 15:13:54 -04:00
|
|
|
webFS embed.FS
|
2023-08-28 23:35:32 -04:00
|
|
|
)
|
2023-08-03 19:33:05 -04:00
|
|
|
|
2023-08-01 19:16:46 -04:00
|
|
|
func main() {
|
2024-08-06 23:14:06 -04:00
|
|
|
var ppURL string
|
|
|
|
|
2023-08-28 23:35:32 -04:00
|
|
|
flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir")
|
2024-08-06 23:14:06 -04:00
|
|
|
flag.StringVar(&photosDir, "photos-dir", "photos", "Cache directory for downloaded photos")
|
2023-09-19 22:47:09 -04:00
|
|
|
flag.BoolVar(&devMode, "dev", false, "Serve local assets during development")
|
2023-09-11 21:16:10 -04:00
|
|
|
flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax")
|
2024-08-06 23:14:06 -04:00
|
|
|
flag.StringVar(&ppURL, "pp-url", "", "Base url for photoprism")
|
2024-08-02 23:39:49 -04:00
|
|
|
flag.StringVar(&photoPrismToken, "pp-token", "", "API token for photoprism")
|
2023-08-31 20:27:43 -04:00
|
|
|
|
2023-08-28 23:35:32 -04:00
|
|
|
flag.Parse()
|
2023-09-01 15:13:54 -04:00
|
|
|
|
2024-08-06 23:14:06 -04:00
|
|
|
photoPrismURL = func() *url.URL {
|
|
|
|
photoPrismURL, err := url.Parse(ppURL)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return photoPrismURL
|
|
|
|
}()
|
2023-09-01 15:13:54 -04:00
|
|
|
collageNameGen = NewNameGen()
|
2024-08-08 22:10:55 -04:00
|
|
|
imagesDirFs = os.DirFS(photosDir)
|
2023-08-31 20:27:43 -04:00
|
|
|
collagesPath := "collages"
|
2024-08-06 23:14:06 -04:00
|
|
|
photosPath := "photos"
|
2023-08-31 20:27:43 -04:00
|
|
|
|
|
|
|
addFileServer := func(path, dir string) {
|
|
|
|
httpFileServer := http.FileServer(http.Dir(dir))
|
|
|
|
http.Handle("/"+path+"/", http.StripPrefix("/"+path, httpFileServer))
|
|
|
|
}
|
|
|
|
|
|
|
|
addFileServer(collagesPath, collageDir)
|
2024-08-06 23:14:06 -04:00
|
|
|
addFileServer(photosPath, photosDir)
|
2023-08-31 20:27:43 -04:00
|
|
|
|
2023-09-19 22:47:09 -04:00
|
|
|
if devMode {
|
2023-08-31 20:27:43 -04:00
|
|
|
httpFileServer := http.FileServer(http.Dir("web"))
|
2023-09-05 09:04:54 -04:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Cache-Control", "no-cache")
|
|
|
|
httpFileServer.ServeHTTP(w, r)
|
|
|
|
})
|
2023-08-31 20:27:43 -04:00
|
|
|
} else {
|
2023-09-13 14:38:42 -04:00
|
|
|
indexModTime := time.Now()
|
|
|
|
indexHTML := func() io.ReadSeeker {
|
|
|
|
indexHTMLContent, err := webFS.ReadFile("web/index.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
devOnlyRegex := regexp.MustCompile("\n[^\n]*<!-- DEVONLY[^\n]*")
|
|
|
|
return bytes.NewReader(devOnlyRegex.ReplaceAllLiteral(indexHTMLContent, nil))
|
|
|
|
}()
|
|
|
|
httpFileServer := func() http.Handler {
|
|
|
|
webrootFs, err := fs.Sub(webFS, "web")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return http.FileServer(http.FS(webrootFs))
|
|
|
|
}()
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
http.ServeContent(w, r, "index.html", indexModTime, indexHTML)
|
|
|
|
} else {
|
|
|
|
httpFileServer.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
2023-08-31 20:27:43 -04:00
|
|
|
}
|
2023-08-01 19:16:46 -04:00
|
|
|
|
2023-08-28 23:35:32 -04:00
|
|
|
http.HandleFunc("/make-collage", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
collageReq := collage.Request{}
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("failed to read request body", "error", err)
|
2023-09-01 15:13:54 -04:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-08-28 23:35:32 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(body, &collageReq); err != nil {
|
|
|
|
slog.Error("failed to unmarshal json request", "error", err)
|
2023-09-01 15:13:54 -04:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-08-28 23:35:32 -04:00
|
|
|
return
|
|
|
|
}
|
2024-08-02 23:39:49 -04:00
|
|
|
collageFilePath, err := MakeCollage(&collageReq)
|
2023-08-28 23:35:32 -04:00
|
|
|
if err != nil {
|
|
|
|
slog.Error("failed to make collage", "error", err)
|
2023-09-01 15:13:54 -04:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-08-28 23:35:32 -04:00
|
|
|
return
|
|
|
|
}
|
2024-08-02 23:39:49 -04:00
|
|
|
if _, err := w.Write([]byte(collageFilePath)); err != nil {
|
2023-09-11 21:16:10 -04:00
|
|
|
slog.Error("Failed to write collageFile", "error", err)
|
|
|
|
}
|
2023-08-03 19:33:05 -04:00
|
|
|
})
|
2024-08-02 23:39:49 -04:00
|
|
|
|
|
|
|
http.HandleFunc("/get-albums", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
albums, err := GetAlbums()
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("failed to get albums", "error", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jData, err := json.Marshal(albums)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("failed to marshal albums", "error", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(jData)
|
|
|
|
})
|
2024-08-07 18:07:41 -04:00
|
|
|
|
|
|
|
http.HandleFunc("/load-photos/{albumID}", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
albumID := r.PathValue("albumID")
|
|
|
|
photos, err := LoadPhotos(albumID)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("failed to load photos", "error", err, "album", albumID)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
photoURLs := []string{}
|
|
|
|
for _, photo := range photos {
|
|
|
|
photoURLs = append(photoURLs, fmt.Sprintf("/%s/%s.jpg", photosPath, photo.FileUID))
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for _, photo := range photos {
|
|
|
|
_, err := DownloadPhoto(&photo)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("failed to download photo", "error", err, "album", albumID, "photoID", photo.FileUID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
jData, err := json.Marshal(photoURLs)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("failed to marshal photoURLs", "error", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(jData)
|
|
|
|
})
|
|
|
|
|
2023-09-11 21:16:10 -04:00
|
|
|
addrType, server, done, err := anyhttp.Serve(listenAddr, idle.WrapHandler(nil))
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("anyhttp Serve failed", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if addrType == anyhttp.SystemdFD {
|
|
|
|
if err := idle.Wait(30 * time.Minute); err != nil {
|
|
|
|
slog.Error("Failed to wait for idler", "error", err)
|
|
|
|
}
|
2023-09-13 14:38:42 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections
|
|
|
|
defer cancel()
|
2023-09-11 21:16:10 -04:00
|
|
|
if err := server.Shutdown(ctx); err != nil {
|
|
|
|
slog.Error("http server Shutdown failed", "error", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
<-done
|
2023-08-03 19:33:05 -04:00
|
|
|
}
|
2023-08-28 23:35:32 -04:00
|
|
|
}
|
2023-08-03 19:33:05 -04:00
|
|
|
|
2023-09-01 15:13:54 -04:00
|
|
|
func MakeCollage(req *collage.Request) (string, error) {
|
|
|
|
collageFile := fmt.Sprintf("collage-%s.jpg", collageNameGen.Next())
|
|
|
|
out, err := os.Create(path.Join(collageDir, collageFile))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to create collage output file, err: %w", err)
|
|
|
|
}
|
2023-09-19 22:47:09 -04:00
|
|
|
defer out.Close()
|
2023-09-01 15:13:54 -04:00
|
|
|
if err := collage.Make(req, imagesDirFs, out); err != nil {
|
|
|
|
return "", fmt.Errorf("failed to make collage, err: %w", err)
|
|
|
|
}
|
|
|
|
return collageFile, nil
|
|
|
|
}
|
2024-08-02 23:39:49 -04:00
|
|
|
|
|
|
|
type Album struct {
|
|
|
|
Title string `json:"Title"`
|
|
|
|
UID string `json:"UID"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAlbums() ([]Album, error) {
|
2024-08-06 23:14:06 -04:00
|
|
|
albumURL := func() string {
|
|
|
|
u := *photoPrismURL
|
|
|
|
u.Path = "/api/v1/albums"
|
|
|
|
v := url.Values{}
|
|
|
|
v.Add("count", "20")
|
|
|
|
v.Add("type", "album")
|
|
|
|
u.RawQuery = v.Encode()
|
|
|
|
return u.String()
|
|
|
|
}()
|
2024-08-02 23:39:49 -04:00
|
|
|
|
2024-08-06 23:14:06 -04:00
|
|
|
req, err := http.NewRequest("GET", albumURL, nil)
|
2024-08-02 23:39:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", photoPrismToken))
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
respBytes, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-06 23:14:06 -04:00
|
|
|
albums := []Album{}
|
2024-08-02 23:39:49 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &albums)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return albums, nil
|
|
|
|
}
|
2024-08-06 23:14:06 -04:00
|
|
|
|
|
|
|
type Photo struct {
|
|
|
|
Hash string `json:"Hash"`
|
|
|
|
FileUID string `json:"FileUID"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadPhotos(albumID string) ([]Photo, error) {
|
|
|
|
loadPhotosURL := func() string {
|
|
|
|
u := *photoPrismURL
|
|
|
|
u.Path = "/api/v1/photos"
|
|
|
|
v := url.Values{}
|
|
|
|
v.Add("count", "50")
|
|
|
|
v.Add("s", albumID)
|
|
|
|
v.Add("merged", "true")
|
|
|
|
v.Add("video", "false")
|
|
|
|
u.RawQuery = v.Encode()
|
|
|
|
return u.String()
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", loadPhotosURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", photoPrismToken))
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
respBytes, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
photos := []Photo{}
|
|
|
|
err = json.Unmarshal(respBytes, &photos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return photos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DownloadPhoto(photo *Photo) (string, error) {
|
|
|
|
photoPath := path.Join(photosDir, fmt.Sprintf("%s.jpg", photo.FileUID))
|
|
|
|
_, err := os.Stat(photoPath)
|
|
|
|
if err == nil {
|
|
|
|
return photoPath, nil
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
out, err := os.Create(photoPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to create collage output file, err: %w", err)
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
downloadPhotoURL := func() string {
|
|
|
|
u := *photoPrismURL
|
|
|
|
u.Path = fmt.Sprintf("/api/v1/dl/%s", photo.Hash)
|
|
|
|
return u.String()
|
|
|
|
}()
|
|
|
|
req, err := http.NewRequest("GET", downloadPhotoURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", photoPrismToken))
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return photoPath, nil
|
|
|
|
}
|