using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Roadie.Api.Services; using Roadie.Library.Caching; using Roadie.Library.Identity; using Roadie.Library.Utility; using System; using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; namespace Roadie.Api.Controllers { [Produces("application/json")] [Route("play")] [ApiController] [Authorize] public class PlayController : EntityControllerBase { private IPlayActivityService PlayActivityService { get; } private IReleaseService ReleaseService { get; } private ITrackService TrackService { get; } public PlayController(ITrackService trackService, IReleaseService releaseService, IPlayActivityService playActivityService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager userManager) : base(cacheManager, configuration, userManager) { this.Logger = logger.CreateLogger("RoadieApi.Controllers.PlayController"); this.TrackService = trackService; this.PlayActivityService = playActivityService; this.ReleaseService = releaseService; } [HttpGet("release/m3u/{id}.{m3u?}")] public async Task M3uForRelease(Guid id) { var user = await this.CurrentUserModel(); var release = await this.ReleaseService.ById(user, id, new string[1] { "tracks" }); if (release == null || release.IsNotFoundResult) { Response.StatusCode = (int)HttpStatusCode.NotFound; } var m3u = M3uHelper.M3uContentForTracks(release.Data.Medias.SelectMany(x => x.Tracks)); return File(System.Text.Encoding.Default.GetBytes(m3u), "audio/mpeg-url"); } [HttpGet("track/m3u/{id}.{m3u?}")] public async Task M3uForTrack(Guid id) { var user = await this.CurrentUserModel(); var track = await this.TrackService.ById(user, id, null); if (track == null || track.IsNotFoundResult) { Response.StatusCode = (int)HttpStatusCode.NotFound; } var release = await this.ReleaseService.ById(user, Guid.Parse(track.Data.Release.Value), new string[1] { "tracks" }); if (release == null || release.IsNotFoundResult) { Response.StatusCode = (int)HttpStatusCode.NotFound; } var m3u = M3uHelper.M3uContentForTracks(release.Data.Medias.SelectMany(x => x.Tracks).Where(x => x.Id == id)); return File(System.Text.Encoding.Default.GetBytes(m3u), "audio/mpeg-url"); } [HttpGet("track/{id}.{mp3?}")] public async Task StreamTrack(Guid id) { return await base.StreamTrack(id, this.TrackService, this.PlayActivityService); } } }