roadie/Roadie.Api.Services/ReleaseService.cs

1020 lines
53 KiB
C#
Raw Normal View History

2018-11-12 00:28:37 +00:00
using Mapster;
2018-12-21 22:59:33 +00:00
using Microsoft.AspNetCore.Http;
2018-11-12 00:28:37 +00:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
2018-11-14 03:19:28 +00:00
using Roadie.Library;
2018-11-12 00:28:37 +00:00
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
using Roadie.Library.Encoding;
2018-12-23 21:36:38 +00:00
using Roadie.Library.Engines;
2018-11-14 03:19:28 +00:00
using Roadie.Library.Enums;
using Roadie.Library.Extensions;
2018-12-23 21:36:38 +00:00
using Roadie.Library.Factories;
2018-12-21 22:59:33 +00:00
using Roadie.Library.Imaging;
2018-12-23 21:36:38 +00:00
using Roadie.Library.MetaData.Audio;
using Roadie.Library.MetaData.FileName;
using Roadie.Library.MetaData.ID3Tags;
using Roadie.Library.MetaData.LastFm;
using Roadie.Library.MetaData.MusicBrainz;
2018-11-12 00:28:37 +00:00
using Roadie.Library.Models;
2018-12-07 04:45:09 +00:00
using Roadie.Library.Models.Collections;
2018-11-12 00:28:37 +00:00
using Roadie.Library.Models.Pagination;
using Roadie.Library.Models.Releases;
2018-11-14 03:19:28 +00:00
using Roadie.Library.Models.Statistics;
2018-11-12 00:28:37 +00:00
using Roadie.Library.Models.Users;
using Roadie.Library.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
2018-11-12 00:28:37 +00:00
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using data = Roadie.Library.Data;
namespace Roadie.Api.Services
{
public class ReleaseService : ServiceBase, IReleaseService
{
2018-12-23 21:36:38 +00:00
private IArtistLookupEngine ArtistLookupEngine { get; }
private IAudioMetaDataHelper AudioMetaDataHelper { get; }
2018-12-08 19:47:19 +00:00
private IBookmarkService BookmarkService { get; } = null;
2018-12-07 04:45:09 +00:00
private ICollectionService CollectionService { get; } = null;
2018-12-23 21:36:38 +00:00
private IFileNameHelper FileNameHelper { get; }
private IID3TagsHelper ID3TagsHelper { get; }
private IImageFactory ImageFactory { get; }
private ILabelFactory LabelFactory { get; }
private ILabelLookupEngine LabelLookupEngine { get; }
private ILastFmHelper LastFmHelper { get; }
private IMusicBrainzProvider MusicBrainzProvider { get; }
2018-12-07 04:45:09 +00:00
private IPlaylistService PlaylistService { get; } = null;
2018-12-23 21:36:38 +00:00
private IReleaseFactory ReleaseFactory { get; }
private IReleaseLookupEngine ReleaseLookupEngine { get; }
2018-12-07 04:45:09 +00:00
2018-11-12 00:28:37 +00:00
public ReleaseService(IRoadieSettings configuration,
IHttpEncoder httpEncoder,
IHttpContext httpContext,
data.IRoadieDbContext dbContext,
ICacheManager cacheManager,
2018-12-07 04:45:09 +00:00
ICollectionService collectionService,
IPlaylistService playlistService,
ILogger<ReleaseService> logger,
IBookmarkService bookmarkService)
2018-11-12 00:28:37 +00:00
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
{
2018-12-07 04:45:09 +00:00
this.CollectionService = collectionService;
this.PlaylistService = playlistService;
this.BookmarkService = bookmarkService;
2018-12-23 21:36:38 +00:00
this.MusicBrainzProvider = new MusicBrainzProvider(configuration, cacheManager, logger);
this.LastFmHelper = new LastFmHelper(configuration, cacheManager, logger, dbContext, httpEncoder);
2018-12-23 21:36:38 +00:00
this.FileNameHelper = new FileNameHelper(configuration, cacheManager, logger);
this.ID3TagsHelper = new ID3TagsHelper(configuration, cacheManager, logger);
this.ArtistLookupEngine = new ArtistLookupEngine(configuration, httpEncoder, dbContext, cacheManager, logger);
this.LabelLookupEngine = new LabelLookupEngine(configuration, httpEncoder, dbContext, cacheManager, logger);
this.ReleaseLookupEngine = new ReleaseLookupEngine(configuration, httpEncoder, dbContext, cacheManager, logger, this.ArtistLookupEngine, this.LabelLookupEngine);
this.ImageFactory = new ImageFactory(configuration, httpEncoder, dbContext, cacheManager, logger, this.ArtistLookupEngine, this.ReleaseLookupEngine);
this.LabelFactory = new LabelFactory(configuration, httpEncoder, dbContext, cacheManager, logger, this.ArtistLookupEngine, this.ReleaseLookupEngine);
this.AudioMetaDataHelper = new AudioMetaDataHelper(configuration, httpEncoder, dbContext, this.MusicBrainzProvider, this.LastFmHelper, cacheManager,
logger, this.ArtistLookupEngine, this.ImageFactory, this.FileNameHelper, this.ID3TagsHelper);
this.ReleaseFactory = new ReleaseFactory(configuration, httpEncoder, dbContext, cacheManager, logger, this.ArtistLookupEngine, this.LabelFactory, this.AudioMetaDataHelper, this.ReleaseLookupEngine);
2018-11-12 00:28:37 +00:00
}
2018-12-23 21:36:38 +00:00
public async Task<OperationResult<Library.Models.Releases.Release>> ById(User roadieUser, Guid id, IEnumerable<string> includes = null)
2018-11-14 03:19:28 +00:00
{
var sw = Stopwatch.StartNew();
sw.Start();
var cacheKey = string.Format("urn:release_by_id_operation:{0}:{1}", id, includes == null ? "0" : string.Join("|", includes));
2018-12-23 21:36:38 +00:00
var result = await this.CacheManager.GetAsync<OperationResult<Library.Models.Releases.Release>>(cacheKey, async () =>
{
2018-11-14 03:19:28 +00:00
return await this.ReleaseByIdAction(id, includes);
}, data.Artist.CacheRegionUrn(id));
if (result?.Data != null && roadieUser != null)
2018-11-14 03:19:28 +00:00
{
var release = this.GetRelease(id);
var userBookmarkResult = await this.BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Release);
if (userBookmarkResult.IsSuccess)
{
2018-12-12 14:31:39 +00:00
result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Value == release.RoadieId.ToString()) != null;
}
2018-11-14 03:19:28 +00:00
if (result.Data.Medias != null)
{
2019-01-02 02:03:17 +00:00
var user = this.GetUser(roadieUser.UserId);
2019-01-08 22:40:26 +00:00
foreach (var media in result.Data.Medias)
2019-01-02 02:03:17 +00:00
{
2019-01-08 22:40:26 +00:00
foreach (var track in media.Tracks)
2019-01-02 02:03:17 +00:00
{
track.TrackPlayUrl = this.MakeTrackPlayUrl(user, track.DatabaseId, track.Id);
}
}
2018-11-14 03:19:28 +00:00
var releaseTrackIds = result.Data.Medias.SelectMany(x => x.Tracks).Select(x => x.Id);
var releaseUserTracks = (from ut in this.DbContext.UserTracks
join t in this.DbContext.Tracks on ut.TrackId equals t.Id
where ut.UserId == roadieUser.Id
where (from x in releaseTrackIds select x).Contains(t.RoadieId)
select new
{
t,
ut
}).ToArray();
if (releaseUserTracks != null && releaseUserTracks.Any())
{
foreach (var releaseUserTrack in releaseUserTracks)
{
foreach (var media in result.Data.Medias)
{
var releaseTrack = media.Tracks.FirstOrDefault(x => x.Id == releaseUserTrack.t.RoadieId);
if (releaseTrack != null)
{
2018-11-15 00:16:25 +00:00
releaseTrack.UserRating = new UserTrack
2018-11-14 03:19:28 +00:00
{
Rating = releaseUserTrack.ut.Rating,
IsDisliked = releaseUserTrack.ut.IsDisliked ?? false,
IsFavorite = releaseUserTrack.ut.IsFavorite ?? false,
LastPlayed = releaseUserTrack.ut.LastPlayed,
PlayedCount = releaseUserTrack.ut.PlayedCount
};
}
}
}
}
}
var userRelease = this.DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == roadieUser.Id);
if (userRelease != null)
{
result.Data.UserRating = new UserRelease
{
IsDisliked = userRelease.IsDisliked ?? false,
IsFavorite = userRelease.IsFavorite ?? false,
Rating = userRelease.Rating
};
}
}
sw.Stop();
2018-12-23 21:36:38 +00:00
return new OperationResult<Library.Models.Releases.Release>(result.Messages)
2018-11-14 03:19:28 +00:00
{
Data = result?.Data,
IsNotFoundResult = result?.IsNotFoundResult ?? false,
2018-11-14 03:19:28 +00:00
Errors = result?.Errors,
IsSuccess = result?.IsSuccess ?? false,
OperationTime = sw.ElapsedMilliseconds
};
}
2018-12-24 19:40:49 +00:00
public Task<Library.Models.Pagination.PagedResult<ReleaseList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, IEnumerable<string> includes = null)
{
var sw = new Stopwatch();
sw.Start();
2018-11-21 06:34:53 +00:00
IEnumerable<int> collectionReleaseIds = null;
2018-12-07 21:02:38 +00:00
if (request.FilterToCollectionId.HasValue)
2018-11-21 06:34:53 +00:00
{
collectionReleaseIds = (from cr in this.DbContext.CollectionReleases
2018-11-22 23:12:57 +00:00
join c in this.DbContext.Collections on cr.CollectionId equals c.Id
2018-11-21 06:34:53 +00:00
join r in this.DbContext.Releases on cr.ReleaseId equals r.Id
2018-11-22 23:12:57 +00:00
where c.RoadieId == request.FilterToCollectionId.Value
2018-12-11 14:08:34 +00:00
orderby cr.ListNumber
2019-01-18 23:00:44 +00:00
select r.Id).ToArray();
2018-11-21 06:34:53 +00:00
}
2018-11-21 18:19:38 +00:00
int[] favoriteReleaseIds = new int[0];
if (request.FilterFavoriteOnly)
{
favoriteReleaseIds = (from a in this.DbContext.Releases
2018-12-07 21:02:38 +00:00
join ur in this.DbContext.UserReleases on a.Id equals ur.ReleaseId
where ur.IsFavorite ?? false
where (roadieUser == null || ur.UserId == roadieUser.Id)
2018-12-01 03:22:35 +00:00
select a.Id
2018-11-21 18:19:38 +00:00
).ToArray();
}
2018-11-22 23:43:02 +00:00
int[] genreReleaseIds = new int[0];
2018-12-30 04:34:32 +00:00
var isFilteredToGenre = false;
if (!string.IsNullOrEmpty(request.FilterByGenre) || (!string.IsNullOrEmpty(request.Filter) && request.Filter.StartsWith(":genre", StringComparison.OrdinalIgnoreCase)))
2018-11-22 23:43:02 +00:00
{
2018-12-30 04:34:32 +00:00
var genreFilter = request.FilterByGenre ?? (request.Filter ?? string.Empty).Replace(":genre ", "", StringComparison.OrdinalIgnoreCase);
2018-11-22 23:43:02 +00:00
genreReleaseIds = (from rg in this.DbContext.ReleaseGenres
join g in this.DbContext.Genres on rg.GenreId equals g.Id
2018-12-30 04:34:32 +00:00
where g.Name.Contains(genreFilter)
select rg.ReleaseId)
.Distinct()
.ToArray();
request.Filter = null;
isFilteredToGenre = true;
2018-11-22 23:43:02 +00:00
}
2018-12-07 21:02:38 +00:00
if (request.FilterFromYear.HasValue || request.FilterToYear.HasValue)
2018-11-24 01:46:12 +00:00
{
// If from is larger than to then reverse values and set sort order to desc
2018-12-07 21:02:38 +00:00
if (request.FilterToYear > request.FilterFromYear)
2018-11-24 01:46:12 +00:00
{
var t = request.FilterToYear;
request.FilterToYear = request.FilterFromYear;
request.FilterFromYear = t;
request.Order = "DESC";
}
else
{
request.Order = "ASC";
}
}
2018-12-09 14:33:40 +00:00
2018-12-01 03:22:35 +00:00
//
2018-12-07 04:45:09 +00:00
// TODO list should honor disliked artist and albums for random
2018-12-01 03:22:35 +00:00
//
2019-03-10 16:55:21 +00:00
var isEqualFilter = false;
if (!string.IsNullOrEmpty(request.FilterValue))
{
var filter = request.FilterValue;
// if filter string is wrapped in quotes then is an exact not like search, e.g. "Diana Ross" should not return "Diana Ross & The Supremes"
if (filter.StartsWith('"') && filter.EndsWith('"'))
{
isEqualFilter = true;
request.Filter = filter.Substring(1, filter.Length - 2);
}
}
2019-01-27 22:49:55 +00:00
var normalizedFilterValue = !string.IsNullOrEmpty(request.FilterValue) ? request.FilterValue.ToAlphanumericName() : null;
2018-12-07 21:02:38 +00:00
var result = (from r in this.DbContext.Releases
join a in this.DbContext.Artists on r.ArtistId equals a.Id
where (request.FilterMinimumRating == null || r.Rating >= request.FilterMinimumRating.Value)
where (request.FilterToArtistId == null || r.Artist.RoadieId == request.FilterToArtistId)
2018-11-21 06:34:53 +00:00
where (request.FilterToCollectionId == null || collectionReleaseIds.Contains(r.Id))
2018-11-21 18:19:38 +00:00
where (!request.FilterFavoriteOnly || favoriteReleaseIds.Contains(r.Id))
2018-12-30 04:34:32 +00:00
where (!isFilteredToGenre || genreReleaseIds.Contains(r.Id))
2018-11-22 17:31:59 +00:00
where (request.FilterFromYear == null || r.ReleaseDate != null && r.ReleaseDate.Value.Year <= request.FilterFromYear)
where (request.FilterToYear == null || r.ReleaseDate != null && r.ReleaseDate.Value.Year >= request.FilterToYear)
2019-05-29 22:25:40 +00:00
where (request.FilterValue == "" || (r.Title.Contains(request.FilterValue) ||
r.AlternateNames.Contains(request.FilterValue) ||
2019-03-10 16:55:21 +00:00
r.AlternateNames.Contains(normalizedFilterValue)))
where (!isEqualFilter || (r.Title.Equals(request.FilterValue) ||
r.AlternateNames.Equals(request.FilterValue) ||
r.AlternateNames.Equals(normalizedFilterValue)))
2018-12-08 19:47:19 +00:00
select new ReleaseList
{
DatabaseId = r.Id,
Id = r.RoadieId,
Artist = new DataToken
{
Value = a.RoadieId.ToString(),
Text = a.Name
},
Release = new DataToken
{
Text = r.Title,
Value = r.RoadieId.ToString()
},
ArtistThumbnail = this.MakeArtistThumbnailImage(a.RoadieId),
CreatedDate = r.CreatedDate,
Duration = r.Duration,
LastPlayed = r.LastPlayed,
LastUpdated = r.LastUpdated,
LibraryStatus = r.LibraryStatus,
MediaCount = r.MediaCount,
Rating = r.Rating,
2019-01-22 00:04:58 +00:00
Rank = r.Rank,
2018-12-08 19:47:19 +00:00
ReleaseDateDateTime = r.ReleaseDate,
ReleasePlayUrl = $"{ this.HttpContext.BaseUrl }/play/release/{ r.RoadieId}",
Status = r.Status,
Thumbnail = this.MakeReleaseThumbnailImage(r.RoadieId),
TrackCount = r.TrackCount,
TrackPlayedCount = r.PlayedCount
}
2018-12-07 21:02:38 +00:00
).Distinct();
ReleaseList[] rows = null;
var rowCount = result.Count();
if (doRandomize ?? false)
{
2018-11-20 14:36:07 +00:00
var randomLimit = roadieUser?.RandomReleaseLimit ?? 100;
request.Limit = request.LimitValue > randomLimit ? randomLimit : request.LimitValue;
2019-03-06 01:18:21 +00:00
rows = result.OrderBy(x => x.RandomSortId).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
}
else
{
string sortBy = null;
if (request.ActionValue == User.ActionKeyUserRated)
{
2018-12-07 04:45:09 +00:00
sortBy = request.OrderValue(new Dictionary<string, string> { { "Rating", "DESC" } });
}
2018-12-06 02:11:42 +00:00
else if (request.FilterToArtistId.HasValue)
{
2018-12-07 04:45:09 +00:00
sortBy = request.OrderValue(new Dictionary<string, string> { { "ReleaseDate", "ASC" }, { "Release.Text", "ASC" } });
2018-12-06 02:11:42 +00:00
}
else
{
2018-12-07 04:45:09 +00:00
sortBy = request.OrderValue(new Dictionary<string, string> { { "Release.Text", "ASC" } });
}
2018-12-08 19:47:19 +00:00
if (request.FilterRatedOnly)
2018-11-20 14:36:07 +00:00
{
result = result.Where(x => x.Rating.HasValue);
}
2018-12-08 19:47:19 +00:00
if (request.FilterMinimumRating.HasValue)
2018-11-20 14:36:07 +00:00
{
result = result.Where(x => x.Rating.HasValue && x.Rating.Value >= request.FilterMinimumRating.Value);
}
2018-12-11 04:55:36 +00:00
if (request.FilterToCollectionId.HasValue)
{
rows = result.ToArray();
}
else
{
rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
}
}
2018-11-21 06:34:53 +00:00
if (rows.Any())
{
2018-11-21 06:34:53 +00:00
var rowIds = rows.Select(x => x.DatabaseId).ToArray();
var genreData = (from rg in this.DbContext.ReleaseGenres
join g in this.DbContext.Genres on rg.GenreId equals g.Id
where rowIds.Contains(rg.ReleaseId)
orderby rg.Id
select new
{
rg.ReleaseId,
dt = new DataToken
{
Text = g.Name,
Value = g.RoadieId.ToString()
}
}).ToArray();
2018-12-08 19:47:19 +00:00
2018-11-21 06:34:53 +00:00
foreach (var release in rows)
{
2018-11-21 06:34:53 +00:00
var genre = genreData.FirstOrDefault(x => x.ReleaseId == release.DatabaseId);
release.Genre = genre?.dt ?? new DataToken();
}
if (request.FilterToCollectionId.HasValue)
{
2018-12-11 13:39:20 +00:00
var newRows = new List<ReleaseList>(rows);
var collection = this.GetCollection(request.FilterToCollectionId.Value);
var collectionReleases = (from c in this.DbContext.Collections
join cr in this.DbContext.CollectionReleases on c.Id equals cr.CollectionId
2018-12-11 04:55:36 +00:00
where c.RoadieId == request.FilterToCollectionId
select cr);
2019-01-18 23:00:44 +00:00
var pars = collection.PositionArtistReleases().ToArray();
foreach (var par in pars)
{
2018-12-11 13:39:20 +00:00
var cr = collectionReleases.FirstOrDefault(x => x.ListNumber == par.Position);
2019-03-09 16:33:19 +00:00
// Release is known for Collection CSV
if (cr != null)
{
2018-12-11 13:39:20 +00:00
var parRelease = rows.FirstOrDefault(x => x.DatabaseId == cr.ReleaseId);
2018-12-23 21:36:38 +00:00
if (parRelease != null)
2018-12-11 13:39:20 +00:00
{
2018-12-23 21:36:38 +00:00
if (!parRelease.ListNumber.HasValue)
2018-12-11 13:39:20 +00:00
{
parRelease.ListNumber = par.Position;
}
}
}
2018-12-11 13:39:20 +00:00
// Release is not known add missing dummy release to rows
else
{
2018-12-11 13:39:20 +00:00
newRows.Add(new ReleaseList
{
2018-12-11 13:39:20 +00:00
Artist = new DataToken
{
Text = par.Artist
},
Release = new DataToken
{
Text = par.Release
},
CssClass = "missing",
2018-12-23 21:36:38 +00:00
ArtistThumbnail = new Library.Models.Image($"{this.HttpContext.ImageBaseUrl }/unknown.jpg"),
Thumbnail = new Library.Models.Image($"{this.HttpContext.ImageBaseUrl }/unknown.jpg"),
2018-12-11 13:39:20 +00:00
ListNumber = par.Position
});
}
}
// Resort the list for the collection by listNumber
2019-01-18 23:00:44 +00:00
rows = newRows.OrderBy(x => x.ListNumber).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
2018-12-11 14:08:34 +00:00
rowCount = collection.CollectionCount;
}
2018-11-21 06:34:53 +00:00
if (roadieUser != null)
{
2018-12-08 19:47:19 +00:00
var userReleaseRatings = (from ur in this.DbContext.UserReleases
where ur.UserId == roadieUser.Id
where rowIds.Contains(ur.ReleaseId)
select ur).ToArray();
foreach (var userReleaseRating in userReleaseRatings.Where(x => rows.Select(r => r.DatabaseId).Contains(x.ReleaseId)))
{
2018-12-08 19:47:19 +00:00
var row = rows.FirstOrDefault(x => x.DatabaseId == userReleaseRating.ReleaseId);
2018-11-21 06:34:53 +00:00
if (row != null)
{
2018-12-08 19:47:19 +00:00
var isDisliked = userReleaseRating.IsDisliked ?? false;
var isFavorite = userReleaseRating.IsFavorite ?? false;
2018-11-21 06:34:53 +00:00
row.UserRating = new UserRelease
{
IsDisliked = isDisliked,
IsFavorite = isFavorite,
2018-12-08 19:47:19 +00:00
Rating = userReleaseRating.Rating,
RatedDate = isDisliked || isFavorite ? (DateTime?)(userReleaseRating.LastUpdated ?? userReleaseRating.CreatedDate) : null
2018-11-21 06:34:53 +00:00
};
}
}
}
}
2018-12-06 02:11:42 +00:00
if (includes != null && includes.Any())
{
if (includes.Contains("tracks"))
{
foreach (var release in rows)
{
2019-05-11 03:07:45 +00:00
release.Media = this.DbContext.ReleaseMedias
.Include(x => x.Tracks)
.Where(x => x.ReleaseId == release.DatabaseId)
2019-05-11 17:32:25 +00:00
.ToArray()
.AsQueryable()
2019-05-11 03:07:45 +00:00
.ProjectToType<ReleaseMediaList>()
.OrderBy(x => x.MediaNumber)
.ToArray();
var userRatingsForRelease = (from ut in this.DbContext.UserTracks
join t in this.DbContext.Tracks on ut.TrackId equals t.Id
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
where rm.ReleaseId == release.DatabaseId
where ut.UserId == roadieUser.Id
select new { trackId = t.RoadieId, ut }).ToArray();
foreach (var userRatingForRelease in userRatingsForRelease)
2018-12-06 02:11:42 +00:00
{
2019-05-11 17:32:25 +00:00
var mediaTrack = release.Media?.SelectMany(x => x.Tracks).FirstOrDefault(x => x.Id == userRatingForRelease.trackId);
2019-05-11 03:07:45 +00:00
if (mediaTrack != null)
2018-12-06 02:11:42 +00:00
{
2019-05-11 03:07:45 +00:00
mediaTrack.UserRating = new UserTrack
2018-12-06 02:11:42 +00:00
{
2019-05-11 03:07:45 +00:00
Rating = userRatingForRelease.ut.Rating,
IsFavorite = userRatingForRelease.ut.IsFavorite ?? false,
IsDisliked = userRatingForRelease.ut.IsDisliked ?? false
};
2018-12-06 02:11:42 +00:00
}
}
}
}
}
if (request.FilterFavoriteOnly)
2018-12-01 03:22:35 +00:00
{
rows = rows.OrderBy(x => x.UserRating.Rating).ToArray();
}
sw.Stop();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new Library.Models.Pagination.PagedResult<ReleaseList>
{
TotalCount = rowCount,
CurrentPage = request.PageValue,
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
OperationTime = sw.ElapsedMilliseconds,
Rows = rows
2018-12-24 19:40:49 +00:00
});
}
2019-01-22 00:04:58 +00:00
public async Task<OperationResult<bool>> MergeReleases(User user, Guid releaseToMergeId, Guid releaseToMergeIntoId, bool addAsMedia)
{
var sw = new Stopwatch();
sw.Start();
var errors = new List<Exception>();
var releaseToMerge = this.DbContext.Releases
.Include(x => x.Artist)
.Include(x => x.Genres)
.Include("Genres.Genre")
.Include(x => x.Medias)
.Include("Medias.Tracks")
.Include("Medias.Tracks.TrackArtist")
.FirstOrDefault(x => x.RoadieId == releaseToMergeId);
if (releaseToMerge == null)
{
this.Logger.LogWarning("MergeReleases Unknown Release [{0}]", releaseToMergeId);
return new OperationResult<bool>(true, string.Format("Release Not Found [{0}]", releaseToMergeId));
}
var releaseToMergeInfo = this.DbContext.Releases
.Include(x => x.Artist)
.Include(x => x.Genres)
.Include("Genres.Genre")
.Include(x => x.Medias)
.Include("Medias.Tracks")
.Include("Medias.Tracks.TrackArtist")
.FirstOrDefault(x => x.RoadieId == releaseToMergeIntoId);
if (releaseToMergeInfo == null)
{
this.Logger.LogWarning("MergeReleases Unknown Release [{0}]", releaseToMergeIntoId);
return new OperationResult<bool>(true, string.Format("Release Not Found [{0}]", releaseToMergeIntoId));
}
try
{
await this.ReleaseFactory.MergeReleases(releaseToMerge, releaseToMergeInfo, addAsMedia);
}
catch (Exception ex)
{
this.Logger.LogError(ex);
errors.Add(ex);
}
sw.Stop();
this.Logger.LogInformation("MergeReleases Release `{0}` Merged Into Release `{1}`, By User `{2}`", releaseToMerge, releaseToMergeInfo, user);
return new OperationResult<bool>
{
IsSuccess = !errors.Any(),
Data = !errors.Any(),
OperationTime = sw.ElapsedMilliseconds,
Errors = errors
};
}
2018-12-24 19:40:49 +00:00
public Task<FileOperationResult<byte[]>> ReleaseZipped(User roadieUser, Guid id)
{
var release = this.GetRelease(id);
if (release == null)
{
2018-12-24 19:40:49 +00:00
return Task.FromResult(new FileOperationResult<byte[]>(true, string.Format("Release Not Found [{0}]", id)));
}
byte[] zipBytes = null;
string zipFileName = null;
try
{
var artistFolder = release.Artist.ArtistFileFolder(this.Configuration, this.Configuration.LibraryFolder);
var releaseFolder = release.ReleaseFileFolder(artistFolder);
2018-12-08 19:47:19 +00:00
if (!Directory.Exists(releaseFolder))
{
this.Logger.LogCritical($"Release Folder [{ releaseFolder }] not found for Release `{ release }`");
2018-12-24 19:40:49 +00:00
return Task.FromResult(new FileOperationResult<byte[]>(true, string.Format("Release Folder Not Found [{0}]", id)));
}
var releaseFiles = Directory.GetFiles(releaseFolder);
using (MemoryStream zipStream = new MemoryStream())
{
using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create))
{
foreach (var releaseFile in releaseFiles)
{
var fileInfo = new FileInfo(releaseFile);
if (fileInfo.Extension.ToLower() == ".mp3" || fileInfo.Extension.ToLower() == ".jpg")
{
ZipArchiveEntry entry = zip.CreateEntry(fileInfo.Name);
using (Stream entryStream = entry.Open())
{
using (FileStream s = fileInfo.OpenRead())
{
s.CopyTo(entryStream);
}
}
}
}
}
zipBytes = zipStream.ToArray();
}
zipFileName = $"{ release.Artist.Name }_{release.Title}.zip".ToFileNameFriendly();
this.Logger.LogInformation($"User `{ roadieUser }` downloaded Release `{ release }` ZipFileName [{ zipFileName }], Zip Size [{ zipBytes?.Length }]");
}
catch (Exception ex)
{
this.Logger.LogError(ex, "Error creating zip for Release `{0}`", release.ToString());
}
2018-12-24 19:40:49 +00:00
return Task.FromResult(new FileOperationResult<byte[]>
{
IsSuccess = zipBytes != null,
Data = zipBytes,
2018-12-08 19:47:19 +00:00
AdditionalData = new Dictionary<string, object> { { "ZipFileName", zipFileName } }
2018-12-24 19:40:49 +00:00
});
}
2018-12-23 21:36:38 +00:00
public async Task<OperationResult<Library.Models.Image>> SetReleaseImageByUrl(User user, Guid id, string imageUrl)
2018-12-21 22:59:33 +00:00
{
return await this.SaveImageBytes(user, id, WebHelper.BytesForImageUrl(imageUrl));
}
2018-12-23 21:36:38 +00:00
public async Task<OperationResult<bool>> UpdateRelease(User user, Library.Models.Releases.Release model)
2018-12-21 22:59:33 +00:00
{
2018-12-23 21:36:38 +00:00
var didChangeArtist = false;
var didChangeThumbnail = false;
2018-12-21 22:59:33 +00:00
var sw = new Stopwatch();
sw.Start();
var errors = new List<Exception>();
2018-12-23 21:36:38 +00:00
var release = this.DbContext.Releases
.Include(x => x.Artist)
.Include(x => x.Genres)
.Include("Genres.Genre")
.Include(x => x.Labels)
.Include("Labels.Label")
.FirstOrDefault(x => x.RoadieId == model.Id);
2018-12-21 22:59:33 +00:00
if (release == null)
{
2018-12-23 21:36:38 +00:00
return new OperationResult<bool>(true, string.Format("Release Not Found [{0}]", model.Id));
2018-12-21 22:59:33 +00:00
}
try
{
var now = DateTime.UtcNow;
2018-12-23 21:36:38 +00:00
var artistFolder = release.Artist.ArtistFileFolder(this.Configuration, this.Configuration.LibraryFolder);
var originalReleaseFolder = release.ReleaseFileFolder(artistFolder);
release.IsLocked = model.IsLocked;
release.IsVirtual = model.IsVirtual;
release.Status = SafeParser.ToEnum<Statuses>(model.Status);
release.Title = model.Title;
release.AlternateNames = model.AlternateNamesList.ToDelimitedList();
release.ReleaseDate = model.ReleaseDate;
release.Rating = model.Rating;
release.TrackCount = model.TrackCount;
release.MediaCount = model.MediaCount;
release.Profile = model.Profile;
release.DiscogsId = model.DiscogsId;
release.ReleaseType = SafeParser.ToEnum<ReleaseType>(model.ReleaseType);
release.LibraryStatus = SafeParser.ToEnum<LibraryStatus>(model.LibraryStatus);
release.ITunesId = model.ITunesId;
release.AmgId = model.AmgId;
release.LastFMId = model.LastFMId;
release.LastFMSummary = model.LastFMSummary;
release.MusicBrainzId = model.MusicBrainzId;
release.SpotifyId = model.SpotifyId;
release.Tags = model.TagsList.ToDelimitedList();
release.URLs = model.URLsList.ToDelimitedList();
if (model?.Artist?.Artist?.Value != null)
{
var artist = this.DbContext.Artists.FirstOrDefault(x => x.RoadieId == SafeParser.ToGuid(model.Artist.Artist.Value));
if (artist != null && release.ArtistId != artist.Id)
{
release.ArtistId = artist.Id;
didChangeArtist = true;
}
}
var releaseImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
if (releaseImage != null)
2018-12-21 22:59:33 +00:00
{
// Ensure is jpeg first
2018-12-23 21:36:38 +00:00
release.Thumbnail = ImageHelper.ConvertToJpegFormat(releaseImage);
2018-12-21 22:59:33 +00:00
// Save unaltered image to cover file
var coverFileName = Path.Combine(release.ReleaseFileFolder(release.Artist.ArtistFileFolder(this.Configuration, this.Configuration.LibraryFolder)), "cover.jpg");
File.WriteAllBytes(coverFileName, release.Thumbnail);
// Resize to store in database as thumbnail
release.Thumbnail = ImageHelper.ResizeImage(release.Thumbnail, this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height);
2018-12-23 21:36:38 +00:00
didChangeThumbnail = true;
}
2018-12-21 22:59:33 +00:00
2018-12-23 21:36:38 +00:00
if (model.Genres != null && model.Genres.Any())
{
// Remove existing Genres not in model list
foreach (var genre in release.Genres.ToList())
{
var doesExistInModel = model.Genres.Any(x => SafeParser.ToGuid(x.Value) == genre.Genre.RoadieId);
if (!doesExistInModel)
{
release.Genres.Remove(genre);
}
}
// Add new Genres in model not in data
foreach (var genre in model.Genres)
{
var genreId = SafeParser.ToGuid(genre.Value);
var doesExistInData = release.Genres.Any(x => x.Genre.RoadieId == genreId);
if (!doesExistInData)
{
var g = this.DbContext.Genres.FirstOrDefault(x => x.RoadieId == genreId);
if (g != null)
{
release.Genres.Add(new data.ReleaseGenre
{
ReleaseId = release.Id,
GenreId = g.Id,
Genre = g
});
}
}
}
}
else if (model.Genres == null || !model.Genres.Any())
{
release.Genres.Clear();
2018-12-21 22:59:33 +00:00
}
2018-12-23 21:36:38 +00:00
if (model.Labels != null && model.Labels.Any())
{
// TODO
}
if (model.Images != null && model.Images.Any())
{
// TODO
}
2018-12-21 22:59:33 +00:00
release.LastUpdated = now;
await this.DbContext.SaveChangesAsync();
2018-12-23 21:36:38 +00:00
await this.ReleaseFactory.CheckAndChangeReleaseTitle(release, originalReleaseFolder);
2018-12-21 22:59:33 +00:00
this.CacheManager.ClearRegion(release.CacheRegion);
2018-12-23 21:36:38 +00:00
this.Logger.LogInformation($"UpdateRelease `{ release }` By User `{ user }`: Edited Artist [{ didChangeArtist }], Uploaded new image [{ didChangeThumbnail }]");
2018-12-21 22:59:33 +00:00
}
catch (Exception ex)
{
this.Logger.LogError(ex);
errors.Add(ex);
}
sw.Stop();
2018-12-23 21:36:38 +00:00
return new OperationResult<bool>
2018-12-21 22:59:33 +00:00
{
IsSuccess = !errors.Any(),
2018-12-23 21:36:38 +00:00
Data = !errors.Any(),
2018-12-21 22:59:33 +00:00
OperationTime = sw.ElapsedMilliseconds,
Errors = errors
};
}
2018-12-23 21:36:38 +00:00
public async Task<OperationResult<Library.Models.Image>> UploadReleaseImage(User user, Guid id, IFormFile file)
{
var bytes = new byte[0];
using (var ms = new MemoryStream())
{
file.CopyTo(ms);
bytes = ms.ToArray();
}
return await this.SaveImageBytes(user, id, bytes);
}
private async Task<OperationResult<Library.Models.Releases.Release>> ReleaseByIdAction(Guid id, IEnumerable<string> includes = null)
2018-11-14 03:19:28 +00:00
{
var sw = Stopwatch.StartNew();
sw.Start();
var release = this.GetRelease(id);
if (release == null)
{
2018-12-23 21:36:38 +00:00
return new OperationResult<Library.Models.Releases.Release>(true, string.Format("Release Not Found [{0}]", id));
2018-11-14 03:19:28 +00:00
}
2018-12-23 21:36:38 +00:00
var result = release.Adapt<Library.Models.Releases.Release>();
2018-12-07 04:45:09 +00:00
result.Artist = ArtistList.FromDataArtist(release.Artist, this.MakeArtistThumbnailImage(release.Artist.RoadieId));
2018-11-14 03:19:28 +00:00
result.Thumbnail = this.MakeReleaseThumbnailImage(release.RoadieId);
2018-12-07 04:45:09 +00:00
result.MediumThumbnail = base.MakeThumbnailImage(id, "release", this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height);
2018-11-14 03:19:28 +00:00
result.ReleasePlayUrl = $"{ this.HttpContext.BaseUrl }/play/release/{ release.RoadieId}";
result.Profile = release.Profile;
result.ReleaseDate = release.ReleaseDate.Value;
result.MediaCount = release.MediaCount;
result.TrackCount = release.TrackCount;
result.CreatedDate = release.CreatedDate;
result.LastUpdated = release.LastUpdated;
result.AlternateNames = release.AlternateNames;
result.Tags = release.Tags;
result.URLs = release.URLs;
if (release.SubmissionId.HasValue)
{
var submission = this.DbContext.Submissions.Include(x => x.User).FirstOrDefault(x => x.Id == release.SubmissionId);
if (submission != null)
{
if (!submission.User.IsPrivate ?? false)
{
result.Submission = new ReleaseSubmission
{
User = new DataToken
{
Text = submission.User.UserName,
Value = submission.User.RoadieId.ToString()
},
UserThumbnail = this.MakeUserThumbnailImage(submission.User.RoadieId),
SubmittedDate = submission.CreatedDate
};
}
}
}
if (includes != null && includes.Any())
{
2018-12-08 19:47:19 +00:00
if (includes.Contains("genres"))
2018-12-07 04:45:09 +00:00
{
result.Genres = release.Genres.Select(x => new DataToken
{
Text = x.Genre.Name,
Value = x.Genre.RoadieId.ToString()
});
}
2018-11-14 03:19:28 +00:00
if (includes.Contains("stats"))
{
var releaseTracks = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
join t in this.DbContext.Tracks on rm.Id equals t.ReleaseMediaId
where r.Id == release.Id
select new
{
id = t.Id,
size = t.FileSize,
time = t.Duration,
isMissing = t.Hash == null
});
var releaseMedias = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
where r.Id == release.Id
select new
{
rm.Id,
rm.MediaNumber
});
2019-02-25 02:26:54 +00:00
long releaseTime = releaseTracks?.Sum(x => (long?)x.time) ?? 0;
2018-11-14 03:19:28 +00:00
var releaseStats = new ReleaseStatistics
{
2018-12-07 04:45:09 +00:00
MediaCount = release.MediaCount,
2018-12-15 04:19:52 +00:00
MissingTrackCount = releaseTracks?.Where(x => x.isMissing).Count(),
2018-12-07 04:45:09 +00:00
TrackCount = release.TrackCount,
TrackPlayedCount = release.PlayedCount,
2018-12-23 21:36:38 +00:00
TrackSize = releaseTracks?.Sum(x => (long?)x.size).ToFileSize(),
TrackTime = releaseTracks.Any() ? new TimeInfo((decimal)releaseTime).ToFullFormattedString() : "--:--"
2018-11-14 03:19:28 +00:00
};
2018-12-15 04:19:52 +00:00
result.MaxMediaNumber = releaseMedias.Any() ? releaseMedias.Max(x => x.MediaNumber) : (short)0;
2018-11-14 03:19:28 +00:00
result.Statistics = releaseStats;
2018-12-15 04:19:52 +00:00
result.MediaCount = release.MediaCount ?? (short?)releaseStats?.MediaCount;
2018-11-14 03:19:28 +00:00
}
if (includes.Contains("images"))
{
2018-12-04 23:26:27 +00:00
var releaseImages = this.DbContext.Images.Where(x => x.ReleaseId == release.Id).Select(x => MakeFullsizeImage(x.RoadieId, x.Caption)).ToArray();
if (releaseImages != null && releaseImages.Any())
2018-11-14 03:19:28 +00:00
{
result.Images = releaseImages;
}
2019-02-10 00:19:26 +00:00
var artistFolder = release.Artist.ArtistFileFolder(this.Configuration, this.Configuration.LibraryFolder);
var releaseFolder = release.ReleaseFileFolder(artistFolder);
2019-02-12 02:17:24 +00:00
var releaseImagesInFolder = ImageHelper.FindImageTypeInDirectory(new DirectoryInfo(releaseFolder), ImageType.ReleaseSecondary, SearchOption.TopDirectoryOnly);
2019-05-29 22:25:40 +00:00
if (releaseImagesInFolder.Any())
2019-02-10 00:19:26 +00:00
{
result.Images = result.Images.Concat(releaseImagesInFolder.Select((x, i) => MakeFullsizeSecondaryImage(id, ImageType.ReleaseSecondary, i)));
}
2018-11-14 03:19:28 +00:00
}
2018-12-08 19:47:19 +00:00
if (includes.Contains("playlists"))
2018-12-07 04:45:09 +00:00
{
var pg = new PagedRequest
{
FilterToReleaseId = release.RoadieId
};
var r = await this.PlaylistService.List(pg);
if (r.IsSuccess)
{
result.Playlists = r.Rows.ToArray();
}
}
2018-11-14 03:19:28 +00:00
if (includes.Contains("labels"))
{
var releaseLabels = (from l in this.DbContext.Labels
join rl in this.DbContext.ReleaseLabels on l.Id equals rl.LabelId
where rl.ReleaseId == release.Id
orderby rl.BeginDate, l.Name
select new
{
l,
rl
}).ToArray();
if (releaseLabels != null)
{
var labels = new List<ReleaseLabel>();
foreach (var releaseLabel in releaseLabels)
{
var rl = new ReleaseLabel
{
BeginDate = releaseLabel.rl.BeginDate,
EndDate = releaseLabel.rl.EndDate,
CatalogNumber = releaseLabel.rl.CatalogNumber,
2018-12-23 21:36:38 +00:00
CreatedDate = releaseLabel.rl.CreatedDate,
Id = releaseLabel.rl.RoadieId,
2018-12-08 19:47:19 +00:00
Label = new LabelList
2018-11-14 03:19:28 +00:00
{
2018-12-07 04:45:09 +00:00
Id = releaseLabel.rl.RoadieId,
Label = new DataToken
{
Text = releaseLabel.l.Name,
Value = releaseLabel.l.RoadieId.ToString()
},
SortName = releaseLabel.l.SortName,
CreatedDate = releaseLabel.l.CreatedDate,
LastUpdated = releaseLabel.l.LastUpdated,
ArtistCount = releaseLabel.l.ArtistCount,
ReleaseCount = releaseLabel.l.ReleaseCount,
TrackCount = releaseLabel.l.TrackCount,
Thumbnail = MakeLabelThumbnailImage(releaseLabel.l.RoadieId)
2018-11-14 03:19:28 +00:00
}
};
labels.Add(rl);
}
result.Labels = labels;
}
}
if (includes.Contains("collections"))
{
var releaseCollections = this.DbContext.CollectionReleases.Include(x => x.Collection).Where(x => x.ReleaseId == release.Id).OrderBy(x => x.ListNumber).ToArray();
if (releaseCollections != null)
{
var collections = new List<ReleaseInCollection>();
foreach (var releaseCollection in releaseCollections)
{
2018-11-14 03:19:28 +00:00
collections.Add(new ReleaseInCollection
{
2018-12-07 04:45:09 +00:00
Collection = new CollectionList
2018-11-14 03:19:28 +00:00
{
2018-12-07 04:45:09 +00:00
DatabaseId = releaseCollection.Collection.Id,
Collection = new DataToken
{
Text = releaseCollection.Collection.Name,
Value = releaseCollection.Collection.RoadieId.ToString()
},
Id = releaseCollection.Collection.RoadieId,
CollectionCount = releaseCollection.Collection.CollectionCount,
CollectionType = (releaseCollection.Collection.CollectionType ?? CollectionType.Unknown).ToString(),
CollectionFoundCount = (from crc in this.DbContext.CollectionReleases
where crc.CollectionId == releaseCollection.Collection.Id
select crc.Id).Count(),
CreatedDate = releaseCollection.Collection.CreatedDate,
2019-01-26 20:55:58 +00:00
IsLocked = releaseCollection.Collection.IsLocked,
2018-12-07 04:45:09 +00:00
LastUpdated = releaseCollection.Collection.LastUpdated,
Thumbnail = MakeCollectionThumbnailImage(releaseCollection.Collection.RoadieId)
2018-11-14 03:19:28 +00:00
},
ListNumber = releaseCollection.ListNumber
});
}
result.Collections = collections;
}
}
if (includes.Contains("tracks"))
{
var releaseMedias = new List<ReleaseMediaList>();
foreach (var releaseMedia in release.Medias.OrderBy(x => x.MediaNumber))
2018-11-14 03:19:28 +00:00
{
var rm = releaseMedia.Adapt<ReleaseMediaList>();
var rmTracks = new List<TrackList>();
foreach (var track in releaseMedia.Tracks.OrderBy(x => x.TrackNumber))
2018-11-14 03:19:28 +00:00
{
var t = track.Adapt<TrackList>();
t.Track = new DataToken
{
Text = track.Title,
Value = track.RoadieId.ToString()
};
2018-12-30 04:34:32 +00:00
t.MediaNumber = rm.MediaNumber;
2018-12-14 03:52:14 +00:00
t.CssClass = string.IsNullOrEmpty(track.Hash) ? "Missing" : "Ok";
2018-12-07 04:45:09 +00:00
t.TrackArtist = track.TrackArtist != null ? ArtistList.FromDataArtist(track.TrackArtist, this.MakeArtistThumbnailImage(track.TrackArtist.RoadieId)) : null;
2018-11-14 03:19:28 +00:00
rmTracks.Add(t);
}
rm.Tracks = rmTracks;
releaseMedias.Add(rm);
}
result.Medias = releaseMedias;
}
2018-11-14 03:19:28 +00:00
}
sw.Stop();
2018-12-23 21:36:38 +00:00
return new OperationResult<Library.Models.Releases.Release>
2018-11-14 03:19:28 +00:00
{
Data = result,
IsSuccess = result != null,
OperationTime = sw.ElapsedMilliseconds
};
}
2018-12-23 21:36:38 +00:00
private async Task<OperationResult<Library.Models.Image>> SaveImageBytes(User user, Guid id, byte[] imageBytes)
{
var sw = new Stopwatch();
sw.Start();
var errors = new List<Exception>();
var release = this.DbContext.Releases.Include(x => x.Artist).FirstOrDefault(x => x.RoadieId == id);
if (release == null)
{
return new OperationResult<Library.Models.Image>(true, string.Format("Release Not Found [{0}]", id));
}
try
{
var now = DateTime.UtcNow;
release.Thumbnail = imageBytes;
if (release.Thumbnail != null)
{
// Ensure is jpeg first
release.Thumbnail = ImageHelper.ConvertToJpegFormat(release.Thumbnail);
// Save unaltered image to cover file
var coverFileName = Path.Combine(release.ReleaseFileFolder(release.Artist.ArtistFileFolder(this.Configuration, this.Configuration.LibraryFolder)), "cover.jpg");
File.WriteAllBytes(coverFileName, release.Thumbnail);
// Resize to store in database as thumbnail
release.Thumbnail = ImageHelper.ResizeImage(release.Thumbnail, this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height);
}
release.LastUpdated = now;
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(release.CacheRegion);
2018-12-26 21:18:51 +00:00
this.Logger.LogInformation($"SaveImageBytes `{ release }` By User `{ user }`");
2018-12-23 21:36:38 +00:00
}
catch (Exception ex)
{
this.Logger.LogError(ex);
errors.Add(ex);
}
sw.Stop();
return new OperationResult<Library.Models.Image>
{
IsSuccess = !errors.Any(),
2018-12-27 21:56:40 +00:00
Data = base.MakeThumbnailImage(id, "release", this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height, true),
2018-12-23 21:36:38 +00:00
OperationTime = sw.ElapsedMilliseconds,
Errors = errors
};
}
2018-11-12 00:28:37 +00:00
}
}