mirror of
https://github.com/sphildreth/roadie
synced 2024-11-10 14:54:11 +00:00
Added playcontroller.
This commit is contained in:
parent
0409f01039
commit
30dad4ae45
22 changed files with 574 additions and 36 deletions
|
@ -39,7 +39,7 @@ namespace Roadie.Api.Controllers
|
|||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(200)]
|
||||
[ProducesResponseType(404)]
|
||||
public async Task<ActionResult<Artist>> Get(Guid id, string inc = null)
|
||||
public async Task<IActionResult> Get(Guid id, string inc = null)
|
||||
{
|
||||
var result = await this.ArtistService.ById(await this.CurrentUserModel(), id, (inc ?? models.Artist.DefaultIncludes).ToLower().Split(","));
|
||||
if (result == null || result.IsNotFoundResult)
|
||||
|
|
72
RoadieApi/Controllers/PlayController.cs
Normal file
72
RoadieApi/Controllers/PlayController.cs
Normal file
|
@ -0,0 +1,72 @@
|
|||
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 System;
|
||||
using System.IO;
|
||||
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 ITrackService TrackService { get; }
|
||||
|
||||
public PlayController(ITrackService trackService, IPlayActivityService playActivityService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager<ApplicationUser> userManager)
|
||||
: base(cacheManager, configuration, userManager)
|
||||
{
|
||||
this._logger = logger.CreateLogger("RoadieApi.Controllers.PlayController");
|
||||
this.TrackService = trackService;
|
||||
this.PlayActivityService = playActivityService;
|
||||
}
|
||||
|
||||
[HttpGet("track/{id}")]
|
||||
public async Task<FileStreamResult> StreamTrack(Guid id)
|
||||
{
|
||||
var track = await this.TrackService.ById(await this.CurrentUserModel(), id, null);
|
||||
if (track == null || track.IsNotFoundResult)
|
||||
{
|
||||
Response.StatusCode = (int)HttpStatusCode.NotFound;
|
||||
}
|
||||
var info = await this.TrackService.TrackStreamInfo(id,
|
||||
Services.TrackService.DetermineByteStartFromHeaders(this.Request.Headers),
|
||||
Services.TrackService.DetermineByteEndFromHeaders(this.Request.Headers, track.Data.FileSize));
|
||||
if (!info.IsSuccess)
|
||||
{
|
||||
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||
}
|
||||
Response.Headers.Add("Content-Disposition", info.Data.ContentDisposition);
|
||||
Response.Headers.Add("X-Content-Duration", info.Data.ContentDuration);
|
||||
if (!info.Data.IsFullRequest)
|
||||
{
|
||||
Response.Headers.Add("Accept-Ranges", info.Data.AcceptRanges);
|
||||
Response.Headers.Add("Content-Range", info.Data.ContentRange);
|
||||
}
|
||||
Response.Headers.Add("Content-Length", info.Data.ContentLength);
|
||||
Response.ContentType = info.Data.ContentType;
|
||||
Response.StatusCode = info.Data.IsFullRequest ? (int)HttpStatusCode.OK : (int)HttpStatusCode.PartialContent;
|
||||
Response.Headers.Add("Last-Modified", info.Data.LastModified);
|
||||
Response.Headers.Add("ETag", info.Data.Etag);
|
||||
Response.Headers.Add("Cache-Control", info.Data.CacheControl);
|
||||
Response.Headers.Add("Expires", info.Data.Expires);
|
||||
var stream = new MemoryStream(info.Data.Bytes);
|
||||
await this.PlayActivityService.CreatePlayActivity(await this.CurrentUserModel(), info.Data);
|
||||
this._logger.LogInformation($"StreamTrack [{ info.Data.ToString() }]");
|
||||
return new FileStreamResult(stream, info.Data.ContentType)
|
||||
{
|
||||
FileDownloadName = info.Data.FileName
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -39,7 +39,7 @@ namespace Roadie.Api.Controllers
|
|||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(200)]
|
||||
[ProducesResponseType(404)]
|
||||
public async Task<ActionResult<Artist>> Get(Guid id, string inc = null)
|
||||
public async Task<IActionResult> Get(Guid id, string inc = null)
|
||||
{
|
||||
var result = await this.ReleaseService.ById(await this.CurrentUserModel(), id, (inc ?? models.Releases.Release.DefaultIncludes).ToLower().Split(","));
|
||||
if (result == null || result.IsNotFoundResult)
|
||||
|
|
|
@ -6,9 +6,12 @@ using Microsoft.Extensions.Logging;
|
|||
using Roadie.Api.Services;
|
||||
using Roadie.Library.Caching;
|
||||
using Roadie.Library.Identity;
|
||||
using Roadie.Library.Models;
|
||||
using Roadie.Library.Models.Pagination;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using models = Roadie.Library.Models;
|
||||
|
||||
namespace Roadie.Api.Controllers
|
||||
{
|
||||
|
@ -33,27 +36,22 @@ namespace Roadie.Api.Controllers
|
|||
// return Ok(this._RoadieDbContext.Tracks.ProjectToType<models.Track>());
|
||||
//}
|
||||
|
||||
//[HttpGet("{id}")]
|
||||
//[ProducesResponseType(200)]
|
||||
//[ProducesResponseType(404)]
|
||||
//public IActionResult Get(Guid id)
|
||||
//{
|
||||
// var key = id.ToString();
|
||||
// var result = this._cacheManager.Get<models.Track>(key, () =>
|
||||
// {
|
||||
// var d = this._RoadieDbContext.Tracks.FirstOrDefault(x => x.RoadieId == id);
|
||||
// if (d != null)
|
||||
// {
|
||||
// return d.Adapt<models.Track>();
|
||||
// }
|
||||
// return null;
|
||||
// }, key);
|
||||
// if (result == null)
|
||||
// {
|
||||
// return NotFound();
|
||||
// }
|
||||
// return Ok(result);
|
||||
//}
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(200)]
|
||||
[ProducesResponseType(404)]
|
||||
public async Task<IActionResult> Get(Guid id, string inc = null)
|
||||
{
|
||||
var result = await this.TrackService.ById(await this.CurrentUserModel(), id, (inc ?? models.Track.DefaultIncludes).ToLower().Split(","));
|
||||
if (result == null || result.IsNotFoundResult)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
return StatusCode((int)HttpStatusCode.InternalServerError);
|
||||
}
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(200)]
|
||||
|
|
|
@ -92,7 +92,6 @@ namespace Roadie.Api.Services
|
|||
}
|
||||
var result = artist.Adapt<Artist>();
|
||||
result.Thumbnail = base.MakeArtistThumbnailImage(id);
|
||||
|
||||
result.Genres = artist.Genres.Select(x => new DataToken { Text = x.Genre.Name, Value = x.Genre.RoadieId.ToString() });
|
||||
if (includes != null && includes.Any())
|
||||
{
|
||||
|
@ -271,6 +270,7 @@ namespace Roadie.Api.Services
|
|||
orderby l.SortName
|
||||
select new LabelList
|
||||
{
|
||||
Id = rl.RoadieId,
|
||||
Label = new DataToken
|
||||
{
|
||||
Text = l.Name,
|
||||
|
|
|
@ -24,7 +24,7 @@ namespace Roadie.Api.Services
|
|||
IHttpContext httpContext,
|
||||
data.IRoadieDbContext context,
|
||||
ICacheManager cacheManager,
|
||||
ILogger<ArtistService> logger,
|
||||
ILogger<BookmarkService> logger,
|
||||
ICollectionService collectionService,
|
||||
IPlaylistService playlistService)
|
||||
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
|
||||
|
|
|
@ -23,7 +23,7 @@ namespace Roadie.Api.Services
|
|||
IHttpContext httpContext,
|
||||
data.IRoadieDbContext dbContext,
|
||||
ICacheManager cacheManager,
|
||||
ILogger<StatisticsService> logger)
|
||||
ILogger<GenreService> logger)
|
||||
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Roadie.Library.Models;
|
||||
using Roadie.Library;
|
||||
using Roadie.Library.Models;
|
||||
using Roadie.Library.Models.Pagination;
|
||||
using Roadie.Library.Models.Users;
|
||||
using System.Threading.Tasks;
|
||||
|
@ -8,5 +9,7 @@ namespace Roadie.Api.Services
|
|||
public interface IPlayActivityService
|
||||
{
|
||||
Task<PagedResult<PlayActivityList>> List(PagedRequest request, User roadieUser = null);
|
||||
|
||||
Task<OperationResult<UserTrack>> CreatePlayActivity(User roadieUser, TrackStreamInfo streamInfo);
|
||||
}
|
||||
}
|
|
@ -1,13 +1,19 @@
|
|||
using Roadie.Library.Models;
|
||||
using Roadie.Library;
|
||||
using Roadie.Library.Models;
|
||||
using Roadie.Library.Models.Pagination;
|
||||
using Roadie.Library.Models.Users;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Roadie.Api.Services
|
||||
{
|
||||
public interface ITrackService
|
||||
{
|
||||
Task<OperationResult<Track>> ById(User roadieUser, Guid id, IEnumerable<string> includes);
|
||||
|
||||
Task<Library.Models.Pagination.PagedResult<TrackList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, Guid? releaseId = null);
|
||||
|
||||
Task<OperationResult<TrackStreamInfo>> TrackStreamInfo(Guid trackId, long beginBytes, long endBytes);
|
||||
}
|
||||
}
|
|
@ -23,7 +23,7 @@ namespace Roadie.Api.Services
|
|||
IHttpContext httpContext,
|
||||
data.IRoadieDbContext context,
|
||||
ICacheManager cacheManager,
|
||||
ILogger<ArtistService> logger,
|
||||
ILogger<LabelService> logger,
|
||||
ICollectionService collectionService,
|
||||
IPlaylistService playlistService)
|
||||
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Roadie.Library;
|
||||
using Roadie.Library.Caching;
|
||||
using Roadie.Library.Configuration;
|
||||
using Roadie.Library.Encoding;
|
||||
|
@ -23,7 +25,7 @@ namespace Roadie.Api.Services
|
|||
IHttpContext httpContext,
|
||||
data.IRoadieDbContext dbContext,
|
||||
ICacheManager cacheManager,
|
||||
ILogger<StatisticsService> logger)
|
||||
ILogger<PlayActivityService> logger)
|
||||
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
|
||||
{
|
||||
}
|
||||
|
@ -104,5 +106,96 @@ namespace Roadie.Api.Services
|
|||
}
|
||||
return new Library.Models.Pagination.PagedResult<PlayActivityList>();
|
||||
}
|
||||
|
||||
public async Task<OperationResult<UserTrack>> CreatePlayActivity(User roadieUser, TrackStreamInfo streamInfo)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
|
||||
var track = this.GetTrack(streamInfo.Track.Value);
|
||||
if (track == null)
|
||||
{
|
||||
return new OperationResult<UserTrack>($"CreatePlayActivity: Unable To Find Track [{ streamInfo.Track.Value }]");
|
||||
}
|
||||
if (!track.IsValid)
|
||||
{
|
||||
return new OperationResult<UserTrack>($"CreatePlayActivity: Invalid Track. Track Id [{streamInfo.Track.Value}], FilePath [{track.FilePath}], Filename [{track.FileName}]");
|
||||
}
|
||||
var user = this.GetUser(roadieUser.UserId);
|
||||
if (user == null)
|
||||
{
|
||||
return new OperationResult<UserTrack>($"CreatePlayActivity: Unable To Find User [{ roadieUser.UserId }]");
|
||||
}
|
||||
var now = DateTime.UtcNow;
|
||||
track.PlayedCount = (track.PlayedCount ?? 0) + 1;
|
||||
var userTrack = user.TrackRatings.FirstOrDefault(x => x.TrackId == track.Id);
|
||||
if (userTrack == null)
|
||||
{
|
||||
userTrack = new data.UserTrack(now)
|
||||
{
|
||||
UserId = user.Id,
|
||||
TrackId = track.Id
|
||||
};
|
||||
this.DbContext.UserTracks.Add(userTrack);
|
||||
}
|
||||
userTrack.LastPlayed = now;
|
||||
userTrack.PlayedCount++;
|
||||
|
||||
this.CacheManager.ClearRegion(user.CacheRegion);
|
||||
this.CacheManager.ClearRegion(track.CacheRegion);
|
||||
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion);
|
||||
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion);
|
||||
|
||||
// TODO publish with SignalR
|
||||
|
||||
//if (!this.RoadieUser.isPrivate ?? false)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// var hub = GlobalHost.ConnectionManager.GetHubContext<Hubs.PlayActivityHub>();
|
||||
// var releaseArtist = track.releasemedia.release.artist;
|
||||
// artist trackArtist = track.artistId == null ? null : context.artists.FirstOrDefault(x => x.id == track.artistId);
|
||||
// hub.Clients.All.PlayActivity(new PlayActivityListModel
|
||||
// {
|
||||
// releaseTitle = track.releasemedia.release.title,
|
||||
// playedDateDateTime = userTrack.lastPlayed,
|
||||
// userId = this.RoadieUser.roadieId,
|
||||
// userName = this.RoadieUser.username,
|
||||
// releaseId = track.releasemedia.release.roadieId,
|
||||
// trackId = track.roadieId,
|
||||
// IsLocked = (track.isLocked ?? false) || (track.releasemedia.release.isLocked ?? false) || ((trackArtist ?? releaseArtist).isLocked ?? false),
|
||||
// createdDateTime = track.createdDate,
|
||||
// lastUpdatedDateTime = track.lastUpdated,
|
||||
// releasePlayUrl = this.Request.Url.BasePath + "/play/release/" + this.Base64BearerToken + "/" + track.releasemedia.release.roadieId,
|
||||
// rating = track.rating,
|
||||
// userRating = userTrack.rating,
|
||||
// releaseArtistId = releaseArtist.roadieId,
|
||||
// releaseArtistName = releaseArtist.name,
|
||||
// roadieId = track.roadieId,
|
||||
// status = track.status.ToString(),
|
||||
// title = track.title,
|
||||
// trackArtistId = trackArtist == null ? null : trackArtist.roadieId,
|
||||
// trackArtistName = trackArtist == null ? null : trackArtist.name,
|
||||
// trackPlayUrl = this.Request.Url.BasePath + "/play/track/" + this.Base64BearerToken + "/" + track.roadieId,
|
||||
// artistThumbnailUrl = this.Request.Url.BasePath + "/api/v1/image/artist/thumbnail/" + (trackArtist != null ? trackArtist.roadieId : releaseArtist.roadieId),
|
||||
// releaseThumbnailUrl = this.Request.Url.BasePath + "/api/v1/image/release/thumbnail/" + track.releasemedia.release.roadieId,
|
||||
// userThumbnailUrl = this.Request.Url.BasePath + "/api/v1/image/user/thumbnail/" + this.RoadieUser.roadieId
|
||||
// });
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// this.LoggingService.Error(ex.Serialize());
|
||||
// }
|
||||
//}
|
||||
|
||||
await this.DbContext.SaveChangesAsync();
|
||||
sw.Stop();
|
||||
return new OperationResult<UserTrack>
|
||||
{
|
||||
Data = userTrack.Adapt<UserTrack>(),
|
||||
IsSuccess = userTrack != null,
|
||||
OperationTime = sw.ElapsedMilliseconds
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -133,11 +133,25 @@ namespace Roadie.Api.Services
|
|||
}, data.Release.CacheRegionUrn(id));
|
||||
}
|
||||
|
||||
protected data.Track GetTrack(string id)
|
||||
{
|
||||
Guid trackId = Guid.Empty;
|
||||
if(Guid.TryParse(id, out trackId))
|
||||
{
|
||||
return this.GetTrack(trackId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected data.Track GetTrack(Guid id)
|
||||
{
|
||||
return this.CacheManager.Get(data.Track.CacheUrn(id), () =>
|
||||
{
|
||||
return this.DbContext.Tracks
|
||||
.Include(x => x.ReleaseMedia)
|
||||
.Include(x => x.ReleaseMedia.Release)
|
||||
.Include(x => x.ReleaseMedia.Release.Artist)
|
||||
.Include(x => x.TrackArtist)
|
||||
.FirstOrDefault(x => x.RoadieId == id);
|
||||
}, data.Track.CacheRegionUrn(id));
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ namespace Roadie.Api.Services
|
|||
IHttpContext httpContext,
|
||||
data.IRoadieDbContext context,
|
||||
ICacheManager cacheManager,
|
||||
ILogger<ArtistService> logger,
|
||||
ILogger<SubsonicService> logger,
|
||||
ICollectionService collectionService,
|
||||
IPlaylistService playlistService)
|
||||
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Roadie.Library;
|
||||
using Roadie.Library.Caching;
|
||||
using Roadie.Library.Configuration;
|
||||
using Roadie.Library.Encoding;
|
||||
using Roadie.Library.Extensions;
|
||||
using Roadie.Library.Models;
|
||||
using Roadie.Library.Models.Pagination;
|
||||
using Roadie.Library.Models.Users;
|
||||
|
@ -9,6 +13,7 @@ using Roadie.Library.Utility;
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Linq.Dynamic.Core;
|
||||
using System.Threading.Tasks;
|
||||
|
@ -23,11 +28,194 @@ namespace Roadie.Api.Services
|
|||
IHttpContext httpContext,
|
||||
data.IRoadieDbContext dbContext,
|
||||
ICacheManager cacheManager,
|
||||
ILogger<StatisticsService> logger)
|
||||
ILogger<TrackService> logger)
|
||||
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<OperationResult<Track>> ById(User roadieUser, Guid id, IEnumerable<string> includes)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
sw.Start();
|
||||
var cacheKey = string.Format("urn:track_by_id_operation:{0}:{1}", id, includes == null ? "0" : string.Join("|", includes));
|
||||
var result = await this.CacheManager.GetAsync<OperationResult<Track>>(cacheKey, async () =>
|
||||
{
|
||||
return await this.TrackByIdAction(id, includes);
|
||||
}, data.Track.CacheRegionUrn(id));
|
||||
if (result?.Data != null && roadieUser != null)
|
||||
{
|
||||
//var artist = this.GetArtist(id);
|
||||
//result.Data.UserBookmark = this.GetUserBookmarks(roadieUser).FirstOrDefault(x => x.Type == BookmarkType.Artist && x.Bookmark.Value == artist.RoadieId.ToString());
|
||||
//var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == roadieUser.Id);
|
||||
//if (userArtist != null)
|
||||
//{
|
||||
// result.Data.UserRating = new UserArtist
|
||||
// {
|
||||
// IsDisliked = userArtist.IsDisliked ?? false,
|
||||
// IsFavorite = userArtist.IsFavorite ?? false,
|
||||
// Rating = userArtist.Rating
|
||||
// };
|
||||
//}
|
||||
|
||||
//if (this.RoadieUser != null)
|
||||
//{
|
||||
// var userTrack = context.usertracks.FirstOrDefault(x => x.trackId == trackInfo.t.id && x.userId == this.RoadieUser.id);
|
||||
// if (userTrack != null)
|
||||
// {
|
||||
// result.UserTrack = Map.ObjectToObject<dto.UserTrack>(userTrack);
|
||||
// result.UserTrack.userId = this.RoadieUser.roadieId;
|
||||
// result.UserTrack.trackId = result.roadieId;
|
||||
// result.UserTrack.createdDateTime = userTrack.createdDate;
|
||||
// result.UserTrack.lastUpdatedDateTime = userTrack.lastUpdated;
|
||||
// }
|
||||
//}
|
||||
|
||||
}
|
||||
sw.Stop();
|
||||
return new OperationResult<Track>(result.Messages)
|
||||
{
|
||||
Data = result?.Data,
|
||||
Errors = result?.Errors,
|
||||
IsNotFoundResult = result?.IsNotFoundResult ?? false,
|
||||
IsSuccess = result?.IsSuccess ?? false,
|
||||
OperationTime = sw.ElapsedMilliseconds
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<OperationResult<Track>> TrackByIdAction(Guid id, IEnumerable<string> includes)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
sw.Start();
|
||||
|
||||
var track = this.GetTrack(id);
|
||||
|
||||
if (track == null)
|
||||
{
|
||||
return new OperationResult<Track>(true, string.Format("Track Not Found [{0}]", id));
|
||||
}
|
||||
var result = track.Adapt<Track>();
|
||||
result.PlayUrl = $"{ this.HttpContext.BaseUrl }/play/track/{track.RoadieId}";
|
||||
result.IsLocked = (track.IsLocked ?? false) ||
|
||||
(track.ReleaseMedia.IsLocked ?? false) ||
|
||||
(track.ReleaseMedia.Release.IsLocked ?? false ) ||
|
||||
(track.ReleaseMedia.Release.Artist.IsLocked ?? false);
|
||||
result.Thumbnail = base.MakeTrackThumbnailImage(id);
|
||||
result.ReleaseMediaId = track.ReleaseMedia.RoadieId.ToString();
|
||||
result.Artist = new DataToken
|
||||
{
|
||||
Text = track.ReleaseMedia.Release.Artist.Name,
|
||||
Value = track.ReleaseMedia.Release.Artist.RoadieId.ToString()
|
||||
};
|
||||
result.ArtistThumbnail = this.MakeArtistThumbnailImage(track.ReleaseMedia.Release.Artist.RoadieId);
|
||||
result.Release = new DataToken
|
||||
{
|
||||
Text = track.ReleaseMedia.Release.Title,
|
||||
Value = track.ReleaseMedia.Release.RoadieId.ToString()
|
||||
};
|
||||
result.ReleaseThumbnail = this.MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId);
|
||||
if(track.ArtistId.HasValue)
|
||||
{
|
||||
var trackArtist = this.DbContext.Artists.FirstOrDefault(x => x.Id == track.ArtistId);
|
||||
if(trackArtist == null)
|
||||
{
|
||||
this.Logger.LogWarning($"Unable to find Track Artist [{ track.ArtistId }");
|
||||
}
|
||||
else
|
||||
{
|
||||
result.TrackArtist = new DataToken
|
||||
{
|
||||
Text = trackArtist.Name,
|
||||
Value = trackArtist.RoadieId.ToString()
|
||||
};
|
||||
result.TrackArtistThumbnail = this.MakeArtistThumbnailImage(trackArtist.RoadieId);
|
||||
}
|
||||
}
|
||||
if (includes != null && includes.Any())
|
||||
{
|
||||
if (includes.Contains("stats"))
|
||||
{
|
||||
var userTracks = (from t in this.DbContext.Tracks
|
||||
join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId into tt
|
||||
from ut in tt.DefaultIfEmpty()
|
||||
where t.Id == track.Id
|
||||
select ut).ToArray();
|
||||
if (userTracks.Any())
|
||||
{
|
||||
result.Statistics = new Library.Models.Statistics.TrackStatistics
|
||||
{
|
||||
DislikedCount = userTracks.Count(x => x.IsDisliked ?? false),
|
||||
FavoriteCount = userTracks.Count(x => x.IsFavorite ?? false),
|
||||
PlayedCount = userTracks.Sum(x => x.PlayedCount),
|
||||
FileSizeFormatted = ((long?)track.FileSize).ToFileSize(),
|
||||
Time = TimeSpan.FromSeconds(Math.Floor((double)track.Duration / 1000)).ToString(@"hh\:mm\:ss")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sw.Stop();
|
||||
return new OperationResult<Track>
|
||||
{
|
||||
Data = result,
|
||||
IsSuccess = result != null,
|
||||
OperationTime = sw.ElapsedMilliseconds
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static long DetermineByteEndFromHeaders(IHeaderDictionary headers, long fileLength)
|
||||
{
|
||||
var defaultFileLength = fileLength - 1;
|
||||
if (headers == null || !headers.Any(x => x.Key == "Range"))
|
||||
{
|
||||
return defaultFileLength;
|
||||
}
|
||||
long? result = null;
|
||||
var rangeHeader = headers["Range"];
|
||||
string rangeEnd = null;
|
||||
var rangeBegin = rangeHeader.FirstOrDefault();
|
||||
if (!string.IsNullOrEmpty(rangeBegin))
|
||||
{
|
||||
//bytes=0-
|
||||
rangeBegin = rangeBegin.Replace("bytes=", "");
|
||||
var parts = rangeBegin.Split('-');
|
||||
rangeBegin = parts[0];
|
||||
if (parts.Length > 1)
|
||||
{
|
||||
rangeEnd = parts[1];
|
||||
}
|
||||
if (!string.IsNullOrEmpty(rangeEnd))
|
||||
{
|
||||
result = long.TryParse(rangeEnd, out long outValue) ? (int?)outValue : null;
|
||||
}
|
||||
}
|
||||
return result ?? defaultFileLength;
|
||||
}
|
||||
|
||||
public static long DetermineByteStartFromHeaders(IHeaderDictionary headers)
|
||||
{
|
||||
if (headers == null || !headers.Any(x => x.Key == "Range"))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
long result = 0;
|
||||
var rangeHeader = headers["Range"];
|
||||
var rangeBegin = rangeHeader.FirstOrDefault();
|
||||
if (!string.IsNullOrEmpty(rangeBegin))
|
||||
{
|
||||
//bytes=0-
|
||||
rangeBegin = rangeBegin.Replace("bytes=", "");
|
||||
var parts = rangeBegin.Split('-');
|
||||
rangeBegin = parts[0];
|
||||
if (!string.IsNullOrEmpty(rangeBegin))
|
||||
{
|
||||
long.TryParse(rangeBegin, out result);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Library.Models.Pagination.PagedResult<TrackList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, Guid? releaseId = null)
|
||||
{
|
||||
var sw = new Stopwatch();
|
||||
|
@ -169,5 +357,77 @@ namespace Roadie.Api.Services
|
|||
Rows = rows
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<OperationResult<TrackStreamInfo>> TrackStreamInfo(Guid trackId, long beginBytes, long endBytes)
|
||||
{
|
||||
var track = this.GetTrack(trackId);
|
||||
if (track == null)
|
||||
{
|
||||
return new OperationResult<TrackStreamInfo>($"TrackStreamInfo: Unable To Find Track [{ trackId }]");
|
||||
}
|
||||
if (!track.IsValid)
|
||||
{
|
||||
return new OperationResult<TrackStreamInfo>($"TrackStreamInfo: Invalid Track. Track Id [{trackId}], FilePath [{track.FilePath}], Filename [{track.FileName}]");
|
||||
}
|
||||
string trackPath = null;
|
||||
try
|
||||
{
|
||||
trackPath = track.PathToTrack(this.Configuration, this.Configuration.LibraryFolder);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new OperationResult<TrackStreamInfo>(ex);
|
||||
}
|
||||
var trackFileInfo = new FileInfo(trackPath);
|
||||
if (!trackFileInfo.Exists)
|
||||
{
|
||||
track.UpdateTrackMissingFile();
|
||||
await this.DbContext.SaveChangesAsync();
|
||||
return new OperationResult<TrackStreamInfo>($"TrackStreamInfo: TrackId [{trackId}] Unable to Find Track [{trackFileInfo.FullName}]");
|
||||
}
|
||||
var contentDurationTimeSpan = TimeSpan.FromMilliseconds((double)(track.Duration ?? 0));
|
||||
var info = new TrackStreamInfo
|
||||
{
|
||||
FileName = this.HttpEncoder.UrlEncode(track.FileName).ToContentDispositionFriendly(),
|
||||
ContentDisposition = $"attachment; filename=\"{ this.HttpEncoder.UrlEncode(track.FileName).ToContentDispositionFriendly() }\"",
|
||||
ContentDuration = contentDurationTimeSpan.TotalSeconds.ToString(),
|
||||
};
|
||||
var cacheTimeout = 86400; // 24 hours
|
||||
var contentLength = (endBytes - beginBytes) + 1;
|
||||
info.Track = new DataToken
|
||||
{
|
||||
Text = track.Title,
|
||||
Value = track.RoadieId.ToString()
|
||||
};
|
||||
info.BeginBytes = beginBytes;
|
||||
info.EndBytes = endBytes;
|
||||
info.ContentRange = $"bytes {beginBytes}-{endBytes}/{contentLength}";
|
||||
info.ContentLength = contentLength.ToString();
|
||||
info.IsFullRequest = beginBytes == 0 && endBytes == (trackFileInfo.Length - 1);
|
||||
info.IsEndRangeRequest = beginBytes > 0 && endBytes != (trackFileInfo.Length - 1);
|
||||
info.LastModified = (track.LastUpdated ?? track.CreatedDate).ToString("R");
|
||||
info.Etag = track.Etag;
|
||||
info.CacheControl = $"public, max-age={ cacheTimeout.ToString() } ";
|
||||
info.Expires = DateTime.UtcNow.AddMinutes(cacheTimeout).ToString("R");
|
||||
int bytesToRead = (int)(endBytes - beginBytes) + 1;
|
||||
byte[] trackBytes = new byte[bytesToRead];
|
||||
using (var fs = trackFileInfo.OpenRead())
|
||||
{
|
||||
try
|
||||
{
|
||||
fs.Seek(beginBytes, SeekOrigin.Begin);
|
||||
var r = fs.Read(trackBytes, 0, bytesToRead);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new OperationResult<TrackStreamInfo>(ex);
|
||||
}
|
||||
}
|
||||
info.Bytes = trackBytes;
|
||||
return new OperationResult<TrackStreamInfo>
|
||||
{
|
||||
Data = info
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,6 +34,7 @@ namespace Roadie.Library.Data
|
|||
this.RoadieId = Guid.NewGuid();
|
||||
this.Status = Statuses.Incomplete;
|
||||
this.CreatedDate = DateTime.UtcNow;
|
||||
this.IsLocked = false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,5 +34,17 @@ namespace Roadie.Library.Data
|
|||
[Column("userId")]
|
||||
[Required]
|
||||
public int UserId { get; set; }
|
||||
|
||||
public UserTrack()
|
||||
{ }
|
||||
|
||||
public UserTrack(DateTime? now = null)
|
||||
{
|
||||
PlayedCount = 0;
|
||||
Rating = 0;
|
||||
IsDisliked = false;
|
||||
IsFavorite = false;
|
||||
LastPlayed = now ?? DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,6 +16,14 @@ namespace Roadie.Library.Identity
|
|||
return $"urn:user_by_id:{ Id }";
|
||||
}
|
||||
|
||||
public string CacheRegion
|
||||
{
|
||||
get
|
||||
{
|
||||
return ApplicationUser.CacheRegionUrn(this.RoadieId);
|
||||
}
|
||||
}
|
||||
|
||||
public string CacheKey
|
||||
{
|
||||
get
|
||||
|
|
|
@ -46,5 +46,10 @@ namespace Roadie.Library.Models
|
|||
}
|
||||
}
|
||||
public object Data { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Text [{ this.Text}], Value [{ this.Value }]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ namespace Roadie.Library.Models.Statistics
|
|||
[Serializable]
|
||||
public class TrackStatistics
|
||||
{
|
||||
public string FileSizeFormatted { get; set; }
|
||||
public string Time { get; set; }
|
||||
public int? DislikedCount { get; set; }
|
||||
public int? FavoriteCount { get; set; }
|
||||
public int? PlayedCount { get; set; }
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Newtonsoft.Json;
|
||||
using Roadie.Library.Models.Statistics;
|
||||
using Roadie.Library.Models.Users;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
@ -8,11 +9,15 @@ namespace Roadie.Library.Models
|
|||
[Serializable]
|
||||
public class Track : EntityModelBase
|
||||
{
|
||||
public const string DefaultIncludes = "stats";
|
||||
|
||||
[MaxLength(50)]
|
||||
public string AmgId { get; set; }
|
||||
|
||||
public Guid ArtistId { get; set; }
|
||||
public DataToken Artist { get; set; }
|
||||
|
||||
public Image ArtistThumbnail { get; set; }
|
||||
public long FileSize { get; set; }
|
||||
public int Duration { get; set; }
|
||||
|
||||
[MaxLength(32)]
|
||||
|
@ -33,23 +38,36 @@ namespace Roadie.Library.Models
|
|||
public string PartTitles { get; set; }
|
||||
|
||||
public int PlayedCount { get; set; }
|
||||
|
||||
public short Rating { get; set; }
|
||||
public DataToken Release { get; set; }
|
||||
|
||||
public string ReleaseMediaId { get; set; }
|
||||
|
||||
public Image ReleaseThumbnail { get; set; }
|
||||
|
||||
[MaxLength(100)]
|
||||
public string SpotifyId { get; set; }
|
||||
|
||||
public TrackStatistics Statistics { get; set; }
|
||||
|
||||
public Image Thumbnail { get; set; }
|
||||
|
||||
[MaxLength(250)]
|
||||
[Required]
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Track Artist, not release artist. If this is present then the track has an artist different than the release.
|
||||
/// </summary>
|
||||
public DataToken TrackArtist { get; set; }
|
||||
|
||||
public Image TrackArtistThumbnail { get; set; }
|
||||
|
||||
[Required]
|
||||
public short TrackNumber { get; set; }
|
||||
|
||||
public UserTrack UserRating { get; set; }
|
||||
|
||||
public string PlayUrl { get; set; }
|
||||
}
|
||||
}
|
45
RoadieLibrary/Models/TrackStreamInfo.cs
Normal file
45
RoadieLibrary/Models/TrackStreamInfo.cs
Normal file
|
@ -0,0 +1,45 @@
|
|||
using System;
|
||||
|
||||
namespace Roadie.Library.Models
|
||||
{
|
||||
public sealed class TrackStreamInfo
|
||||
{
|
||||
public DataToken Track { get; set; }
|
||||
public string AcceptRanges
|
||||
{
|
||||
get
|
||||
{
|
||||
return "bytes";
|
||||
}
|
||||
}
|
||||
|
||||
public string CacheControl { get; set; }
|
||||
public string ContentDisposition { get; set; }
|
||||
public string ContentDuration { get; set; }
|
||||
public string ContentLength { get; set; }
|
||||
public string ContentRange { get; set; }
|
||||
|
||||
public string ContentType
|
||||
{
|
||||
get
|
||||
{
|
||||
return "audio/mpeg";
|
||||
}
|
||||
}
|
||||
|
||||
public string Etag { get; set; }
|
||||
public string Expires { get; set; }
|
||||
public bool IsEndRangeRequest { get; set; }
|
||||
public bool IsFullRequest { get; set; }
|
||||
public string LastModified { get; set; }
|
||||
public byte[] Bytes { get; set; }
|
||||
public long BeginBytes { get; set; }
|
||||
public long EndBytes { get; set; }
|
||||
public string FileName { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"TrackId [{ this.Track }], Begin [{ this.BeginBytes }], End [{ this.EndBytes }]";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ namespace Roadie.Library.Models.Users
|
|||
public int? PlayerTrackLimit { get; set; }
|
||||
public int? RecentlyPlayedLimit { get; set; }
|
||||
public int? RandomReleaseLimit { get; set; }
|
||||
public bool IsPrivate { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue