Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
158 lines
5.7 KiB
Go
158 lines
5.7 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/spotifyimport"
|
|
)
|
|
|
|
// spotifyImportPath is the route of the import of a Spotify export.
|
|
const spotifyImportPath = "/api/v1/me/import/spotify"
|
|
|
|
// importMaxBytes bounds the upload of an export (the zip, or its JSON files), which is far larger than any other
|
|
// request: ten years of listening are a few hundred megabytes. It is a variable so that a test can lower it.
|
|
var importMaxBytes int64 = 512 << 20
|
|
|
|
// importFormMemory is how much of an upload is kept in memory while it is read; the rest goes to a temporary file.
|
|
const importFormMemory = 32 << 20
|
|
|
|
// spotifyImportHandlers import the Spotify export of the signed-in person into their own listens and playlists.
|
|
type spotifyImportHandlers struct{ svc spotifyImportService }
|
|
|
|
type spotifyHistoryJSON struct {
|
|
Entries int `json:"entries"`
|
|
Matched int `json:"matched"`
|
|
Imported int `json:"imported"`
|
|
AlreadyThere int `json:"already_there"`
|
|
NotFound int `json:"not_found"`
|
|
Ignored int `json:"ignored"`
|
|
KeptOutside int `json:"kept_outside"`
|
|
}
|
|
|
|
type spotifyPlaylistJSON struct {
|
|
Name string `json:"name"`
|
|
Tracks int `json:"tracks"`
|
|
Matched int `json:"matched"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
type spotifyMissingJSON struct {
|
|
Name string `json:"name"`
|
|
Count int `json:"count"`
|
|
}
|
|
|
|
type spotifyReportJSON struct {
|
|
ID int64 `json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
History spotifyHistoryJSON `json:"history"`
|
|
Playlists []spotifyPlaylistJSON `json:"playlists"`
|
|
Missing struct {
|
|
Total int `json:"total"`
|
|
Artists []spotifyMissingJSON `json:"artists"`
|
|
} `json:"missing"`
|
|
}
|
|
|
|
func toSpotifyReport(run spotifyimport.Run) spotifyReportJSON {
|
|
r := run.Report
|
|
out := spotifyReportJSON{
|
|
ID: run.ID, CreatedAt: run.At.UTC(),
|
|
History: spotifyHistoryJSON{
|
|
Entries: r.History.Entries, Matched: r.History.Matched, Imported: r.History.Imported,
|
|
AlreadyThere: r.History.AlreadyThere, NotFound: r.History.NotFound, Ignored: r.History.Ignored, KeptOutside: r.History.KeptOutside,
|
|
},
|
|
Playlists: make([]spotifyPlaylistJSON, 0, len(r.Playlists)),
|
|
}
|
|
for _, p := range r.Playlists {
|
|
out.Playlists = append(out.Playlists, spotifyPlaylistJSON{Name: p.Name, Tracks: p.Tracks, Matched: p.Matched, Status: p.Status})
|
|
}
|
|
out.Missing.Total = r.MissingTotal
|
|
out.Missing.Artists = make([]spotifyMissingJSON, 0, len(r.Missing))
|
|
for _, m := range r.Missing {
|
|
out.Missing.Artists = append(out.Missing.Artists, spotifyMissingJSON{Name: m.Artist, Count: m.Count})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (h *spotifyImportHandlers) importExport(c *gin.Context) {
|
|
if err := c.Request.ParseMultipartForm(importFormMemory); err != nil {
|
|
var tooBig *http.MaxBytesError
|
|
if errors.As(err, &tooBig) {
|
|
writeError(c, http.StatusRequestEntityTooLarge, "too_large", "these files are too large to import")
|
|
return
|
|
}
|
|
writeError(c, http.StatusBadRequest, "invalid_input", "send the files of the export as a multipart form, in the field files")
|
|
return
|
|
}
|
|
defer func() { _ = c.Request.MultipartForm.RemoveAll() }()
|
|
headers := c.Request.MultipartForm.File["files"]
|
|
if len(headers) == 0 {
|
|
writeError(c, http.StatusBadRequest, "invalid_input", "send the files of the export as a multipart form, in the field files")
|
|
return
|
|
}
|
|
files := make([]spotifyimport.File, 0, len(headers))
|
|
for _, fh := range headers {
|
|
f, err := fh.Open()
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
defer f.Close()
|
|
files = append(files, spotifyimport.File{R: f, Size: fh.Size})
|
|
}
|
|
|
|
run, err := h.svc.Import(c.Request.Context(), currentUserID(c), files)
|
|
switch {
|
|
case err == nil:
|
|
c.JSON(http.StatusOK, toSpotifyReport(run))
|
|
case errors.Is(err, spotifyimport.ErrNotKept):
|
|
// The listens are recorded, so say so; the report just will not be on the page of past imports.
|
|
_ = c.Error(err)
|
|
c.JSON(http.StatusOK, toSpotifyReport(run))
|
|
case errors.Is(err, spotifyimport.ErrNothingFound):
|
|
writeError(c, http.StatusBadRequest, "no_spotify_data", "these files hold no streaming history and no playlists of Spotify")
|
|
case errors.Is(err, spotifyimport.ErrUnreadable):
|
|
writeError(c, http.StatusBadRequest, "unreadable_file", "a file is damaged or cut short")
|
|
case errors.Is(err, spotifyimport.ErrTooBig):
|
|
writeError(c, http.StatusRequestEntityTooLarge, "too_large", "these files hold more than can be imported")
|
|
case errors.Is(err, spotifyimport.ErrBusy):
|
|
writeError(c, http.StatusConflict, "import_running", "an import of yours is still running")
|
|
default:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
}
|
|
}
|
|
|
|
type spotifyImportPageJSON struct {
|
|
Items []spotifyReportJSON `json:"items"`
|
|
Total int `json:"total"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
}
|
|
|
|
// list gives the imports of the signed-in person that were kept, newest first.
|
|
func (h *spotifyImportHandlers) list(c *gin.Context) {
|
|
page, ok := pageOf(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
got, err := h.svc.History(c.Request.Context(), currentUserID(c), page.Limit, page.Offset)
|
|
switch {
|
|
case errors.Is(err, spotifyimport.ErrInvalidPage):
|
|
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
|
return
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
out := spotifyImportPageJSON{Items: make([]spotifyReportJSON, 0, len(got.Items)), Total: got.Total, Limit: got.Limit, Offset: got.Offset}
|
|
for _, run := range got.Items {
|
|
out.Items = append(out.Items, toSpotifyReport(run))
|
|
}
|
|
c.JSON(http.StatusOK, out)
|
|
}
|