full release edit capabilities oh my goodness gracious
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
34cddcfdb2
commit
604e2a4a7c
25 changed files with 1043 additions and 202 deletions
130
api/release.go
130
api/release.go
|
@ -1,9 +1,14 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -85,40 +90,53 @@ func CreateRelease() http.Handler {
|
|||
http.Error(w, "Release ID cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if *data.Title == "" {
|
||||
http.Error(w, "Release title cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
|
||||
title := data.ID
|
||||
if data.Title != nil && *data.Title != "" {
|
||||
title = *data.Title
|
||||
}
|
||||
if data.Buyname == nil || *data.Buyname == "" { *data.Buyname = "buy" }
|
||||
if data.Buylink == nil || *data.Buylink == "" { *data.Buylink = "https://arimelody.me" }
|
||||
|
||||
description := ""
|
||||
if data.Description != nil && *data.Description != "" { description = *data.Description }
|
||||
|
||||
releaseType := model.Single
|
||||
if data.ReleaseType != nil && *data.ReleaseType != "" { releaseType = *data.ReleaseType }
|
||||
|
||||
releaseDate := time.Time{}
|
||||
if data.ReleaseDate != nil && *data.ReleaseDate != "" {
|
||||
releaseDate, err = time.Parse("2006-01-02T15:04", *data.ReleaseDate)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid release date", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
releaseDate = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
|
||||
}
|
||||
|
||||
artwork := "/img/default-cover-art.png"
|
||||
if data.Artwork != nil && *data.Artwork != "" { artwork = *data.Artwork }
|
||||
|
||||
buyname := ""
|
||||
if data.Buyname != nil && *data.Buyname != "" { buyname = *data.Buyname }
|
||||
|
||||
buylink := ""
|
||||
if data.Buylink != nil && *data.Buylink != "" { buylink = *data.Buylink }
|
||||
|
||||
if global.GetRelease(data.ID) != nil {
|
||||
http.Error(w, fmt.Sprintf("Release %s already exists\n", data.ID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
releaseDate := time.Time{}
|
||||
if *data.ReleaseDate == "" {
|
||||
http.Error(w, "Release date cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
} else if data.ReleaseDate != nil {
|
||||
releaseDate, err = time.Parse("2006-01-02T15:04", *data.ReleaseDate)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid release date", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var release = model.Release{
|
||||
ID: data.ID,
|
||||
Visible: *data.Visible,
|
||||
Title: *data.Title,
|
||||
Description: *data.Description,
|
||||
ReleaseType: *data.ReleaseType,
|
||||
Visible: false,
|
||||
Title: title,
|
||||
Description: description,
|
||||
ReleaseType: releaseType,
|
||||
ReleaseDate: releaseDate,
|
||||
Artwork: *data.Artwork,
|
||||
Buyname: *data.Buyname,
|
||||
Buylink: *data.Buylink,
|
||||
Artwork: artwork,
|
||||
Buyname: buyname,
|
||||
Buylink: buylink,
|
||||
Links: []*model.Link{},
|
||||
Credits: []*model.Credit{},
|
||||
Tracks: []*model.Track{},
|
||||
|
@ -181,7 +199,69 @@ func UpdateRelease() http.Handler {
|
|||
}
|
||||
update.ReleaseDate = newDate
|
||||
}
|
||||
if data.Artwork != nil { update.Artwork = *data.Artwork }
|
||||
if data.Artwork != nil {
|
||||
if strings.Contains(*data.Artwork, ";base64,") {
|
||||
split := strings.Split(*data.Artwork, ";base64,")
|
||||
header := split[0]
|
||||
imageData, err := base64.StdEncoding.DecodeString(split[1])
|
||||
ext, _ := strings.CutPrefix(header, "data:image/")
|
||||
|
||||
switch ext {
|
||||
case "png":
|
||||
case "jpg":
|
||||
case "jpeg":
|
||||
default:
|
||||
http.Error(w, "Invalid image type. Allowed: .png, .jpg, .jpeg", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
artworkDirectory := filepath.Join("uploads", "musicart")
|
||||
// ensure directory exists
|
||||
os.MkdirAll(artworkDirectory, os.ModePerm)
|
||||
|
||||
imagePath := filepath.Join(artworkDirectory, fmt.Sprintf("%s.%s", update.ID, ext))
|
||||
file, err := os.Create(imagePath)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to create file %s: %s\n", imagePath, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
buffer := bufio.NewWriter(file)
|
||||
_, err = buffer.Write(imageData)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to write to file %s: %s\n", imagePath, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := buffer.Flush(); err != nil {
|
||||
fmt.Printf("FATAL: Failed to flush data to file %s: %s\n", imagePath, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// clean up files with this ID and different extensions
|
||||
err = filepath.Walk(artworkDirectory, func(path string, info fs.FileInfo, err error) error {
|
||||
if path == imagePath { return nil }
|
||||
|
||||
withoutExt := strings.TrimSuffix(path, filepath.Ext(path))
|
||||
if withoutExt != filepath.Join(artworkDirectory, update.ID) { return nil }
|
||||
|
||||
return os.Remove(path)
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while cleaning up artwork files: %s\n", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Artwork for %s updated.\n", update.ID)
|
||||
update.Artwork = fmt.Sprintf("/uploads/musicart/%s.%s", update.ID, ext)
|
||||
} else {
|
||||
update.Artwork = *data.Artwork
|
||||
}
|
||||
}
|
||||
if data.Buyname != nil {
|
||||
if *data.Buyname == "" {
|
||||
http.Error(w, "Release buy name cannot be empty", http.StatusBadRequest)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue