You've already forked collage-maker
Compare commits
1 Commits
12befa0ac3
...
uploadexp
Author | SHA1 | Date | |
---|---|---|---|
d4446c1f24 |
195
main.go
195
main.go
@@ -10,7 +10,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -26,45 +25,32 @@ import (
|
|||||||
var (
|
var (
|
||||||
imagesDir string
|
imagesDir string
|
||||||
collageDir string
|
collageDir string
|
||||||
photosDir string
|
|
||||||
devMode bool
|
devMode bool
|
||||||
collageNameGen *nameGen
|
collageNameGen *nameGen
|
||||||
imagesDirFs fs.FS
|
imagesDirFs fs.FS
|
||||||
listenAddr string
|
listenAddr string
|
||||||
photoPrismURL *url.URL
|
|
||||||
photoPrismToken string
|
|
||||||
|
|
||||||
//go:embed web
|
//go:embed web
|
||||||
webFS embed.FS
|
webFS embed.FS
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
const (
|
||||||
var ppURL string
|
kb = 1024
|
||||||
|
mb = 1024 * kb
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir")
|
flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir")
|
||||||
flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir")
|
flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir")
|
||||||
flag.StringVar(&photosDir, "photos-dir", "photos", "Cache directory for downloaded photos")
|
|
||||||
flag.BoolVar(&devMode, "dev", false, "Serve local assets during development")
|
flag.BoolVar(&devMode, "dev", false, "Serve local assets during development")
|
||||||
flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax")
|
flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax")
|
||||||
flag.StringVar(&ppURL, "pp-url", "", "Base url for photoprism")
|
|
||||||
flag.StringVar(&photoPrismToken, "pp-token", "", "API token for photoprism")
|
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
photoPrismURL = func() *url.URL {
|
|
||||||
photoPrismURL, err := url.Parse(ppURL)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return photoPrismURL
|
|
||||||
}()
|
|
||||||
copy := *photoPrismURL
|
|
||||||
fmt.Println(copy)
|
|
||||||
collageNameGen = NewNameGen()
|
collageNameGen = NewNameGen()
|
||||||
imagesDirFs = os.DirFS(imagesDir)
|
imagesDirFs = os.DirFS(imagesDir)
|
||||||
imagesURLPath := "images"
|
imagesURLPath := "images"
|
||||||
collagesPath := "collages"
|
collagesPath := "collages"
|
||||||
photosPath := "photos"
|
|
||||||
|
|
||||||
addFileServer := func(path, dir string) {
|
addFileServer := func(path, dir string) {
|
||||||
httpFileServer := http.FileServer(http.Dir(dir))
|
httpFileServer := http.FileServer(http.Dir(dir))
|
||||||
@@ -73,7 +59,6 @@ func main() {
|
|||||||
|
|
||||||
addFileServer(imagesURLPath, imagesDir)
|
addFileServer(imagesURLPath, imagesDir)
|
||||||
addFileServer(collagesPath, collageDir)
|
addFileServer(collagesPath, collageDir)
|
||||||
addFileServer(photosPath, photosDir)
|
|
||||||
|
|
||||||
if devMode {
|
if devMode {
|
||||||
httpFileServer := http.FileServer(http.Dir("web"))
|
httpFileServer := http.FileServer(http.Dir("web"))
|
||||||
@@ -120,32 +105,55 @@ func main() {
|
|||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
collageFilePath, err := MakeCollage(&collageReq)
|
collageFile, err := MakeCollage(&collageReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to make collage", "error", err)
|
slog.Error("failed to make collage", "error", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err := w.Write([]byte(collageFilePath)); err != nil {
|
if _, err := w.Write([]byte(collageFile)); err != nil {
|
||||||
slog.Error("Failed to write collageFile", "error", err)
|
slog.Error("Failed to write collageFile", "error", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.HandleFunc("/get-albums", func(w http.ResponseWriter, r *http.Request) {
|
err := func() error {
|
||||||
albums, err := GetAlbums()
|
err := r.ParseMultipartForm(500 * mb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to get albums", "error", err)
|
return err
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
}
|
||||||
|
files, ok := r.MultipartForm.File["photos"]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("photos not found in request")
|
||||||
|
}
|
||||||
|
for i, f := range files {
|
||||||
|
imgFile, err := f.Open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer imgFile.Close()
|
||||||
|
imagePath := path.Join(imagesDir, fmt.Sprintf("img%d.jpg", i+1))
|
||||||
|
os.Remove(imagePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
op, err := os.Create(imagePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer op.Close()
|
||||||
|
_, err = io.Copy(op, imgFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}()
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Image upload failed", "error", err)
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
jData, err := json.Marshal(albums)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
addrType, server, done, err := anyhttp.Serve(listenAddr, idle.WrapHandler(nil))
|
addrType, server, done, err := anyhttp.Serve(listenAddr, idle.WrapHandler(nil))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -178,120 +186,3 @@ func MakeCollage(req *collage.Request) (string, error) {
|
|||||||
}
|
}
|
||||||
return collageFile, nil
|
return collageFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Album struct {
|
|
||||||
Title string `json:"Title"`
|
|
||||||
UID string `json:"UID"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAlbums() ([]Album, error) {
|
|
||||||
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()
|
|
||||||
}()
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", albumURL, 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
|
|
||||||
}
|
|
||||||
albums := []Album{}
|
|
||||||
|
|
||||||
err = json.Unmarshal(respBytes, &albums)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return albums, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
@@ -1,23 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
|
|
||||||
<!-- DEVONLY --> <script src="http://localhost:35729/livereload.js"></script>
|
|
||||||
<script src="choose.js" defer></script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<dialog id="notice_dialog">
|
|
||||||
<p id="notice_p"></p>
|
|
||||||
<form><button type="submit" formmethod="dialog">X</button></form>
|
|
||||||
</dialog>
|
|
||||||
<label>
|
|
||||||
<span>Select Album</span>
|
|
||||||
<select id="album_selector" size="8">
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
<button id="load_albums_button">Load Albums</button>
|
|
||||||
<button id="load_photos_button">Load Photos</button>
|
|
||||||
<div id="selected_photos"></div>
|
|
||||||
<div id="album_photos"></div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
@@ -1,76 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
|
|
||||||
// elements
|
|
||||||
|
|
||||||
/** @type {HTMLButtonElement} */
|
|
||||||
let loadAlbumsBtn
|
|
||||||
|
|
||||||
/** @type {HTMLButtonElement} */
|
|
||||||
let loadPhotosBtn
|
|
||||||
|
|
||||||
/** @type {HTMLSelectElement} */
|
|
||||||
let albumSelect
|
|
||||||
|
|
||||||
/** @type {HTMLParagraphElement} */
|
|
||||||
let noticeP
|
|
||||||
|
|
||||||
/** @type {HTMLDialogElement} */
|
|
||||||
let noticeDialog
|
|
||||||
|
|
||||||
function main() {
|
|
||||||
albumSelect = document.getElementById("album_selector")
|
|
||||||
loadAlbumsBtn = document.getElementById("load_albums_button")
|
|
||||||
loadPhotosBtn = document.getElementById("load_photos_button")
|
|
||||||
noticeDialog = document.getElementById("notice_dialog")
|
|
||||||
noticeP = document.getElementById("notice_p")
|
|
||||||
loadAlbumsBtn.onclick = () => loadAlbums()
|
|
||||||
loadPhotosBtn.onclick = () => loadPhotos()
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadAlbums() {
|
|
||||||
(async () => {
|
|
||||||
try {
|
|
||||||
closeNotice()
|
|
||||||
const resp = await fetch("get-albums")
|
|
||||||
const albums = await resp.json()
|
|
||||||
if(albums.length == 0) {
|
|
||||||
showNotice("No Albums found")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
albumSelect.replaceChildren() // This empties existing options
|
|
||||||
for(const album of albums) {
|
|
||||||
albumSelect.add(new Option(album.Title, album.UID))
|
|
||||||
}
|
|
||||||
} catch(e) {
|
|
||||||
console.log(e)
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
}
|
|
||||||
|
|
||||||
function closeNotice() {
|
|
||||||
noticeDialog.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} notice
|
|
||||||
*/
|
|
||||||
function showNotice(notice) {
|
|
||||||
noticeP.textContent = notice
|
|
||||||
noticeDialog.show()
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadPhotos() {
|
|
||||||
closeNotice()
|
|
||||||
const selected = albumSelect.selectedOptions
|
|
||||||
if(selected.length != 1) {
|
|
||||||
showNotice("Select an album to load photos")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const [elem] = selected
|
|
||||||
console.log(elem)
|
|
||||||
console.log(elem.text)
|
|
||||||
console.log(elem.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
main()
|
|
@@ -10,7 +10,36 @@
|
|||||||
justify-content: space-around;
|
justify-content: space-around;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
flex: 25%;
|
flex: 25%;
|
||||||
|
overflow: scroll;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.img-container {
|
||||||
|
/** margin: 30px; **/
|
||||||
|
display: flex;
|
||||||
|
gap: 1rem;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.img-container img {
|
||||||
|
width: 100px;
|
||||||
|
height: 100px;
|
||||||
|
border: 5px solid #999;
|
||||||
|
}
|
||||||
|
.img-container p {
|
||||||
|
text-align: center;
|
||||||
|
font-size: 2rem;
|
||||||
|
}
|
||||||
|
.toolbar {
|
||||||
|
height: 30px;
|
||||||
|
display: flex;
|
||||||
|
gap: 1rem;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: flex-start;
|
||||||
|
}
|
||||||
|
.selected-img {
|
||||||
|
border: 5px solid lightgreen !important;
|
||||||
|
}
|
||||||
|
|
||||||
.imagebox {
|
.imagebox {
|
||||||
padding: 2rem;
|
padding: 2rem;
|
||||||
flex: 75%;
|
flex: 75%;
|
||||||
|
@@ -24,6 +24,20 @@
|
|||||||
<option value="foursix-landscape" > 4 × 6 (Landscape) </option>
|
<option value="foursix-landscape" > 4 × 6 (Landscape) </option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
|
<div>
|
||||||
|
<div class="toolbar">
|
||||||
|
<button>Left</button>
|
||||||
|
<button>Right</button>
|
||||||
|
</div>
|
||||||
|
<div class="img-container">
|
||||||
|
<div> <img src="images/img1.jpg"/> </div>
|
||||||
|
<div> <img class="selected-img" src="images/img2.jpg"/> </div>
|
||||||
|
<div> <img src="images/img3.jpg"/> </div>
|
||||||
|
<div> <img src="images/img4.jpg"/> </div>
|
||||||
|
<div> <img src="images/img5.jpg"/> </div>
|
||||||
|
<div> <img src="images/img6.jpg"/> </div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<ul class="templates">
|
<ul class="templates">
|
||||||
|
12
web/upload.html
Normal file
12
web/upload.html
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<!-- DEVONLY --> <script src="http://localhost:35729/livereload.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="/upload" method="POST" enctype="multipart/form-data">
|
||||||
|
<input name="photos" type=file multiple accept="image/*" />
|
||||||
|
<input type=submit>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
Reference in New Issue
Block a user