|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/Quaver/api2/azure" |
| 6 | + "github.com/Quaver/api2/cache" |
| 7 | + "github.com/Quaver/api2/db" |
| 8 | + "github.com/gabriel-vasile/mimetype" |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "gorm.io/gorm" |
| 11 | + "net/http" |
| 12 | + "strconv" |
| 13 | +) |
| 14 | + |
| 15 | +// UploadMusicArtistSong Uploads a new song for a music artist's album |
| 16 | +// Endpoint: POST /v2/artists/album/:id/song |
| 17 | +func UploadMusicArtistSong(c *gin.Context) *APIError { |
| 18 | + user := getAuthedUser(c) |
| 19 | + |
| 20 | + if user == nil { |
| 21 | + return nil |
| 22 | + } |
| 23 | + |
| 24 | + if !canUserAccessAdminRoute(c) { |
| 25 | + return nil |
| 26 | + } |
| 27 | + |
| 28 | + id, err := strconv.Atoi(c.Param("id")) |
| 29 | + |
| 30 | + if err != nil { |
| 31 | + return APIErrorBadRequest("Invalid id") |
| 32 | + } |
| 33 | + |
| 34 | + body := struct { |
| 35 | + Name string `form:"name" json:"name" binding:"required"` |
| 36 | + BPM int `form:"bpm" json:"bpm" binding:"required"` |
| 37 | + Length int `form:"length" json:"length" binding:"required"` |
| 38 | + }{} |
| 39 | + |
| 40 | + if err := c.ShouldBind(&body); err != nil { |
| 41 | + return APIErrorBadRequest("Invalid request body") |
| 42 | + } |
| 43 | + |
| 44 | + album, err := db.GetMusicArtistAlbumById(id) |
| 45 | + |
| 46 | + if err != nil && err != gorm.ErrRecordNotFound { |
| 47 | + return APIErrorServerError("Error retrieving album from db", err) |
| 48 | + } |
| 49 | + |
| 50 | + if album == nil { |
| 51 | + return APIErrorNotFound("Album") |
| 52 | + } |
| 53 | + |
| 54 | + fileBytes, apiErr := validateUploadedAudio(c) |
| 55 | + |
| 56 | + if apiErr != nil { |
| 57 | + return apiErr |
| 58 | + } |
| 59 | + |
| 60 | + song := db.MusicArtistSong{ |
| 61 | + AlbumId: id, |
| 62 | + Name: body.Name, |
| 63 | + Length: body.Length, |
| 64 | + BPM: body.BPM, |
| 65 | + SortOrder: len(album.Songs), |
| 66 | + } |
| 67 | + |
| 68 | + if err := db.SQL.Create(&song).Error; err != nil { |
| 69 | + return APIErrorServerError("Error inserting song into db", err) |
| 70 | + } |
| 71 | + |
| 72 | + _ = cache.RemoveCacheServerMusicArtistSong(song.Id) |
| 73 | + |
| 74 | + if err := azure.Client.UploadFile("music-artist-songs", fmt.Sprintf("%v.mp3", song.Id), fileBytes); err != nil { |
| 75 | + return APIErrorServerError("Error uploading song to azure", err) |
| 76 | + } |
| 77 | + |
| 78 | + c.JSON(http.StatusOK, gin.H{ |
| 79 | + "message": "Your song has been successfully uploaded.", |
| 80 | + "song": song, |
| 81 | + }) |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// Validates an uploaded audio file and returns the file bytes |
| 87 | +func validateUploadedAudio(c *gin.Context) ([]byte, *APIError) { |
| 88 | + fileHeader, _ := c.FormFile("audio") |
| 89 | + |
| 90 | + if fileHeader == nil { |
| 91 | + return nil, APIErrorBadRequest("You must provide a valid `audio` file.") |
| 92 | + } |
| 93 | + |
| 94 | + file, err := fileHeader.Open() |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + return nil, APIErrorServerError("Error opening file", err) |
| 98 | + } |
| 99 | + |
| 100 | + defer file.Close() |
| 101 | + |
| 102 | + var fileBytes = make([]byte, fileHeader.Size) |
| 103 | + |
| 104 | + if _, err = file.Read(fileBytes); err != nil { |
| 105 | + return nil, APIErrorServerError("Error reading file", err) |
| 106 | + } |
| 107 | + |
| 108 | + if mime := mimetype.Detect(fileBytes); mime == nil || mime.String() != "audio/mpeg" { |
| 109 | + return nil, APIErrorBadRequest("The file you provided was not a valid .mp3 file.") |
| 110 | + } |
| 111 | + |
| 112 | + return fileBytes, nil |
| 113 | +} |
0 commit comments