turns out rewriting all of your database code takes a while
This commit is contained in:
parent
1998a36d6d
commit
965d6f5c3e
30 changed files with 947 additions and 1036 deletions
49
api/api.go
49
api/api.go
|
@ -1,9 +1,13 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"arimelody.me/arimelody.me/admin"
|
||||
"arimelody.me/arimelody.me/global"
|
||||
"arimelody.me/arimelody.me/music/model"
|
||||
music "arimelody.me/arimelody.me/music/view"
|
||||
)
|
||||
|
||||
|
@ -13,16 +17,25 @@ func Handler() http.Handler {
|
|||
// ARTIST ENDPOINTS
|
||||
|
||||
mux.Handle("/v1/artist/", http.StripPrefix("/v1/artist", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artistID = strings.Split(r.URL.Path[1:], "/")[0]
|
||||
var artist model.Artist
|
||||
err := global.DB.Get(&artist, "SELECT * FROM artist WHERE id=$1", artistID)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Error while retrieving artist %s: %s\n", artistID, err)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/artist/{id}
|
||||
ServeArtist().ServeHTTP(w, r)
|
||||
ServeArtist(artist).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/artist/{id} (admin)
|
||||
admin.MustAuthorise(UpdateArtist()).ServeHTTP(w, r)
|
||||
admin.MustAuthorise(UpdateArtist(artist)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/artist/{id} (admin)
|
||||
admin.MustAuthorise(DeleteArtist()).ServeHTTP(w, r)
|
||||
admin.MustAuthorise(DeleteArtist(artist)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
@ -43,16 +56,25 @@ func Handler() http.Handler {
|
|||
// RELEASE ENDPOINTS
|
||||
|
||||
mux.Handle("/v1/music/", http.StripPrefix("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var releaseID = strings.Split(r.URL.Path[1:], "/")[0]
|
||||
var release model.Release
|
||||
err := global.DB.Get(&release, "SELECT * FROM musicrelease WHERE id=$1", releaseID)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/music/{id}
|
||||
music.ServeRelease().ServeHTTP(w, r)
|
||||
music.ServeRelease(release).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/music/{id} (admin)
|
||||
admin.MustAuthorise(UpdateRelease()).ServeHTTP(w, r)
|
||||
admin.MustAuthorise(UpdateRelease(release)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/music/{id} (admin)
|
||||
admin.MustAuthorise(DeleteRelease()).ServeHTTP(w, r)
|
||||
admin.MustAuthorise(DeleteRelease(release)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
@ -73,16 +95,25 @@ func Handler() http.Handler {
|
|||
// TRACK ENDPOINTS
|
||||
|
||||
mux.Handle("/v1/track/", http.StripPrefix("/v1/track", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var trackID = strings.Split(r.URL.Path[1:], "/")[0]
|
||||
var track model.Track
|
||||
err := global.DB.Get(&track, "SELECT * FROM musictrack WHERE id=$1", trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Error while retrieving track %s: %s\n", trackID, err)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/track/{id} (admin)
|
||||
admin.MustAuthorise(ServeTrack()).ServeHTTP(w, r)
|
||||
admin.MustAuthorise(ServeTrack(track)).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/track/{id} (admin)
|
||||
admin.MustAuthorise(UpdateTrack()).ServeHTTP(w, r)
|
||||
admin.MustAuthorise(UpdateTrack(track)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/track/{id} (admin)
|
||||
admin.MustAuthorise(DeleteTrack()).ServeHTTP(w, r)
|
||||
admin.MustAuthorise(DeleteTrack(track)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
|
183
api/artist.go
183
api/artist.go
|
@ -4,10 +4,10 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"arimelody.me/arimelody.me/global"
|
||||
"arimelody.me/arimelody.me/music/model"
|
||||
controller "arimelody.me/arimelody.me/music/controller"
|
||||
)
|
||||
|
||||
type artistJSON struct {
|
||||
|
@ -19,76 +19,60 @@ type artistJSON struct {
|
|||
|
||||
func ServeAllArtists() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(global.Artists)
|
||||
var artists = []*model.Artist{}
|
||||
err := global.DB.Select(&artists, "SELECT * FROM artist")
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to serve all artists: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(artists)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func ServeArtist() http.Handler {
|
||||
func ServeArtist(artist model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
ServeAllArtists().ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
type (
|
||||
creditJSON struct {
|
||||
Role string `json:"role"`
|
||||
Primary bool `json:"primary"`
|
||||
Release string `json:"release"`
|
||||
Role string `json:"role"`
|
||||
Primary bool `json:"primary"`
|
||||
}
|
||||
artistJSON struct {
|
||||
model.Artist
|
||||
Credits map[string]creditJSON `json:"credits"`
|
||||
}
|
||||
)
|
||||
var artist = artistJSON{}
|
||||
|
||||
artist.ID = r.URL.Path[1:]
|
||||
var a = global.GetArtist(artist.ID)
|
||||
if a == nil {
|
||||
http.NotFound(w, r)
|
||||
var credits = map[string]creditJSON{}
|
||||
err := global.DB.Select(&credits, "SELECT release,role,is_primary FROM musiccredit WHERE id=$1", artist.ID)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to retrieve artist credits for %s: %s\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
artist.Name = a.Name
|
||||
artist.Website = a.Website
|
||||
artist.Credits = make(map[string]creditJSON)
|
||||
|
||||
for _, release := range global.Releases {
|
||||
for _, credit := range release.Credits {
|
||||
if credit.Artist.ID != artist.ID {
|
||||
continue
|
||||
}
|
||||
artist.Credits[release.ID] = creditJSON{
|
||||
Role: credit.Role,
|
||||
Primary: credit.Primary,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(artist)
|
||||
err = json.NewEncoder(w).Encode(artistJSON{
|
||||
Artist: artist,
|
||||
Credits: credits,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func CreateArtist() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var data artistJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create artist: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
@ -102,11 +86,6 @@ func CreateArtist() http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
if global.GetArtist(data.ID) != nil {
|
||||
http.Error(w, fmt.Sprintf("Artist %s already exists\n", data.ID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var artist = model.Artist{
|
||||
ID: data.ID,
|
||||
Name: *data.Name,
|
||||
|
@ -114,116 +93,66 @@ func CreateArtist() http.Handler {
|
|||
Avatar: *data.Avatar,
|
||||
}
|
||||
|
||||
err = controller.CreateArtistDB(global.DB, &artist)
|
||||
_, err = global.DB.Exec(
|
||||
"INSERT INTO artist (id, name, website, avatar) "+
|
||||
"VALUES ($1, $2, $3, $4)",
|
||||
artist.ID,
|
||||
artist.Name,
|
||||
artist.Website,
|
||||
artist.Avatar)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create artist %s: %s\n", artist.ID, err)
|
||||
if strings.Contains(err.Error(), "duplicate key") {
|
||||
http.Error(w, fmt.Sprintf("Artist %s already exists\n", data.ID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Printf("FATAL: Failed to create artist %s: %s\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
global.Artists = append(global.Artists, &artist)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(artist)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateArtist() http.Handler {
|
||||
func UpdateArtist(artist model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var data artistJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update artist: %s\n", err)
|
||||
fmt.Printf("FATAL: Failed to update artist: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var artistID = r.URL.Path[1:]
|
||||
var artist = global.GetArtist(artistID)
|
||||
if artist == nil {
|
||||
http.Error(w, fmt.Sprintf("Artist %s does not exist\n", artistID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if data.ID != "" { artist.ID = data.ID }
|
||||
if data.Name != nil { artist.Name = *data.Name }
|
||||
if data.Website != nil { artist.Website = *data.Website }
|
||||
if data.Avatar != nil { artist.Avatar = *data.Avatar }
|
||||
|
||||
var update = *artist
|
||||
|
||||
if data.ID != "" { update.ID = data.ID }
|
||||
if data.Name != nil { update.Name = *data.Name }
|
||||
if data.Website != nil { update.Website = *data.Website }
|
||||
if data.Avatar != nil { update.Avatar = *data.Avatar }
|
||||
|
||||
err = controller.UpdateArtistDB(global.DB, &update)
|
||||
_, err = global.DB.Exec(
|
||||
"UPDATE artist "+
|
||||
"SET name=$2, website=$3, avatar=$4 "+
|
||||
"WHERE id=$1",
|
||||
artist.ID,
|
||||
artist.Name,
|
||||
artist.Website,
|
||||
artist.Avatar)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update artist %s: %s\n", artist.ID, err)
|
||||
fmt.Printf("FATAL: Failed to update artist %s: %s\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
artist.ID = update.ID
|
||||
artist.Name = update.Name
|
||||
artist.Website = update.Website
|
||||
artist.Avatar = update.Avatar
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(artist)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteArtist() http.Handler {
|
||||
func DeleteArtist(artist model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var artistID = r.URL.Path[1:]
|
||||
var artist = global.GetArtist(artistID)
|
||||
if artist == nil {
|
||||
http.Error(w, fmt.Sprintf("Artist %s does not exist\n", artistID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := controller.DeleteArtistDB(global.DB, artist)
|
||||
_, err := global.DB.Exec(
|
||||
"DELETE FROM artist "+
|
||||
"WHERE id=$1",
|
||||
artist.ID)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to delete artist %s: %s\n", artist.ID, err)
|
||||
fmt.Printf("FATAL: Failed to delete artist %s: %s\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
global.Artists = func () []*model.Artist {
|
||||
var artists = []*model.Artist{}
|
||||
for _, a := range global.Artists {
|
||||
if a.ID == artist.ID { continue }
|
||||
artists = append(artists, a)
|
||||
}
|
||||
return artists
|
||||
}()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(fmt.Sprintf("Artist %s has been deleted\n", artist.ID)))
|
||||
})
|
||||
}
|
||||
|
|
449
api/release.go
449
api/release.go
|
@ -1,8 +1,6 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
|
@ -14,7 +12,6 @@ import (
|
|||
|
||||
"arimelody.me/arimelody.me/admin"
|
||||
"arimelody.me/arimelody.me/global"
|
||||
controller "arimelody.me/arimelody.me/music/controller"
|
||||
"arimelody.me/arimelody.me/music/model"
|
||||
)
|
||||
|
||||
|
@ -32,39 +29,40 @@ type releaseBodyJSON struct {
|
|||
|
||||
func ServeCatalog() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
type CatalogItem struct {
|
||||
type catalogItem struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
ReleaseType model.ReleaseType `json:"type"`
|
||||
ReleaseDate time.Time `json:"releaseDate"`
|
||||
Artwork string `json:"artwork"`
|
||||
Buyname string `json:"buyname"`
|
||||
Buylink string `json:"buylink"`
|
||||
Links []*model.Link `json:"links"`
|
||||
}
|
||||
|
||||
catalog := []CatalogItem{}
|
||||
releases := []*model.Release{}
|
||||
err := global.DB.Select(&releases, "SELECT * FROM musicrelease ORDER BY release_date DESC")
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
catalog := []catalogItem{}
|
||||
authorised := admin.GetSession(r) != nil
|
||||
for _, release := range global.Releases {
|
||||
for _, release := range releases {
|
||||
if !release.Visible && !authorised {
|
||||
continue
|
||||
}
|
||||
catalog = append(catalog, CatalogItem{
|
||||
catalog = append(catalog, catalogItem{
|
||||
ID: release.ID,
|
||||
Title: release.Title,
|
||||
Description: release.Description,
|
||||
ReleaseType: release.ReleaseType,
|
||||
ReleaseDate: release.ReleaseDate,
|
||||
Artwork: release.Artwork,
|
||||
Buyname: release.Buyname,
|
||||
Buylink: release.Buylink,
|
||||
Links: release.Links,
|
||||
})
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(catalog)
|
||||
err = json.NewEncoder(w).Encode(catalog)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -122,11 +120,6 @@ func CreateRelease() http.Handler {
|
|||
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
|
||||
}
|
||||
|
||||
var release = model.Release{
|
||||
ID: data.ID,
|
||||
Visible: false,
|
||||
|
@ -137,31 +130,42 @@ func CreateRelease() http.Handler {
|
|||
Artwork: artwork,
|
||||
Buyname: buyname,
|
||||
Buylink: buylink,
|
||||
Links: []*model.Link{},
|
||||
Credits: []*model.Credit{},
|
||||
Tracks: []*model.Track{},
|
||||
}
|
||||
|
||||
err = controller.CreateReleaseDB(global.DB, &release)
|
||||
_, err = global.DB.Exec(
|
||||
"INSERT INTO musicrelease "+
|
||||
"(id, visible, title, description, type, release_date, artwork, buyname, buylink) "+
|
||||
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
||||
release.ID,
|
||||
release.Visible,
|
||||
release.Title,
|
||||
release.Description,
|
||||
release.ReleaseType,
|
||||
release.ReleaseDate.Format("2006-01-02 15:04:05"),
|
||||
release.Artwork,
|
||||
release.Buyname,
|
||||
release.Buylink)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "duplicate key") {
|
||||
http.Error(w, fmt.Sprintf("Release %s already exists\n", data.ID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Failed to create release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
global.Releases = append([]*model.Release{&release}, global.Releases...)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(release)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Release %s created, but failed to send JSON response: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateRelease() http.Handler {
|
||||
func UpdateRelease(release model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
|
@ -170,124 +174,11 @@ func UpdateRelease() http.Handler {
|
|||
|
||||
segments := strings.Split(r.URL.Path[1:], "/")
|
||||
var releaseID = segments[0]
|
||||
var release = global.GetRelease(releaseID)
|
||||
if release == nil {
|
||||
http.Error(w, fmt.Sprintf("Release %s does not exist\n", releaseID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if len(segments) == 1 {
|
||||
var data releaseBodyJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update release: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var update = *release
|
||||
if data.ID != "" { update.ID = data.ID }
|
||||
if data.Visible != nil { update.Visible = *data.Visible }
|
||||
if data.Title != nil { update.Title = *data.Title }
|
||||
if data.Description != nil { update.Description = *data.Description }
|
||||
if data.ReleaseType != nil { update.ReleaseType = *data.ReleaseType }
|
||||
if data.ReleaseDate != nil {
|
||||
newDate, err := time.Parse("2006-01-02T15:04", *data.ReleaseDate)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid release date", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
update.ReleaseDate = newDate
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
update.Artwork = fmt.Sprintf("/uploads/musicart/%s.%s", update.ID, ext)
|
||||
} else {
|
||||
update.Artwork = *data.Artwork
|
||||
}
|
||||
}
|
||||
|
||||
if data.Buyname != nil { update.Buyname = *data.Buyname }
|
||||
if data.Buylink != nil { update.Buylink = *data.Buylink }
|
||||
|
||||
err = controller.UpdateReleaseDB(global.DB, &update)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
release.ID = update.ID
|
||||
release.Visible = update.Visible
|
||||
release.Title = update.Title
|
||||
release.Description = update.Description
|
||||
release.ReleaseType = update.ReleaseType
|
||||
release.ReleaseDate = update.ReleaseDate
|
||||
release.Artwork = update.Artwork
|
||||
release.Buyname = update.Buyname
|
||||
release.Buylink = update.Buylink
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(release)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var exists int
|
||||
err := global.DB.Get(&exists, "SELECT count(*) FROM musicrelease WHERE id=$1", releaseID)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update release: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -302,18 +193,81 @@ func UpdateRelease() http.Handler {
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
http.NotFound(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseTracks(release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" || r.Method != http.MethodPut {
|
||||
|
||||
if len(segments) > 2 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var data releaseBodyJSON
|
||||
err = json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if data.ID != "" { release.ID = data.ID }
|
||||
if data.Visible != nil { release.Visible = *data.Visible }
|
||||
if data.Title != nil { release.Title = *data.Title }
|
||||
if data.Description != nil { release.Description = *data.Description }
|
||||
if data.ReleaseType != nil { release.ReleaseType = *data.ReleaseType }
|
||||
if data.ReleaseDate != nil {
|
||||
newDate, err := time.Parse("2006-01-02T15:04", *data.ReleaseDate)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid release date", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
release.ReleaseDate = newDate
|
||||
}
|
||||
if data.Artwork != nil {
|
||||
if strings.Contains(*data.Artwork, ";base64,") {
|
||||
var artworkDirectory = filepath.Join("uploads", "musicart")
|
||||
filename, err := HandleImageUpload(data.Artwork, artworkDirectory, data.ID)
|
||||
|
||||
// clean up files with this ID and different extensions
|
||||
err = filepath.Walk(artworkDirectory, func(path string, info fs.FileInfo, err error) error {
|
||||
if path == filepath.Join(artworkDirectory, filename) { return nil }
|
||||
|
||||
withoutExt := strings.TrimSuffix(path, filepath.Ext(path))
|
||||
if withoutExt != filepath.Join(artworkDirectory, release.ID) { return nil }
|
||||
|
||||
return os.Remove(path)
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while cleaning up artwork files: %s\n", err)
|
||||
}
|
||||
|
||||
release.Artwork = fmt.Sprintf("/uploads/musicart/%s", filename)
|
||||
} else {
|
||||
release.Artwork = *data.Artwork
|
||||
}
|
||||
}
|
||||
|
||||
if data.Buyname != nil { release.Buyname = *data.Buyname }
|
||||
if data.Buylink != nil { release.Buylink = *data.Buylink }
|
||||
|
||||
_, err = global.DB.Exec(
|
||||
"UPDATE musicrelease SET "+
|
||||
"visible=$2, title=$3, description=$4, type=$5, release_date=$6, artwork=$7, buyname=$8, buylink=$9 "+
|
||||
"WHERE id=$1",
|
||||
release.ID,
|
||||
release.Visible,
|
||||
release.Title,
|
||||
release.Description,
|
||||
release.ReleaseType,
|
||||
release.ReleaseDate.Format("2006-01-02 15:04:05"),
|
||||
release.Artwork,
|
||||
release.Buyname,
|
||||
release.Buylink)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to update release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseTracks(release model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var trackIDs = []string{}
|
||||
err := json.NewDecoder(r.Body).Decode(&trackIDs)
|
||||
if err != nil {
|
||||
|
@ -321,123 +275,85 @@ func UpdateReleaseTracks(release *model.Release) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
var old_tracks = (*release).Tracks
|
||||
var new_tracks = []*model.Track{}
|
||||
for _, trackID := range trackIDs {
|
||||
var track = global.GetTrack(trackID)
|
||||
if track == nil {
|
||||
http.Error(w, fmt.Sprintf("Track %s does not exist\n", trackID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
new_tracks = append(new_tracks, track)
|
||||
track.Release = release
|
||||
tx := global.DB.MustBegin()
|
||||
tx.MustExec("DELETE FROM musicreleasetrack WHERE release=$1", release.ID)
|
||||
for i, trackID := range trackIDs {
|
||||
tx.MustExec(
|
||||
"INSERT INTO musicreleasetrack "+
|
||||
"(release, track, number) "+
|
||||
"VALUES ($1, $2, $3)",
|
||||
release.ID,
|
||||
trackID,
|
||||
i)
|
||||
}
|
||||
|
||||
err = controller.UpdateReleaseTracksDB(global.DB, release, new_tracks)
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update tracks for %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
release.Tracks = new_tracks
|
||||
|
||||
// remove release from orphaned tracks
|
||||
for _, old_track := range old_tracks {
|
||||
var exists = false
|
||||
for _, track := range new_tracks {
|
||||
if track.ID == old_track.ID {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
old_track.Release = nil
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
err = json.NewEncoder(w).Encode(release)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseCredits(release *model.Release) http.Handler {
|
||||
func UpdateReleaseCredits(release model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
type creditJSON struct {
|
||||
Artist string
|
||||
Role string
|
||||
Primary bool
|
||||
}
|
||||
|
||||
var list []creditJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&list)
|
||||
var data []creditJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var credits = []*model.Credit{}
|
||||
for i, data := range list {
|
||||
if data.Artist == "" {
|
||||
http.Error(w, fmt.Sprintf("Artist ID cannot be blank (%d)", i), http.StatusBadRequest)
|
||||
return
|
||||
// clear duplicates
|
||||
type Credit struct {
|
||||
Role string
|
||||
Primary bool
|
||||
}
|
||||
var credits = map[string]Credit{}
|
||||
for _, credit := range data {
|
||||
credits[credit.Artist] = Credit{
|
||||
Role: credit.Role,
|
||||
Primary: credit.Primary,
|
||||
}
|
||||
|
||||
for _, credit := range credits {
|
||||
if data.Artist == credit.Artist.ID {
|
||||
http.Error(w, fmt.Sprintf("Artist %s credited more than once", data.Artist), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if data.Role == "" {
|
||||
http.Error(w, fmt.Sprintf("Artist role cannot be blank (%d)", i), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var artist = global.GetArtist(data.Artist)
|
||||
if artist == nil {
|
||||
http.Error(w, fmt.Sprintf("Artist %s does not exist\n", data.Artist), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
credits = append(credits, &model.Credit{
|
||||
Artist: artist,
|
||||
Role: data.Role,
|
||||
Primary: data.Primary,
|
||||
})
|
||||
}
|
||||
|
||||
err = controller.UpdateReleaseCreditsDB(global.DB, release, credits)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update links %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
tx := global.DB.MustBegin()
|
||||
tx.MustExec("DELETE FROM musiccredit WHERE release=$1", release.ID)
|
||||
for artistID := range credits {
|
||||
if credits[artistID].Role == "" {
|
||||
http.Error(w, fmt.Sprintf("Artist role cannot be blank (%s)", artistID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
release.Credits = credits
|
||||
var exists int
|
||||
_ = global.DB.Get(&exists, "SELECT count(*) FROM artist WHERE id=$1", artistID)
|
||||
if exists == 0 {
|
||||
http.Error(w, fmt.Sprintf("Artist %s does not exist\n", artistID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
err = json.NewEncoder(w).Encode(release)
|
||||
tx.MustExec(
|
||||
"INSERT INTO musiccredit "+
|
||||
"(release, artist, role, is_primary) "+
|
||||
"VALUES ($1, $2, $3, $4)",
|
||||
release.ID,
|
||||
artistID,
|
||||
credits[artistID].Role,
|
||||
credits[artistID].Primary)
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update links for %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseLinks(release *model.Release) http.Handler {
|
||||
func UpdateReleaseLinks(release model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
http.NotFound(w, r)
|
||||
|
@ -451,56 +367,31 @@ func UpdateReleaseLinks(release *model.Release) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
err = controller.UpdateReleaseLinksDB(global.DB, release, links)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update links %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
tx := global.DB.MustBegin()
|
||||
tx.MustExec("DELETE FROM musiclink WHERE release=$1", release.ID)
|
||||
for _, link := range links {
|
||||
tx.MustExec(
|
||||
"INSERT INTO musiclink "+
|
||||
"(release, name, url) "+
|
||||
"VALUES ($1, $2, $3)",
|
||||
release.ID,
|
||||
link.Name,
|
||||
link.URL)
|
||||
}
|
||||
|
||||
release.Links = links
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
err = json.NewEncoder(w).Encode(release)
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update links for %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteRelease() http.Handler {
|
||||
func DeleteRelease(release model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var releaseID = r.URL.Path[1:]
|
||||
var release = global.GetRelease(releaseID)
|
||||
if release == nil {
|
||||
http.Error(w, fmt.Sprintf("Release %s does not exist\n", releaseID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := controller.DeleteReleaseDB(global.DB, release)
|
||||
_, err := global.DB.Exec("DELETE FROM musicrelease WHERE id=$1", release.ID)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to delete release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
global.Releases = func () []*model.Release {
|
||||
var releases = []*model.Release{}
|
||||
for _, r := range global.Releases {
|
||||
if r.ID == release.ID { continue }
|
||||
releases = append(releases, r)
|
||||
}
|
||||
return releases
|
||||
}()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(fmt.Sprintf("Release %s has been deleted\n", release.ID)))
|
||||
})
|
||||
}
|
||||
|
|
173
api/track.go
173
api/track.go
|
@ -7,35 +7,32 @@ import (
|
|||
|
||||
"arimelody.me/arimelody.me/global"
|
||||
"arimelody.me/arimelody.me/music/model"
|
||||
controller "arimelody.me/arimelody.me/music/controller"
|
||||
)
|
||||
|
||||
func ServeAllTracks() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// type trackJSON struct {
|
||||
// model.Track
|
||||
// Release string `json:"release"`
|
||||
// }
|
||||
// var tracks = []trackJSON{}
|
||||
//
|
||||
// for _, track := range global.Tracks {
|
||||
// for _, release := range global. {
|
||||
// tracks = append(tracks, {
|
||||
// track,
|
||||
// Release
|
||||
// })
|
||||
// }
|
||||
type track struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
var tracks = []track{}
|
||||
|
||||
err := global.DB.Select(&tracks, "SELECT id, title FROM musictrack")
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to pull tracks from DB: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(global.Tracks)
|
||||
err = json.NewEncoder(w).Encode(tracks)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to serve all tracks: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func ServeTrack() http.Handler {
|
||||
func ServeTrack(track model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
ServeAllTracks().ServeHTTP(w, r)
|
||||
|
@ -43,17 +40,35 @@ func ServeTrack() http.Handler {
|
|||
}
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
var track = global.GetTrack(trackID)
|
||||
if track == nil {
|
||||
var track = model.Track{}
|
||||
err := global.DB.Get(&track, "SELECT * from musictrack WHERE id=$1", trackID)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(track)
|
||||
if err != nil {
|
||||
var releases = []*model.Release{}
|
||||
err = global.DB.Select(&releases,
|
||||
"SELECT * FROM musicrelease JOIN musicreleasetrack AS mrt "+
|
||||
"WHERE mrt.track=$1 "+
|
||||
"ORDER BY release_date",
|
||||
track.ID,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to pull track releases for %s from DB: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
type response struct {
|
||||
model.Track
|
||||
Releases []*model.Release
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(response{ track, releases })
|
||||
if err != nil {
|
||||
fmt.Printf("FATAL: Failed to serve track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -77,122 +92,92 @@ func CreateTrack() http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
trackID, err := controller.CreateTrackDB(global.DB, &track)
|
||||
var trackID string
|
||||
err = global.DB.Get(&trackID,
|
||||
"INSERT INTO musictrack (title, description, lyrics, preview_url) "+
|
||||
"VALUES ($1, $2, $3, $4) "+
|
||||
"RETURNING id",
|
||||
track.Title,
|
||||
track.Description,
|
||||
track.Lyrics,
|
||||
track.PreviewURL)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create track: %s\n", err)
|
||||
fmt.Printf("FATAL: Failed to create track: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
track.ID = trackID
|
||||
global.Tracks = append(global.Tracks, &track)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(track)
|
||||
w.Write([]byte(trackID))
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateTrack() http.Handler {
|
||||
func UpdateTrack(track model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
if r.Method != http.MethodPut || r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var data model.Track
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
var update model.Track
|
||||
err := json.NewDecoder(r.Body).Decode(&update)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update track: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
var track = global.GetTrack(trackID)
|
||||
if track == nil {
|
||||
http.Error(w, fmt.Sprintf("Track %s does not exist\n", trackID), http.StatusBadRequest)
|
||||
if update.Title == "" {
|
||||
http.Error(w, "Track title cannot be empty\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
data.ID = trackID
|
||||
var trackID = r.URL.Path[1:]
|
||||
var track = model.Track{}
|
||||
err = global.DB.Get(&track, "SELECT * from musictrack WHERE id=$1", trackID)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if data.Title == "" { data.Title = track.Title }
|
||||
|
||||
err = controller.UpdateTrackDB(global.DB, &data)
|
||||
_, err = global.DB.Exec(
|
||||
"UPDATE musictrack "+
|
||||
"SET title=$2, description=$3, lyrics=$4, preview_url=$5 "+
|
||||
"WHERE id=$1",
|
||||
track.ID,
|
||||
track.Title,
|
||||
track.Description,
|
||||
track.Lyrics,
|
||||
track.PreviewURL)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update track %s: %s\n", track.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
track.Title = data.Title
|
||||
track.Description = data.Description
|
||||
track.Lyrics = data.Lyrics
|
||||
track.PreviewURL = data.PreviewURL
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(track)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteTrack() http.Handler {
|
||||
func DeleteTrack(track model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete {
|
||||
if r.Method != http.MethodDelete || r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
var track = global.GetTrack(trackID)
|
||||
if track == nil {
|
||||
http.Error(w, fmt.Sprintf("Track %s does not exist\n", trackID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := controller.DeleteTrackDB(global.DB, track)
|
||||
_, err := global.DB.Exec(
|
||||
"DELETE FROM musictrack "+
|
||||
"WHERE id=$1",
|
||||
trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to delete track %s: %s\n", track.ID, err)
|
||||
fmt.Printf("Failed to delete track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// clear track from releases
|
||||
for _, release := range global.Releases {
|
||||
release.Tracks = func () []*model.Track {
|
||||
var tracks = []*model.Track{}
|
||||
for _, t := range release.Tracks {
|
||||
if t.ID == track.ID { continue }
|
||||
tracks = append(tracks, t)
|
||||
}
|
||||
return tracks
|
||||
}()
|
||||
}
|
||||
|
||||
global.Tracks = func () []*model.Track {
|
||||
var tracks = []*model.Track{}
|
||||
for _, t := range global.Tracks {
|
||||
if t.ID == track.ID { continue }
|
||||
tracks = append(tracks, t)
|
||||
}
|
||||
return tracks
|
||||
}()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(fmt.Sprintf("Track %s has been deleted\n", track.ID)))
|
||||
})
|
||||
}
|
||||
|
|
49
api/uploads.go
Normal file
49
api/uploads.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func HandleImageUpload(data *string, directory string, filename string) (string, error) {
|
||||
split := strings.Split(*data, ";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:
|
||||
return "", errors.New("Invalid image type. Allowed: .png, .jpg, .jpeg")
|
||||
}
|
||||
filename = fmt.Sprintf("%s.%s", filename, ext)
|
||||
|
||||
// ensure directory exists
|
||||
os.MkdirAll(directory, os.ModePerm)
|
||||
|
||||
imagePath := filepath.Join(directory, filename)
|
||||
file, err := os.Create(imagePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
buffer := bufio.NewWriter(file)
|
||||
_, err = buffer.Write(imageData)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
if err := buffer.Flush(); err != nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return filename, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue