package api import ( "bufio" "encoding/base64" "encoding/json" "fmt" "io/fs" "net/http" "os" "path/filepath" "strings" "time" "arimelody.me/arimelody.me/admin" "arimelody.me/arimelody.me/global" controller "arimelody.me/arimelody.me/music/controller" "arimelody.me/arimelody.me/music/model" ) type releaseBodyJSON struct { ID string `json:"id"` Visible *bool `json:"visible"` Title *string `json:"title"` Description *string `json:"description"` ReleaseType *model.ReleaseType `json:"type"` ReleaseDate *string `json:"releaseDate"` Artwork *string `json:"artwork"` Buyname *string `json:"buyname"` Buylink *string `json:"buylink"` } func ServeCatalog() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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{} authorised := admin.GetSession(r) != nil for _, release := range global.Releases { if !release.Visible && !authorised { continue } 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) if err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } }) } func CreateRelease() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { 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 == "" { http.Error(w, "Release ID cannot be empty\n", http.StatusBadRequest) return } title := data.ID if data.Title != nil && *data.Title != "" { title = *data.Title } 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 } var release = model.Release{ ID: data.ID, Visible: false, Title: title, Description: description, ReleaseType: releaseType, ReleaseDate: releaseDate, Artwork: artwork, Buyname: buyname, Buylink: buylink, Links: []*model.Link{}, Credits: []*model.Credit{}, Tracks: []*model.Track{}, } err = controller.CreateReleaseDB(global.DB, &release) if err != nil { 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 { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } }) } func UpdateRelease() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { http.NotFound(w, r) return } 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) } 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 { 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 } return } if len(segments) == 2 { switch segments[1] { case "tracks": UpdateReleaseTracks(release).ServeHTTP(w, r) case "credits": UpdateReleaseCredits(release).ServeHTTP(w, r) case "links": UpdateReleaseLinks(release).ServeHTTP(w, r) } 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 { http.NotFound(w, r) return } var trackIDs = []string{} err := json.NewDecoder(r.Body).Decode(&trackIDs) if err != nil { http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) 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 } err = controller.UpdateReleaseTracksDB(global.DB, release, new_tracks) 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 { 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) 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 } 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 } release.Credits = credits 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 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) return } var links = []*model.Link{} err := json.NewDecoder(r.Body).Decode(&links) if err != nil { http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) 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 } release.Links = links 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 DeleteRelease() 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) 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))) }) }