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;
|
2019-11-17 14:10:17 +00:00
|
|
|
|
using Roadie.Library.Data.Context;
|
2018-11-12 00:28:37 +00:00
|
|
|
|
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-21 22:59:33 +00:00
|
|
|
|
using Roadie.Library.Imaging;
|
2018-12-23 21:36:38 +00:00
|
|
|
|
using Roadie.Library.MetaData.Audio;
|
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;
|
2018-11-24 16:18:03 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
2018-11-12 00:28:37 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Dynamic.Core;
|
2022-01-17 20:03:52 +00:00
|
|
|
|
using System.Net.Http;
|
2018-11-12 00:28:37 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using data = Roadie.Library.Data;
|
2019-06-30 22:14:36 +00:00
|
|
|
|
using Image = Roadie.Library.Models.Image;
|
|
|
|
|
using Release = Roadie.Library.Models.Releases.Release;
|
2018-11-12 00:28:37 +00:00
|
|
|
|
|
|
|
|
|
namespace Roadie.Api.Services
|
|
|
|
|
{
|
2022-01-17 20:03:52 +00:00
|
|
|
|
public class ReleaseService : HttpFactoryServiceBase<ReleaseService>, IReleaseService
|
2018-11-12 00:28:37 +00:00
|
|
|
|
{
|
2018-12-23 21:36:38 +00:00
|
|
|
|
private IArtistLookupEngine ArtistLookupEngine { get; }
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2018-12-23 21:36:38 +00:00
|
|
|
|
private IAudioMetaDataHelper AudioMetaDataHelper { get; }
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
|
|
|
|
private IBookmarkService BookmarkService { get; }
|
|
|
|
|
|
|
|
|
|
private IPlaylistService PlaylistService { get; }
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
private readonly List<int> _addedTrackIds = new List<int>();
|
|
|
|
|
|
|
|
|
|
public IEnumerable<int> AddedTrackIds => _addedTrackIds;
|
|
|
|
|
|
2019-07-28 04:05:24 +00:00
|
|
|
|
public ReleaseService(IRoadieSettings configuration,
|
2019-07-07 03:16:33 +00:00
|
|
|
|
IHttpEncoder httpEncoder,
|
2019-06-30 22:14:36 +00:00
|
|
|
|
IHttpContext httpContext,
|
2019-11-17 14:10:17 +00:00
|
|
|
|
IRoadieDbContext dbContext,
|
2019-06-30 22:14:36 +00:00
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
|
IPlaylistService playlistService,
|
|
|
|
|
ILogger<ReleaseService> logger,
|
2019-07-07 03:16:33 +00:00
|
|
|
|
IBookmarkService bookmarkService,
|
|
|
|
|
IArtistLookupEngine artistLookupEngine,
|
2022-01-17 20:03:52 +00:00
|
|
|
|
IAudioMetaDataHelper audioMetaDataHelper,
|
|
|
|
|
IHttpClientFactory httpClientFactory
|
2019-08-22 20:33:07 +00:00
|
|
|
|
)
|
2022-01-17 20:03:52 +00:00
|
|
|
|
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext, httpClientFactory)
|
2018-11-12 00:28:37 +00:00
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
PlaylistService = playlistService;
|
|
|
|
|
BookmarkService = bookmarkService;
|
2019-07-07 03:16:33 +00:00
|
|
|
|
ArtistLookupEngine = artistLookupEngine;
|
|
|
|
|
AudioMetaDataHelper = audioMetaDataHelper;
|
2018-11-12 00:28:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
private async Task<OperationResult<Release>> ReleaseByIdAction(Guid id, IEnumerable<string> includes = null)
|
2018-11-14 03:19:28 +00:00
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
var timings = new Dictionary<string, long>();
|
|
|
|
|
var tsw = new Stopwatch();
|
|
|
|
|
|
2018-11-14 03:19:28 +00:00
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
sw.Start();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var release = await GetRelease(id).ConfigureAwait(false);
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("getRelease", tsw.ElapsedMilliseconds);
|
|
|
|
|
|
|
|
|
|
if (release == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<Release>(true, $"Release Not Found [{id}]");
|
|
|
|
|
}
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var result = release.Adapt<Release>();
|
|
|
|
|
result.Artist = ArtistList.FromDataArtist(release.Artist, ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, release.Artist.RoadieId));
|
|
|
|
|
result.Thumbnail = ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, release.RoadieId);
|
|
|
|
|
result.MediumThumbnail = ImageHelper.MakeThumbnailImage(Configuration, HttpContext, id, "release", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
|
|
|
|
|
result.ReleasePlayUrl = $"{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;
|
|
|
|
|
result.RankPosition = result.Rank > 0
|
|
|
|
|
? SafeParser.ToNumber<int?>(DbContext.Releases.Count(x => (double?)x.Rank > result.Rank) + 1)
|
|
|
|
|
: null;
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("adapt", tsw.ElapsedMilliseconds);
|
|
|
|
|
if (release.SubmissionId.HasValue)
|
2018-11-14 03:19:28 +00:00
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var submission = DbContext.Submissions.Include(x => x.User).FirstOrDefault(x => x.Id == release.SubmissionId);
|
|
|
|
|
if (submission != null && (!submission.User.IsPrivate ?? false))
|
|
|
|
|
{
|
|
|
|
|
result.Submission = new ReleaseSubmission
|
|
|
|
|
{
|
|
|
|
|
User = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = submission.User.UserName,
|
|
|
|
|
Value = submission.User.RoadieId.ToString()
|
|
|
|
|
},
|
|
|
|
|
UserThumbnail = ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, submission.User.RoadieId),
|
|
|
|
|
SubmittedDate = submission.CreatedDate
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
timings.Add("submissions", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
2019-08-04 16:30:19 +00:00
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (includes?.Any() == true)
|
|
|
|
|
{
|
|
|
|
|
if (includes.Contains("credits"))
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
tsw.Restart();
|
|
|
|
|
|
|
|
|
|
result.Credits = (await (from c in DbContext.Credits
|
|
|
|
|
join cc in DbContext.CreditCategory on c.CreditCategoryId equals cc.Id
|
|
|
|
|
join a in DbContext.Artists on c.ArtistId equals a.Id into agg
|
|
|
|
|
from a in agg.DefaultIfEmpty()
|
|
|
|
|
where c.ReleaseId == release.Id
|
|
|
|
|
select new { c, cc, a })
|
|
|
|
|
.ToListAsync().ConfigureAwait(false))
|
|
|
|
|
.Select(x => new CreditList
|
|
|
|
|
{
|
|
|
|
|
Id = x.c.RoadieId,
|
|
|
|
|
Artist = x.a == null ? null : ArtistList.FromDataArtist(x.a, ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, x.a.RoadieId)),
|
|
|
|
|
Category = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = x.cc.Name,
|
|
|
|
|
Value = x.cc.RoadieId.ToString()
|
|
|
|
|
},
|
|
|
|
|
CreditName = x.a?.Name ?? x.c.CreditToName,
|
|
|
|
|
Description = x.c.Description
|
|
|
|
|
}).ToArray();
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("credits", tsw.ElapsedMilliseconds);
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (includes.Contains("genres"))
|
2018-11-14 03:19:28 +00:00
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var releaseGenreIds = release.Genres.Select(x => x.GenreId).ToArray();
|
|
|
|
|
result.Genres = (await (from g in DbContext.Genres
|
|
|
|
|
let releaseCount = (from rg in DbContext.ReleaseGenres
|
|
|
|
|
where rg.GenreId == g.Id
|
|
|
|
|
select rg.Id).Count()
|
|
|
|
|
let artistCount = (from rg in DbContext.ArtistGenres
|
|
|
|
|
where rg.GenreId == g.Id
|
|
|
|
|
select rg.Id).Count()
|
|
|
|
|
where releaseGenreIds.Contains(g.Id)
|
|
|
|
|
select new { g, releaseCount, artistCount }).ToListAsync().ConfigureAwait(false))
|
|
|
|
|
.Select(x => GenreList.FromDataGenre(x.g,
|
|
|
|
|
ImageHelper.MakeGenreThumbnailImage(Configuration, HttpContext, x.g.RoadieId),
|
|
|
|
|
x.artistCount,
|
|
|
|
|
x.releaseCount))
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
timings.Add("genres", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
if (includes.Contains("stats"))
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var releaseTracks = from r in DbContext.Releases
|
|
|
|
|
join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
|
|
|
|
|
join t in 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 DbContext.Releases
|
|
|
|
|
join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
|
|
|
|
|
where r.Id == release.Id
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
rm.Id,
|
|
|
|
|
rm.MediaNumber
|
|
|
|
|
};
|
|
|
|
|
var releaseTime = releaseTracks?.Sum(x => (long?)x.time) ?? 0;
|
|
|
|
|
var releaseStats = new ReleaseStatistics
|
|
|
|
|
{
|
|
|
|
|
MediaCount = release.MediaCount,
|
|
|
|
|
MissingTrackCount = releaseTracks?.Where(x => x.isMissing).Count(),
|
|
|
|
|
TrackCount = release.TrackCount,
|
|
|
|
|
TrackPlayedCount = release.PlayedCount,
|
|
|
|
|
TrackSize = releaseTracks?.Sum(x => (long?)x.size).ToFileSize(),
|
|
|
|
|
TrackTime = releaseTracks.Any() ? new TimeInfo(releaseTime).ToFullFormattedString() : "--:--"
|
|
|
|
|
};
|
|
|
|
|
result.MaxMediaNumber = releaseMedias.Any() ? releaseMedias.Max(x => x.MediaNumber) : (short)0;
|
|
|
|
|
result.Statistics = releaseStats;
|
|
|
|
|
result.MediaCount = release.MediaCount ?? (short?)releaseStats?.MediaCount;
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("stats", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
2019-08-04 16:30:19 +00:00
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (includes.Contains("images"))
|
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var artistFolder = release.Artist.ArtistFileFolder(Configuration);
|
|
|
|
|
var releaseFolder = release.ReleaseFileFolder(artistFolder);
|
|
|
|
|
var releaseImagesInFolder = ImageHelper.FindImageTypeInDirectory(new DirectoryInfo(releaseFolder), ImageType.ReleaseSecondary, SearchOption.TopDirectoryOnly);
|
|
|
|
|
if (releaseImagesInFolder.Any())
|
|
|
|
|
{
|
|
|
|
|
result.Images = releaseImagesInFolder.Select((_, i) => ImageHelper.MakeFullsizeSecondaryImage(Configuration, HttpContext, id, ImageType.ReleaseSecondary, i));
|
|
|
|
|
}
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("images", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (includes.Contains("playlists"))
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var pg = new PagedRequest
|
|
|
|
|
{
|
|
|
|
|
FilterToReleaseId = release.RoadieId
|
|
|
|
|
};
|
|
|
|
|
var r = await PlaylistService.ListAsync(pg).ConfigureAwait(false);
|
|
|
|
|
if (r.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
result.Playlists = r.Rows.ToArray();
|
|
|
|
|
}
|
2019-08-06 03:13:00 +00:00
|
|
|
|
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
timings.Add("playlists", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
2019-08-04 16:30:19 +00:00
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (includes.Contains("labels"))
|
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var releaseLabels = (from l in DbContext.Labels
|
|
|
|
|
join rl in 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)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var labels = new List<ReleaseLabel>();
|
|
|
|
|
foreach (var releaseLabel in releaseLabels)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var rl = new ReleaseLabel
|
2018-11-14 03:19:28 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
BeginDate = releaseLabel.rl.BeginDate,
|
|
|
|
|
EndDate = releaseLabel.rl.EndDate,
|
|
|
|
|
CatalogNumber = releaseLabel.rl.CatalogNumber,
|
|
|
|
|
CreatedDate = releaseLabel.rl.CreatedDate,
|
|
|
|
|
Id = releaseLabel.rl.RoadieId,
|
|
|
|
|
Label = new LabelList
|
2019-08-04 16:30:19 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
Id = releaseLabel.rl.RoadieId,
|
|
|
|
|
Label = new DataToken
|
2018-11-14 03:19:28 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
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 = ImageHelper.MakeLabelThumbnailImage(Configuration, HttpContext, releaseLabel.l.RoadieId)
|
2019-08-04 16:30:19 +00:00
|
|
|
|
}
|
2020-06-07 22:46:24 +00:00
|
|
|
|
};
|
|
|
|
|
labels.Add(rl);
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2020-06-07 22:46:24 +00:00
|
|
|
|
|
|
|
|
|
result.Labels = labels;
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
timings.Add("labels", tsw.ElapsedMilliseconds);
|
2018-11-14 03:19:28 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (includes.Contains("collections"))
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var releaseCollections = DbContext.CollectionReleases.Include(x => x.Collection)
|
|
|
|
|
.Where(x => x.ReleaseId == release.Id)
|
|
|
|
|
.OrderBy(x => x.ListNumber)
|
|
|
|
|
.ToArray();
|
|
|
|
|
if (releaseCollections != null)
|
2018-11-14 03:19:28 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var collections = new List<ReleaseInCollection>();
|
|
|
|
|
foreach (var releaseCollection in releaseCollections)
|
|
|
|
|
{
|
|
|
|
|
collections.Add(new ReleaseInCollection
|
|
|
|
|
{
|
|
|
|
|
Collection = new CollectionList
|
|
|
|
|
{
|
|
|
|
|
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 DbContext.CollectionReleases
|
|
|
|
|
where crc.CollectionId == releaseCollection.Collection.Id
|
|
|
|
|
select crc.Id).Count(),
|
|
|
|
|
CreatedDate = releaseCollection.Collection.CreatedDate,
|
|
|
|
|
IsLocked = releaseCollection.Collection.IsLocked,
|
|
|
|
|
LastUpdated = releaseCollection.Collection.LastUpdated,
|
|
|
|
|
Thumbnail = ImageHelper.MakeCollectionThumbnailImage(Configuration, HttpContext, releaseCollection.Collection.RoadieId)
|
|
|
|
|
},
|
|
|
|
|
ListNumber = releaseCollection.ListNumber
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
result.Collections = collections;
|
|
|
|
|
}
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("collections", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (includes.Contains("comments"))
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var releaseComments = DbContext.Comments.Include(x => x.User)
|
|
|
|
|
.Where(x => x.ReleaseId == release.Id)
|
|
|
|
|
.OrderByDescending(x => x.CreatedDate)
|
|
|
|
|
.ToArray();
|
|
|
|
|
if (releaseComments.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var comments = new List<Comment>();
|
|
|
|
|
var commentIds = releaseComments.Select(x => x.Id).ToArray();
|
|
|
|
|
var userCommentReactions = (from cr in DbContext.CommentReactions
|
|
|
|
|
where commentIds.Contains(cr.CommentId)
|
|
|
|
|
select cr).ToArray();
|
|
|
|
|
foreach (var releaseComment in releaseComments)
|
|
|
|
|
{
|
|
|
|
|
var comment = releaseComment.Adapt<Comment>();
|
|
|
|
|
comment.DatabaseId = releaseComment.Id;
|
|
|
|
|
comment.User = UserList.FromDataUser(releaseComment.User, ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, releaseComment.User.RoadieId));
|
|
|
|
|
comment.DislikedCount = userCommentReactions.Count(x => x.CommentId == releaseComment.Id && x.ReactionValue == CommentReaction.Dislike);
|
|
|
|
|
comment.LikedCount = userCommentReactions.Count(x => x.CommentId == releaseComment.Id && x.ReactionValue == CommentReaction.Like);
|
|
|
|
|
comments.Add(comment);
|
|
|
|
|
}
|
|
|
|
|
result.Comments = comments;
|
|
|
|
|
}
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("comments", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (includes.Contains("tracks"))
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var releaseMedias = new List<ReleaseMediaList>();
|
|
|
|
|
foreach (var releaseMedia in release.Medias.OrderBy(x => x.MediaNumber))
|
|
|
|
|
{
|
|
|
|
|
var rm = releaseMedia.Adapt<ReleaseMediaList>();
|
|
|
|
|
var rmTracks = new List<TrackList>();
|
|
|
|
|
foreach (var track in releaseMedia.Tracks.OrderBy(x => x.TrackNumber))
|
|
|
|
|
{
|
|
|
|
|
var t = track.Adapt<TrackList>();
|
|
|
|
|
t.Track = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = track.Title,
|
|
|
|
|
Value = track.RoadieId.ToString()
|
|
|
|
|
};
|
|
|
|
|
t.MediaNumber = rm.MediaNumber;
|
|
|
|
|
t.Status = track.Status;
|
|
|
|
|
t.TrackArtist = track.TrackArtist != null
|
|
|
|
|
? ArtistList.FromDataArtist(track.TrackArtist,
|
|
|
|
|
ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, track.TrackArtist.RoadieId))
|
|
|
|
|
: null;
|
|
|
|
|
rmTracks.Add(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rm.Tracks = rmTracks;
|
|
|
|
|
releaseMedias.Add(rm);
|
|
|
|
|
}
|
|
|
|
|
result.Medias = releaseMedias;
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("tracks", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sw.Stop();
|
|
|
|
|
Logger.LogInformation($"ByIdAction: Release `{ release }`: includes [{includes.ToCSV()}], timings: [{ timings.ToTimings() }]");
|
|
|
|
|
return new OperationResult<Release>
|
|
|
|
|
{
|
|
|
|
|
Data = result,
|
|
|
|
|
IsSuccess = result != null,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<OperationResult<Image>> SaveImageBytes(Library.Identity.User user, Guid id, byte[] imageBytes)
|
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var errors = new List<Exception>();
|
|
|
|
|
var release = DbContext.Releases.Include(x => x.Artist).FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
if (release == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<Image>(true, $"Release Not Found [{id}]");
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
imageBytes = ImageHelper.ConvertToJpegFormat(imageBytes);
|
|
|
|
|
if (imageBytes != null)
|
|
|
|
|
{
|
|
|
|
|
// Save unaltered image to cover file
|
|
|
|
|
var coverFileName = Path.Combine(release.ReleaseFileFolder(release.Artist.ArtistFileFolder(Configuration), true), "cover.jpg");
|
|
|
|
|
File.WriteAllBytes(coverFileName, imageBytes);
|
|
|
|
|
|
|
|
|
|
release.LastUpdated = now;
|
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
CacheManager.ClearRegion(release.CacheRegion);
|
|
|
|
|
Logger.LogInformation($"SaveImageBytes `{release}` By User `{user}`");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogError(ex);
|
|
|
|
|
errors.Add(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sw.Stop();
|
|
|
|
|
|
|
|
|
|
return new OperationResult<Image>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = errors.Count == 0,
|
|
|
|
|
Data = ImageHelper.MakeThumbnailImage(Configuration, HttpContext, id, "release", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height, true),
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Errors = errors
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<OperationResult<Release>> ByIdAsync(User roadieUser, Guid id, IEnumerable<string> includes = null)
|
|
|
|
|
{
|
|
|
|
|
var timings = new Dictionary<string, long>();
|
|
|
|
|
var tsw = new Stopwatch();
|
|
|
|
|
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var cacheKey = $"urn:release_by_id_operation:{id}:{(includes == null ? "0" : string.Join("|", includes))}";
|
|
|
|
|
var result = await CacheManager.GetAsync(cacheKey, async () =>
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var rr = await ReleaseByIdAction(id, includes).ConfigureAwait(false);
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add(nameof(ReleaseByIdAction), tsw.ElapsedMilliseconds);
|
|
|
|
|
return rr;
|
|
|
|
|
}, data.Artist.CacheRegionUrn(id)).ConfigureAwait(false);
|
|
|
|
|
if (result?.Data != null && roadieUser != null)
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var release = await GetRelease(id).ConfigureAwait(false);
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("getRelease", tsw.ElapsedMilliseconds);
|
|
|
|
|
|
|
|
|
|
var userBookmarkResult = await BookmarkService.ListAsync(roadieUser, new PagedRequest(), false, BookmarkType.Release).ConfigureAwait(false);
|
|
|
|
|
if (userBookmarkResult.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x?.Bookmark?.Value == release?.RoadieId.ToString()) != null;
|
|
|
|
|
}
|
|
|
|
|
if (result.Data.Medias != null)
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
2022-03-09 03:23:46 +00:00
|
|
|
|
var user = await GetUserAsync(roadieUser.UserId).ConfigureAwait(false);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("getUser", tsw.ElapsedMilliseconds);
|
|
|
|
|
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
Parallel.ForEach(result.Data.Medias.SelectMany(x => x.Tracks), track => track.TrackPlayUrl = MakeTrackPlayUrl(user, HttpContext.BaseUrl, track.Id));
|
|
|
|
|
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("makeTrackPlayUrls", tsw.ElapsedMilliseconds);
|
|
|
|
|
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var releaseTrackIds = result.Data.Medias.SelectMany(x => x.Tracks).Select(x => x.Id).ToList();
|
|
|
|
|
var releaseUserTracks = (from ut in DbContext.UserTracks
|
|
|
|
|
join t in DbContext.Tracks on ut.TrackId equals t.Id
|
|
|
|
|
where releaseTrackIds.Contains(t.RoadieId)
|
|
|
|
|
where ut.UserId == roadieUser.Id
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
t,
|
|
|
|
|
ut
|
|
|
|
|
}).ToArray();
|
|
|
|
|
if (releaseUserTracks?.Any() == true)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
releaseTrack.UserRating = new UserTrack
|
|
|
|
|
{
|
|
|
|
|
Rating = releaseUserTrack.ut.Rating,
|
|
|
|
|
IsDisliked = releaseUserTrack.ut.IsDisliked ?? false,
|
|
|
|
|
IsFavorite = releaseUserTrack.ut.IsFavorite ?? false,
|
|
|
|
|
LastPlayed = releaseUserTrack.ut.LastPlayed,
|
|
|
|
|
PlayedCount = releaseUserTrack.ut.PlayedCount
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("userTracks", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userRelease = DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == roadieUser.Id);
|
|
|
|
|
if (userRelease != null)
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
result.Data.UserRating = new UserRelease
|
|
|
|
|
{
|
|
|
|
|
IsDisliked = userRelease.IsDisliked ?? false,
|
|
|
|
|
IsFavorite = userRelease.IsFavorite ?? false,
|
|
|
|
|
Rating = userRelease.Rating
|
|
|
|
|
};
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("userRelease", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
if (result.Data.Comments.Any())
|
2019-06-28 21:24:32 +00:00
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
2019-06-28 21:24:32 +00:00
|
|
|
|
var commentIds = result.Data.Comments.Select(x => x.DatabaseId).ToArray();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var userCommentReactions = (from cr in DbContext.CommentReactions
|
2019-06-28 21:24:32 +00:00
|
|
|
|
where commentIds.Contains(cr.CommentId)
|
|
|
|
|
where cr.UserId == roadieUser.Id
|
|
|
|
|
select cr).ToArray();
|
|
|
|
|
foreach (var comment in result.Data.Comments)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var userCommentReaction = Array.Find(userCommentReactions, x => x.CommentId == comment.DatabaseId);
|
2019-06-28 21:24:32 +00:00
|
|
|
|
comment.IsDisliked = userCommentReaction?.ReactionValue == CommentReaction.Dislike;
|
|
|
|
|
comment.IsLiked = userCommentReaction?.ReactionValue == CommentReaction.Like;
|
|
|
|
|
}
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("userComments", tsw.ElapsedMilliseconds);
|
2019-06-28 21:24:32 +00:00
|
|
|
|
}
|
2018-11-14 03:19:28 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2018-11-14 03:19:28 +00:00
|
|
|
|
sw.Stop();
|
2019-08-04 16:30:19 +00:00
|
|
|
|
Logger.LogInformation($"ById Release: `{ result?.Data }`, includes [{ includes.ToCSV() }], timings [{ timings.ToTimings() }]");
|
2019-06-30 22:14:36 +00:00
|
|
|
|
return new OperationResult<Release>(result.Messages)
|
2018-11-14 03:19:28 +00:00
|
|
|
|
{
|
|
|
|
|
Data = result?.Data,
|
2018-11-14 23:18:02 +00:00
|
|
|
|
IsNotFoundResult = result?.IsNotFoundResult ?? false,
|
2018-11-14 03:19:28 +00:00
|
|
|
|
Errors = result?.Errors,
|
|
|
|
|
IsSuccess = result?.IsSuccess ?? false,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 03:13:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// See if the given release has properties that have been modified that affect the folder structure, if so then handle
|
|
|
|
|
/// necessary operations for changes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="release">Release that has been modified</param>
|
|
|
|
|
/// <param name="oldReleaseFolder">Folder for release before any changes</param>
|
|
|
|
|
public async Task<OperationResult<bool>> CheckAndChangeReleaseTitle(data.Release release, string oldReleaseFolder)
|
|
|
|
|
{
|
|
|
|
|
SimpleContract.Requires<ArgumentNullException>(release != null, "Invalid Release");
|
|
|
|
|
SimpleContract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(oldReleaseFolder), "Invalid Release Old Folder");
|
|
|
|
|
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
var result = false;
|
|
|
|
|
var artistFolder = release.Artist.ArtistFileFolder(Configuration);
|
|
|
|
|
var newReleaseFolder = release.ReleaseFileFolder(artistFolder);
|
|
|
|
|
if (newReleaseFolder != oldReleaseFolder)
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(oldReleaseFolder))
|
|
|
|
|
{
|
|
|
|
|
Directory.Move(oldReleaseFolder, newReleaseFolder);
|
|
|
|
|
Logger.LogInformation($"Moved Release Folder From [{ oldReleaseFolder }] -> [{ newReleaseFolder }]");
|
|
|
|
|
}
|
|
|
|
|
else if (!Directory.Exists(newReleaseFolder))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(newReleaseFolder);
|
|
|
|
|
}
|
|
|
|
|
var releaseDirectoryInfo = new DirectoryInfo(newReleaseFolder);
|
|
|
|
|
// Update and move tracks under new release folder
|
|
|
|
|
foreach (var releaseMedia in DbContext.ReleaseMedias.Where(x => x.ReleaseId == release.Id).ToArray())
|
2020-01-28 03:44:13 +00:00
|
|
|
|
{
|
2019-08-06 03:13:00 +00:00
|
|
|
|
// Update the track path to have the new album title. This is needed because future scans might not work properly without updating track title.
|
|
|
|
|
foreach (var track in DbContext.Tracks.Where(x => x.ReleaseMediaId == releaseMedia.Id).ToArray())
|
|
|
|
|
{
|
2019-11-09 17:43:39 +00:00
|
|
|
|
track.FilePath = FolderPathHelper.TrackPath(Configuration, release.Artist, release, track);
|
2019-08-06 03:13:00 +00:00
|
|
|
|
var trackPath = track.PathToTrack(Configuration);
|
|
|
|
|
var trackFileInfo = new FileInfo(trackPath);
|
|
|
|
|
if (trackFileInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
// Update the tracks release tags
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var audioMetaData = await AudioMetaDataHelper.GetInfo(trackFileInfo).ConfigureAwait(false);
|
2019-08-06 03:13:00 +00:00
|
|
|
|
audioMetaData.Release = release.Title;
|
|
|
|
|
AudioMetaDataHelper.WriteTags(audioMetaData, trackFileInfo);
|
|
|
|
|
|
|
|
|
|
// Update track path
|
|
|
|
|
track.Status = Statuses.Ok;
|
|
|
|
|
track.Hash = HashHelper.CreateMD5(release.ArtistId + trackFileInfo.LastWriteTimeUtc.GetHashCode().ToString() + audioMetaData.GetHashCode());
|
|
|
|
|
track.LastUpdated = now;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
track.Hash = null;
|
|
|
|
|
track.Status = Statuses.Missing;
|
|
|
|
|
}
|
|
|
|
|
CacheManager.ClearRegion(track.CacheRegion);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
result = true;
|
2019-08-06 03:13:00 +00:00
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
}
|
2019-08-06 03:13:00 +00:00
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-08-06 03:13:00 +00:00
|
|
|
|
|
|
|
|
|
// Clean up any empty folders for the artist
|
|
|
|
|
FolderPathHelper.DeleteEmptyFoldersForArtist(Configuration, release.Artist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sw.Stop();
|
|
|
|
|
CacheManager.ClearRegion(release.CacheRegion);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (release.Artist != null)
|
|
|
|
|
{
|
|
|
|
|
CacheManager.ClearRegion(release.Artist.CacheRegion);
|
|
|
|
|
}
|
2019-08-06 03:13:00 +00:00
|
|
|
|
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = result,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<bool>> DeleteAsync(Library.Identity.User user, data.Release release, bool doDeleteFiles = false, bool doUpdateArtistCounts = true)
|
2019-08-06 03:13:00 +00:00
|
|
|
|
{
|
|
|
|
|
SimpleContract.Requires<ArgumentNullException>(release != null, "Invalid Release");
|
|
|
|
|
SimpleContract.Requires<ArgumentNullException>(release.Artist != null, "Invalid Artist");
|
|
|
|
|
|
|
|
|
|
var releaseCacheRegion = release.CacheRegion;
|
|
|
|
|
var artistCacheRegion = release.Artist.CacheRegion;
|
|
|
|
|
|
|
|
|
|
var result = false;
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
2019-11-28 17:38:26 +00:00
|
|
|
|
var releaseTracks = await (from r in DbContext.Releases
|
|
|
|
|
join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
|
|
|
|
|
join t in DbContext.Tracks on rm.Id equals t.ReleaseMediaId
|
|
|
|
|
where r.Id == release.Id
|
2020-01-28 03:44:13 +00:00
|
|
|
|
select t).ToArrayAsync().ConfigureAwait(false);
|
2019-11-28 17:38:26 +00:00
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var releaseLabelIds = await DbContext.ReleaseLabels.Where(x => x.ReleaseId == release.Id).Select(x => x.LabelId).ToArrayAsync().ConfigureAwait(false);
|
2019-11-28 17:38:26 +00:00
|
|
|
|
|
|
|
|
|
var playlistIdsWithReleaseTracks = await (from r in DbContext.Releases
|
2020-01-28 03:44:13 +00:00
|
|
|
|
join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
|
|
|
|
|
join tr in DbContext.Tracks on rm.Id equals tr.ReleaseMediaId
|
|
|
|
|
join plt in DbContext.PlaylistTracks on tr.Id equals plt.TrackId
|
|
|
|
|
where r.Id == release.Id
|
|
|
|
|
select plt.PlayListId).ToListAsync().ConfigureAwait(false);
|
2019-08-06 03:13:00 +00:00
|
|
|
|
if (doDeleteFiles)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-06-05 22:49:12 +00:00
|
|
|
|
Directory.Delete(release.ReleaseFileFolder(release.Artist.ArtistFileFolder(Configuration)), true);
|
2019-08-06 03:13:00 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogError(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DbContext.Releases.Remove(release);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var i = await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-08-06 03:13:00 +00:00
|
|
|
|
result = true;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CacheManager.ClearRegion(releaseCacheRegion);
|
|
|
|
|
CacheManager.ClearRegion(artistCacheRegion);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
Logger.LogError(ex, $"Error Clearing Cache For Release [{release.Id}] Exception [{ex.Serialize()}]");
|
2019-08-06 03:13:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
2019-11-28 17:38:26 +00:00
|
|
|
|
if (doUpdateArtistCounts)
|
|
|
|
|
{
|
|
|
|
|
var releaseArtistIds = new List<int>(release.ArtistId);
|
|
|
|
|
releaseArtistIds.AddRange(releaseTracks.Where(x => x.ArtistId.HasValue).Select(x => x.ArtistId.Value));
|
|
|
|
|
foreach (var artistId in releaseArtistIds.Distinct())
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await UpdateArtistCounts(artistId, now).ConfigureAwait(false);
|
|
|
|
|
}
|
2019-11-28 17:38:26 +00:00
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (releaseLabelIds?.Any() == true)
|
2019-11-09 17:43:39 +00:00
|
|
|
|
{
|
2019-08-06 03:13:00 +00:00
|
|
|
|
foreach (var releaseLabelId in releaseLabelIds)
|
2019-11-09 17:43:39 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await UpdateLabelCounts(releaseLabelId, now).ConfigureAwait(false);
|
2019-11-09 17:43:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-06 03:13:00 +00:00
|
|
|
|
sw.Stop();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
await BookmarkService.RemoveAllBookmarksForItemAsync(BookmarkType.Release, release.Id).ConfigureAwait(false);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
foreach (var releaseTrack in releaseTracks)
|
2019-11-28 17:38:26 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
await BookmarkService.RemoveAllBookmarksForItemAsync(BookmarkType.Track, releaseTrack.Id).ConfigureAwait(false);
|
2019-11-28 17:38:26 +00:00
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
foreach (var playlistIdWithReleaseTracks in playlistIdsWithReleaseTracks)
|
2019-11-28 17:38:26 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await UpdatePlaylistCounts(playlistIdWithReleaseTracks, now).ConfigureAwait(false);
|
2019-11-28 17:38:26 +00:00
|
|
|
|
}
|
2019-08-06 03:13:00 +00:00
|
|
|
|
Logger.LogWarning("User `{0}` deleted Release `{1}]`", user, release);
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
Data = result,
|
|
|
|
|
IsSuccess = result,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<bool>> DeleteReleasesAsync(Library.Identity.User user, IEnumerable<Guid> releaseIds, bool doDeleteFiles = false)
|
2019-08-06 03:13:00 +00:00
|
|
|
|
{
|
2019-11-28 17:38:26 +00:00
|
|
|
|
if (!releaseIds.Any())
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, "Invalid ReleaseIds");
|
|
|
|
|
}
|
2019-08-06 03:13:00 +00:00
|
|
|
|
var result = false;
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var releases = (from r in DbContext.Releases.Include(r => r.Artist)
|
|
|
|
|
where releaseIds.Contains(r.RoadieId)
|
|
|
|
|
select r
|
|
|
|
|
).ToArray();
|
|
|
|
|
|
|
|
|
|
var artistIds = releases.Select(x => x.ArtistId).Distinct().ToArray();
|
|
|
|
|
|
|
|
|
|
foreach (var release in releases)
|
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var defaultResult = await DeleteAsync(user, release, doDeleteFiles, false).ConfigureAwait(false);
|
2019-08-06 03:13:00 +00:00
|
|
|
|
result &= defaultResult.IsSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var artistId in artistIds)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await UpdateArtistCounts(artistId, now).ConfigureAwait(false);
|
2019-08-06 03:13:00 +00:00
|
|
|
|
}
|
|
|
|
|
sw.Stop();
|
|
|
|
|
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
Data = result,
|
|
|
|
|
IsSuccess = result,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<Library.Models.Pagination.PagedResult<ReleaseList>> ListAsync(User roadieUser, PagedRequest request, bool? doRandomize = false, IEnumerable<string> includes = null)
|
2018-11-14 23:18:02 +00:00
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
2019-08-03 22:59:20 +00:00
|
|
|
|
int? rowCount = null;
|
2018-11-14 23:18:02 +00:00
|
|
|
|
|
2019-06-23 15:45:45 +00:00
|
|
|
|
IQueryable<int> collectionReleaseIds = null;
|
2018-12-07 21:02:38 +00:00
|
|
|
|
if (request.FilterToCollectionId.HasValue)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
collectionReleaseIds = from cr in DbContext.CollectionReleases
|
|
|
|
|
join c in DbContext.Collections on cr.CollectionId equals c.Id
|
|
|
|
|
join r in DbContext.Releases on cr.ReleaseId equals r.Id
|
|
|
|
|
where c.RoadieId == request.FilterToCollectionId.Value
|
|
|
|
|
orderby cr.ListNumber
|
|
|
|
|
select r.Id;
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2019-06-23 15:45:45 +00:00
|
|
|
|
IQueryable<int> favoriteReleaseIds = null;
|
2018-11-21 18:19:38 +00:00
|
|
|
|
if (request.FilterFavoriteOnly)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
favoriteReleaseIds = from a in DbContext.Releases
|
|
|
|
|
join ur in DbContext.UserReleases on a.Id equals ur.ReleaseId
|
|
|
|
|
where ur.IsFavorite ?? false
|
|
|
|
|
where roadieUser == null || ur.UserId == roadieUser.Id
|
|
|
|
|
select a.Id;
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2019-06-23 15:45:45 +00:00
|
|
|
|
IQueryable<int> genreReleaseIds = null;
|
2018-12-30 04:34:32 +00:00
|
|
|
|
var isFilteredToGenre = false;
|
2019-08-06 03:13:00 +00:00
|
|
|
|
if (request.FilterToGenreId.HasValue)
|
2019-08-02 20:59:24 +00:00
|
|
|
|
{
|
|
|
|
|
genreReleaseIds = (from rg in DbContext.ReleaseGenres
|
|
|
|
|
join g in DbContext.Genres on rg.GenreId equals g.Id
|
|
|
|
|
where g.RoadieId == request.FilterToGenreId
|
|
|
|
|
select rg.ReleaseId)
|
|
|
|
|
.Distinct();
|
|
|
|
|
isFilteredToGenre = true;
|
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
else if (!string.IsNullOrEmpty(request.FilterByGenre) || (!string.IsNullOrEmpty(request.Filter) && request.Filter.StartsWith(":genre", StringComparison.OrdinalIgnoreCase)))
|
2018-11-22 23:43:02 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var genreFilter = request.FilterByGenre ?? (request.Filter ?? string.Empty).Replace(":genre ", string.Empty, StringComparison.OrdinalIgnoreCase);
|
2019-06-30 22:14:36 +00:00
|
|
|
|
genreReleaseIds = (from rg in DbContext.ReleaseGenres
|
|
|
|
|
join g in DbContext.Genres on rg.GenreId equals g.Id
|
2018-12-30 04:34:32 +00:00
|
|
|
|
where g.Name.Contains(genreFilter)
|
|
|
|
|
select rg.ReleaseId)
|
2019-08-02 20:59:24 +00:00
|
|
|
|
.Distinct();
|
2018-12-30 04:34:32 +00:00
|
|
|
|
request.Filter = null;
|
|
|
|
|
isFilteredToGenre = true;
|
2018-11-22 23:43:02 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +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";
|
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
#pragma warning disable IDE0057 // Use range operator
|
2019-03-10 16:55:21 +00:00
|
|
|
|
request.Filter = filter.Substring(1, filter.Length - 2);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
#pragma warning restore IDE0057 // Use range operator
|
2019-03-10 16:55:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var normalizedFilterValue = !string.IsNullOrEmpty(request.FilterValue)
|
|
|
|
|
? request.FilterValue.ToAlphanumericName()
|
|
|
|
|
: null;
|
2019-08-03 22:59:20 +00:00
|
|
|
|
|
|
|
|
|
int[] randomReleaseIds = null;
|
2019-11-17 00:08:44 +00:00
|
|
|
|
SortedDictionary<int, int> randomReleaseData = null;
|
2019-08-03 22:59:20 +00:00
|
|
|
|
if (doRandomize ?? false)
|
|
|
|
|
{
|
|
|
|
|
var randomLimit = request.Limit ?? roadieUser?.RandomReleaseLimit ?? request.LimitValue;
|
2020-06-07 22:46:24 +00:00
|
|
|
|
randomReleaseData = await DbContext.RandomReleaseIdsAsync(roadieUser?.Id ?? -1, randomLimit, request.FilterFavoriteOnly, request.FilterRatedOnly).ConfigureAwait(false);
|
2019-11-17 00:08:44 +00:00
|
|
|
|
randomReleaseIds = randomReleaseData.Select(x => x.Value).ToArray();
|
2019-08-03 22:59:20 +00:00
|
|
|
|
rowCount = DbContext.Releases.Count();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 20:01:45 +00:00
|
|
|
|
var result = from r in DbContext.Releases
|
2020-06-07 22:46:24 +00:00
|
|
|
|
join a in DbContext.Artists on r.ArtistId equals a.Id
|
|
|
|
|
where randomReleaseIds == null || randomReleaseIds.Contains(r.Id)
|
|
|
|
|
where request.FilterMinimumRating == null || r.Rating >= request.FilterMinimumRating.Value
|
|
|
|
|
where request.FilterToArtistId == null || r.Artist.RoadieId == request.FilterToArtistId
|
|
|
|
|
where request.FilterToCollectionId == null || collectionReleaseIds.Contains(r.Id)
|
|
|
|
|
where !request.FilterFavoriteOnly || favoriteReleaseIds.Contains(r.Id)
|
|
|
|
|
where !isFilteredToGenre || genreReleaseIds.Contains(r.Id)
|
|
|
|
|
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)
|
|
|
|
|
where string.IsNullOrEmpty(normalizedFilterValue) ||
|
|
|
|
|
r.Title.ToLower().Contains(normalizedFilterValue) ||
|
|
|
|
|
r.AlternateNames.ToLower().Contains(normalizedFilterValue)
|
|
|
|
|
where !isEqualFilter ||
|
|
|
|
|
r.Title.ToLower().Equals(normalizedFilterValue) ||
|
|
|
|
|
r.AlternateNames.ToLower().Contains(normalizedFilterValue)
|
2020-06-05 22:49:12 +00:00
|
|
|
|
select new ReleaseList
|
2020-06-07 22:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
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 = ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, a.RoadieId),
|
|
|
|
|
CreatedDate = r.CreatedDate,
|
|
|
|
|
Duration = r.Duration,
|
|
|
|
|
LastPlayed = r.LastPlayed,
|
|
|
|
|
LastUpdated = r.LastUpdated,
|
|
|
|
|
LibraryStatus = r.LibraryStatus,
|
|
|
|
|
MediaCount = r.MediaCount,
|
|
|
|
|
Rating = r.Rating,
|
|
|
|
|
Rank = (double?)r.Rank,
|
|
|
|
|
ReleaseDateDateTime = r.ReleaseDate,
|
|
|
|
|
ReleasePlayUrl = $"{HttpContext.BaseUrl}/play/release/{r.RoadieId}",
|
|
|
|
|
Status = r.Status,
|
|
|
|
|
Thumbnail = ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, r.RoadieId),
|
|
|
|
|
TrackCount = r.TrackCount,
|
|
|
|
|
TrackPlayedCount = r.PlayedCount
|
|
|
|
|
};
|
2019-08-04 04:36:36 +00:00
|
|
|
|
|
2018-11-14 23:18:02 +00:00
|
|
|
|
ReleaseList[] rows = null;
|
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
rowCount ??= result.Count();
|
2018-11-14 23:18:02 +00:00
|
|
|
|
|
|
|
|
|
if (doRandomize ?? false)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var resultData = await result.ToArrayAsync().ConfigureAwait(false);
|
2019-11-17 00:08:44 +00:00
|
|
|
|
rows = (from r in resultData
|
|
|
|
|
join ra in randomReleaseData on r.DatabaseId equals ra.Value
|
|
|
|
|
orderby ra.Key
|
|
|
|
|
select r
|
|
|
|
|
).ToArray();
|
2018-11-14 23:18:02 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string sortBy = null;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (request.ActionValue == User.ActionKeyUserRated)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2018-12-07 04:45:09 +00:00
|
|
|
|
sortBy = request.OrderValue(new Dictionary<string, string> { { "Rating", "DESC" } });
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2018-12-06 02:11:42 +00:00
|
|
|
|
else if (request.FilterToArtistId.HasValue)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2019-10-23 13:45:36 +00:00
|
|
|
|
sortBy = request.OrderValue(new Dictionary<string, string> { { "ReleaseDateDateTime", "ASC" }, { "Release.Text", "ASC" } });
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2018-11-14 23:18:02 +00:00
|
|
|
|
else
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2018-12-07 04:45:09 +00:00
|
|
|
|
sortBy = request.OrderValue(new Dictionary<string, string> { { "Release.Text", "ASC" } });
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.FilterRatedOnly)
|
|
|
|
|
{
|
|
|
|
|
result = result.Where(x => x.Rating.HasValue);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 19:47:19 +00:00
|
|
|
|
if (request.FilterMinimumRating.HasValue)
|
2019-08-03 22:59:20 +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)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
rows = await result.ToArrayAsync().ConfigureAwait(false);
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2018-12-11 04:55:36 +00:00
|
|
|
|
else
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2019-10-23 13:45:36 +00:00
|
|
|
|
sortBy = sortBy.Replace("ReleaseDate ", "ReleaseDateDateTime ");
|
|
|
|
|
|
2019-11-24 21:58:38 +00:00
|
|
|
|
rows = await result
|
|
|
|
|
.OrderBy(sortBy)
|
|
|
|
|
.Skip(request.SkipValue)
|
|
|
|
|
.Take(request.LimitValue)
|
2020-01-28 03:44:13 +00:00
|
|
|
|
.ToArrayAsync().ConfigureAwait(false);
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2018-11-14 23:18:02 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (rows.Length > 0)
|
2018-11-14 23:18:02 +00:00
|
|
|
|
{
|
2018-11-21 06:34:53 +00:00
|
|
|
|
var rowIds = rows.Select(x => x.DatabaseId).ToArray();
|
2019-11-24 21:58:38 +00:00
|
|
|
|
var genreData = await (from rg in DbContext.ReleaseGenres
|
2020-01-28 03:44:13 +00:00
|
|
|
|
join g in 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()
|
|
|
|
|
}
|
|
|
|
|
}).ToArrayAsync().ConfigureAwait(false);
|
2018-12-08 19:47:19 +00:00
|
|
|
|
|
2018-11-21 06:34:53 +00:00
|
|
|
|
foreach (var release in rows)
|
2018-11-14 23:18:02 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var genre = Array.Find(genreData, x => x.ReleaseId == release.DatabaseId);
|
2018-11-21 06:34:53 +00:00
|
|
|
|
release.Genre = genre?.dt ?? new DataToken();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 04:41:15 +00:00
|
|
|
|
if (request.FilterToCollectionId.HasValue)
|
|
|
|
|
{
|
2018-12-11 13:39:20 +00:00
|
|
|
|
var newRows = new List<ReleaseList>(rows);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var collection = await GetCollection(request.FilterToCollectionId.Value).ConfigureAwait(false);
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var collectionReleases = from c in DbContext.Collections
|
|
|
|
|
join cr in DbContext.CollectionReleases on c.Id equals cr.CollectionId
|
|
|
|
|
where c.RoadieId == request.FilterToCollectionId
|
|
|
|
|
select cr;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
foreach (var par in collection.PositionArtistReleases().ToArray())
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
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
|
2018-12-11 04:41:15 +00:00
|
|
|
|
if (cr != null)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var parRelease = Array.Find(rows, x => x.DatabaseId == cr.ReleaseId);
|
|
|
|
|
if (parRelease?.ListNumber.HasValue == false)
|
|
|
|
|
{
|
|
|
|
|
parRelease.ListNumber = par.Position;
|
|
|
|
|
}
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
2018-12-11 13:39:20 +00:00
|
|
|
|
// Release is not known add missing dummy release to rows
|
|
|
|
|
else
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
2018-12-11 13:39:20 +00:00
|
|
|
|
newRows.Add(new ReleaseList
|
2018-12-11 04:41:15 +00:00
|
|
|
|
{
|
2018-12-11 13:39:20 +00:00
|
|
|
|
Artist = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = par.Artist
|
|
|
|
|
},
|
|
|
|
|
Release = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = par.Release
|
|
|
|
|
},
|
2019-06-23 15:45:45 +00:00
|
|
|
|
Status = Statuses.Missing,
|
2019-11-24 21:58:38 +00:00
|
|
|
|
ArtistThumbnail = ImageHelper.MakeUnknownImage(HttpContext),
|
|
|
|
|
Thumbnail = ImageHelper.MakeUnknownImage(HttpContext),
|
2018-12-11 13:39:20 +00:00
|
|
|
|
ListNumber = par.Position
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2018-12-11 04:41:15 +00:00
|
|
|
|
// Resort the list for the collection by listNumber
|
2019-06-23 15:45:45 +00:00
|
|
|
|
if (request.FilterToStatusValue != Statuses.Ok)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2019-06-23 15:45:45 +00:00
|
|
|
|
newRows = newRows.Where(x => x.Status == request.FilterToStatusValue).ToList();
|
2019-08-03 22:59:20 +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-12-11 04:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 06:34:53 +00:00
|
|
|
|
if (roadieUser != null)
|
|
|
|
|
{
|
2019-11-24 21:58:38 +00:00
|
|
|
|
var userReleaseRatings = await (from ur in DbContext.UserReleases
|
|
|
|
|
where ur.UserId == roadieUser.Id
|
|
|
|
|
where rowIds.Contains(ur.ReleaseId)
|
2020-01-28 03:44:13 +00:00
|
|
|
|
select ur).ToArrayAsync().ConfigureAwait(false);
|
2018-12-08 19:47:19 +00:00
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
foreach (var userReleaseRating in userReleaseRatings.Where(x =>
|
|
|
|
|
rows.Select(r => r.DatabaseId).Contains(x.ReleaseId)))
|
2018-11-14 23:18:02 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var row = Array.Find(rows, x => x.DatabaseId == userReleaseRating.ReleaseId);
|
2018-11-21 06:34:53 +00:00
|
|
|
|
if (row != null)
|
2018-11-14 23:18:02 +00:00
|
|
|
|
{
|
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,
|
2019-06-30 22:14:36 +00:00
|
|
|
|
RatedDate = isDisliked || isFavorite
|
|
|
|
|
? (DateTime?)(userReleaseRating.LastUpdated ?? userReleaseRating.CreatedDate)
|
|
|
|
|
: null
|
2018-11-21 06:34:53 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2018-11-14 23:18:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (includes?.Any() == true && includes.Contains("tracks"))
|
2019-10-23 13:45:36 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var rowIds = rows.Select(x => x.DatabaseId).ToArray();
|
|
|
|
|
var userRatingsForResult = await (from ut in DbContext.UserTracks.Include(x => x.Track)
|
|
|
|
|
join t in DbContext.Tracks on ut.TrackId equals t.Id
|
|
|
|
|
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
where rowIds.Contains(rm.ReleaseId)
|
|
|
|
|
where ut.UserId == roadieUser.Id
|
|
|
|
|
select ut)
|
|
|
|
|
.ToArrayAsync().ConfigureAwait(false) ?? new data.UserTrack[0];
|
|
|
|
|
|
|
|
|
|
foreach (var release in rows)
|
2019-10-23 13:45:36 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
release.Media = DbContext.ReleaseMedias
|
|
|
|
|
.Include(x => x.Tracks)
|
|
|
|
|
.Where(x => x.ReleaseId == release.DatabaseId)
|
|
|
|
|
.ToArray()
|
|
|
|
|
.AsQueryable()
|
|
|
|
|
.ProjectToType<ReleaseMediaList>()
|
|
|
|
|
.OrderBy(x => x.MediaNumber)
|
|
|
|
|
.ToArray(); // Async operation on Project Mapping async throws error
|
2019-10-23 13:45:36 +00:00
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
Parallel.ForEach(release.Media, (media) =>
|
2018-12-06 02:11:42 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
media.Tracks = media.Tracks.OrderBy(x => x.TrackNumber);
|
|
|
|
|
var userTracksForMedia = userRatingsForResult.Where(x => x.Track.ReleaseMediaId == media.DatabaseId).ToArray();
|
|
|
|
|
if (userTracksForMedia.Length > 0)
|
2018-12-06 02:11:42 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
Parallel.ForEach(userTracksForMedia, (userTrack) =>
|
2019-10-23 13:45:36 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var mediaTrack = media.Tracks.FirstOrDefault(x => x.DatabaseId == userTrack.TrackId);
|
|
|
|
|
if (mediaTrack != null)
|
2018-12-06 02:11:42 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
mediaTrack.UserRating = new UserTrack
|
2019-10-23 13:45:36 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
Rating = userTrack.Rating,
|
|
|
|
|
IsFavorite = userTrack.IsFavorite ?? false,
|
|
|
|
|
IsDisliked = userTrack.IsDisliked ?? false
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-10-23 13:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-11-24 21:58:38 +00:00
|
|
|
|
if (request.FilterFavoriteOnly)
|
|
|
|
|
{
|
|
|
|
|
rows = rows.OrderBy(x => x.UserRating.Rating).ToArray();
|
|
|
|
|
}
|
2018-11-14 23:18:02 +00:00
|
|
|
|
sw.Stop();
|
2019-11-17 20:02:19 +00:00
|
|
|
|
return new Library.Models.Pagination.PagedResult<ReleaseList>
|
2018-11-14 23:18:02 +00:00
|
|
|
|
{
|
2019-08-03 22:59:20 +00:00
|
|
|
|
TotalCount = rowCount.Value,
|
2018-11-14 23:18:02 +00:00
|
|
|
|
CurrentPage = request.PageValue,
|
|
|
|
|
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Rows = rows
|
2019-11-17 20:02:19 +00:00
|
|
|
|
};
|
2018-11-14 23:18:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<bool>> MergeReleasesAsync(Library.Identity.User user, Guid releaseToMergeId, Guid releaseToMergeIntoId, bool addAsMedia)
|
2019-01-22 00:04:58 +00:00
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
|
|
|
|
var errors = new List<Exception>();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var releaseToMerge = 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);
|
2019-01-22 00:04:58 +00:00
|
|
|
|
if (releaseToMerge == null)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogWarning("MergeReleases Unknown Release [{0}]", releaseToMergeId);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
return new OperationResult<bool>(true, $"Release Not Found [{releaseToMergeId}]");
|
2019-01-22 00:04:58 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
|
|
|
|
var releaseToMergeInfo = 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);
|
2019-01-22 00:04:58 +00:00
|
|
|
|
if (releaseToMergeInfo == null)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogWarning("MergeReleases Unknown Release [{0}]", releaseToMergeIntoId);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
return new OperationResult<bool>(true, $"Release Not Found [{releaseToMergeIntoId}]");
|
2019-01-22 00:04:58 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-01-22 00:04:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
await MergeReleasesAsync(user, releaseToMerge, releaseToMergeInfo, addAsMedia).ConfigureAwait(false);
|
2019-01-22 00:04:58 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogError(ex);
|
2019-01-22 00:04:58 +00:00
|
|
|
|
errors.Add(ex);
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-01-22 00:04:58 +00:00
|
|
|
|
sw.Stop();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogInformation("MergeReleases Release `{0}` Merged Into Release `{1}`, By User `{2}`",
|
|
|
|
|
releaseToMerge, releaseToMergeInfo, user);
|
2019-01-22 00:04:58 +00:00
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
IsSuccess = errors.Count == 0,
|
|
|
|
|
Data = errors.Count == 0,
|
2019-01-22 00:04:58 +00:00
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Errors = errors
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 03:16:33 +00:00
|
|
|
|
/// <summary>
|
2019-09-30 02:03:00 +00:00
|
|
|
|
/// Merge one release into another one
|
2019-07-07 03:16:33 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="releaseToMerge">The release to be merged</param>
|
|
|
|
|
/// <param name="releaseToMergeInto">The release to merge into</param>
|
|
|
|
|
/// <param name="addAsMedia">If true then add a ReleaseMedia to the release to be merged into</param>
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<bool>> MergeReleasesAsync(Library.Identity.User user, data.Release releaseToMerge, data.Release releaseToMergeInto, bool addAsMedia)
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
|
|
|
|
SimpleContract.Requires<ArgumentNullException>(releaseToMerge != null, "Invalid Release");
|
|
|
|
|
SimpleContract.Requires<ArgumentNullException>(releaseToMergeInto != null, "Invalid Release");
|
|
|
|
|
SimpleContract.Requires<ArgumentNullException>(releaseToMerge.Artist != null, "Invalid Artist");
|
|
|
|
|
SimpleContract.Requires<ArgumentNullException>(releaseToMergeInto.Artist != null, "Invalid Artist");
|
|
|
|
|
var result = false;
|
|
|
|
|
var resultErrors = new List<Exception>();
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var mergedTracksToMove = new List<data.Track>();
|
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
releaseToMergeInto.MediaCount ??= 0;
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
2019-07-31 16:44:25 +00:00
|
|
|
|
var releaseToMergeReleaseMedia = DbContext.ReleaseMedias.Where(x => x.ReleaseId == releaseToMerge.Id).ToList();
|
|
|
|
|
var releaseToMergeIntoReleaseMedia = DbContext.ReleaseMedias.Where(x => x.ReleaseId == releaseToMergeInto.Id).ToList();
|
2019-07-07 03:16:33 +00:00
|
|
|
|
var releaseToMergeIntoLastMediaNumber = releaseToMergeIntoReleaseMedia.Max(x => x.MediaNumber);
|
|
|
|
|
|
|
|
|
|
// Add new ReleaseMedia
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (addAsMedia || releaseToMergeIntoReleaseMedia.Count == 0)
|
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
foreach (var rm in releaseToMergeReleaseMedia)
|
|
|
|
|
{
|
|
|
|
|
releaseToMergeIntoLastMediaNumber++;
|
|
|
|
|
rm.ReleaseId = releaseToMergeInto.Id;
|
|
|
|
|
rm.MediaNumber = releaseToMergeIntoLastMediaNumber;
|
|
|
|
|
rm.LastUpdated = now;
|
|
|
|
|
releaseToMergeInto.MediaCount++;
|
|
|
|
|
releaseToMergeInto.TrackCount += rm.TrackCount;
|
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
// Merge into existing ReleaseMedia
|
|
|
|
|
else
|
2020-01-28 03:44:13 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
// See if each media exists and merge details of each including tracks
|
|
|
|
|
foreach (var rm in releaseToMergeReleaseMedia)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var existingReleaseMedia = releaseToMergeIntoReleaseMedia.Find(x => x.MediaNumber == rm.MediaNumber);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
var mergeTracks = DbContext.Tracks.Where(x => x.ReleaseMediaId == rm.Id).ToArray();
|
|
|
|
|
if (existingReleaseMedia == null)
|
|
|
|
|
{
|
|
|
|
|
releaseToMergeIntoLastMediaNumber++;
|
|
|
|
|
// Doesnt exist in release being merged to add
|
|
|
|
|
rm.ReleaseId = releaseToMergeInto.Id;
|
|
|
|
|
rm.MediaNumber = releaseToMergeIntoLastMediaNumber;
|
|
|
|
|
rm.LastUpdated = now;
|
|
|
|
|
releaseToMergeInto.MediaCount++;
|
|
|
|
|
releaseToMergeInto.TrackCount += rm.TrackCount;
|
|
|
|
|
mergedTracksToMove.AddRange(mergeTracks);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// ReleaseMedia Does exist merge tracks and details
|
|
|
|
|
|
2019-08-11 19:11:29 +00:00
|
|
|
|
var mergeIntoTracks = DbContext.Tracks.Where(x => x.ReleaseMediaId == existingReleaseMedia.Id).ToArray();
|
2019-07-07 03:16:33 +00:00
|
|
|
|
foreach (var mergeTrack in mergeTracks)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var existingTrack = Array.Find(mergeIntoTracks, x => x.TrackNumber == mergeTrack.TrackNumber);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (existingTrack == null)
|
|
|
|
|
{
|
|
|
|
|
// Track does not exist, update to existing ReleaseMedia and update ReleaseToMergeInfo counts
|
|
|
|
|
mergeTrack.LastUpdated = now;
|
|
|
|
|
mergeTrack.ReleaseMediaId = existingReleaseMedia.Id;
|
|
|
|
|
existingReleaseMedia.TrackCount++;
|
|
|
|
|
existingReleaseMedia.LastUpdated = now;
|
|
|
|
|
releaseToMergeInto.TrackCount++;
|
|
|
|
|
mergedTracksToMove.Add(mergeTrack);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Track does exist merge two tracks together
|
2020-01-28 03:44:13 +00:00
|
|
|
|
existingTrack.MusicBrainzId ??= mergeTrack.MusicBrainzId;
|
|
|
|
|
existingTrack.SpotifyId ??= mergeTrack.SpotifyId;
|
|
|
|
|
existingTrack.AmgId ??= mergeTrack.AmgId;
|
|
|
|
|
existingTrack.ISRC ??= mergeTrack.ISRC;
|
|
|
|
|
existingTrack.AmgId ??= mergeTrack.AmgId;
|
|
|
|
|
existingTrack.LastFMId ??= mergeTrack.LastFMId;
|
|
|
|
|
existingTrack.PartTitles ??= mergeTrack.PartTitles;
|
2019-07-31 16:44:25 +00:00
|
|
|
|
existingTrack.PlayedCount = (existingTrack.PlayedCount ?? 0) + (mergeTrack.PlayedCount ?? 0);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (mergeTrack.LastPlayed.HasValue && existingTrack.LastPlayed.HasValue &&
|
|
|
|
|
mergeTrack.LastPlayed > existingTrack.LastPlayed)
|
2020-01-28 03:44:13 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
existingTrack.LastPlayed = mergeTrack.LastPlayed;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
existingTrack.MusicBrainzId ??= mergeTrack.MusicBrainzId;
|
2019-07-31 16:44:25 +00:00
|
|
|
|
existingTrack.Tags = existingTrack.Tags.AddToDelimitedList(mergeTrack.Tags.ToListFromDelimited());
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (!mergeTrack.Title.Equals(existingTrack.Title,
|
|
|
|
|
StringComparison.OrdinalIgnoreCase))
|
2020-01-28 03:44:13 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
existingTrack.AlternateNames =
|
|
|
|
|
existingTrack.AlternateNames.AddToDelimitedList(new[]
|
|
|
|
|
{mergeTrack.Title, mergeTrack.Title.ToAlphanumericName()});
|
2020-01-28 03:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 16:44:25 +00:00
|
|
|
|
existingTrack.AlternateNames = existingTrack.AlternateNames.AddToDelimitedList(mergeTrack.AlternateNames.ToListFromDelimited());
|
2019-07-07 03:16:33 +00:00
|
|
|
|
existingTrack.LastUpdated = now;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
|
|
|
|
var releaseToMergeFolder = releaseToMerge.ReleaseFileFolder(releaseToMerge.Artist.ArtistFileFolder(Configuration));
|
|
|
|
|
var releaseToMergeIntoArtistFolder = releaseToMergeInto.Artist.ArtistFileFolder(Configuration);
|
|
|
|
|
var releaseToMergeIntoDirectory = new DirectoryInfo(releaseToMergeInto.ReleaseFileFolder(releaseToMergeIntoArtistFolder));
|
|
|
|
|
|
|
|
|
|
// Move tracks for releaseToMergeInto into correct folders
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (mergedTracksToMove.Count > 0)
|
2019-09-30 02:03:00 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
foreach (var track in mergedTracksToMove)
|
|
|
|
|
{
|
|
|
|
|
var oldTrackPath = track.PathToTrack(Configuration);
|
2019-09-30 02:03:00 +00:00
|
|
|
|
var newTrackPath = FolderPathHelper.TrackFullPath(Configuration, releaseToMergeInto.Artist, releaseToMergeInto, track);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
var trackFile = new FileInfo(oldTrackPath);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (!newTrackPath.Equals(oldTrackPath, StringComparison.OrdinalIgnoreCase))
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var audioMetaData = await AudioMetaDataHelper.GetInfo(trackFile).ConfigureAwait(false);
|
2019-08-01 00:10:00 +00:00
|
|
|
|
track.FilePath = FolderPathHelper.TrackPath(Configuration, releaseToMergeInto.Artist, releaseToMergeInto, track);
|
|
|
|
|
track.Hash = HashHelper.CreateMD5(releaseToMergeInto.ArtistId + trackFile.LastWriteTimeUtc.GetHashCode().ToString() + audioMetaData.GetHashCode());
|
2019-07-07 03:16:33 +00:00
|
|
|
|
track.LastUpdated = now;
|
|
|
|
|
File.Move(oldTrackPath, newTrackPath);
|
2020-06-05 22:49:12 +00:00
|
|
|
|
|
|
|
|
|
// If track has track image move into new folder
|
|
|
|
|
var trackImageFileName = track.PathToTrackThumbnail(Configuration);
|
|
|
|
|
if (File.Exists(trackImageFileName))
|
|
|
|
|
{
|
|
|
|
|
File.Move(trackImageFileName, newTrackPath);
|
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-30 02:03:00 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
|
|
|
|
// Cleanup folders
|
2020-01-28 03:44:13 +00:00
|
|
|
|
FileDirectoryProcessorService.DeleteEmptyFolders(new DirectoryInfo(releaseToMergeIntoArtistFolder), Logger);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
|
|
|
|
// Now Merge release details
|
|
|
|
|
releaseToMergeInto.AlternateNames = releaseToMergeInto.AlternateNames.AddToDelimitedList(new[]
|
|
|
|
|
{releaseToMerge.Title, releaseToMerge.Title.ToAlphanumericName()});
|
|
|
|
|
releaseToMergeInto.AlternateNames =
|
|
|
|
|
releaseToMergeInto.AlternateNames.AddToDelimitedList(releaseToMerge.AlternateNames
|
|
|
|
|
.ToListFromDelimited());
|
|
|
|
|
releaseToMergeInto.Tags =
|
|
|
|
|
releaseToMergeInto.Tags.AddToDelimitedList(releaseToMerge.Tags.ToListFromDelimited());
|
|
|
|
|
releaseToMergeInto.URLs.AddToDelimitedList(releaseToMerge.URLs.ToListFromDelimited());
|
2020-01-28 03:44:13 +00:00
|
|
|
|
releaseToMergeInto.MusicBrainzId ??= releaseToMerge.MusicBrainzId;
|
|
|
|
|
releaseToMergeInto.Profile ??= releaseToMerge.Profile;
|
|
|
|
|
releaseToMergeInto.ReleaseDate ??= releaseToMerge.ReleaseDate;
|
|
|
|
|
releaseToMergeInto.MusicBrainzId ??= releaseToMerge.MusicBrainzId;
|
|
|
|
|
releaseToMergeInto.DiscogsId ??= releaseToMerge.DiscogsId;
|
|
|
|
|
releaseToMergeInto.ITunesId ??= releaseToMerge.ITunesId;
|
|
|
|
|
releaseToMergeInto.AmgId ??= releaseToMerge.AmgId;
|
|
|
|
|
releaseToMergeInto.LastFMId ??= releaseToMerge.LastFMId;
|
|
|
|
|
releaseToMergeInto.LastFMSummary ??= releaseToMerge.LastFMSummary;
|
|
|
|
|
releaseToMergeInto.SpotifyId ??= releaseToMerge.SpotifyId;
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (releaseToMergeInto.ReleaseType == ReleaseType.Unknown &&
|
|
|
|
|
releaseToMerge.ReleaseType != ReleaseType.Unknown)
|
2020-01-28 03:44:13 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
releaseToMergeInto.ReleaseType = releaseToMerge.ReleaseType;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 03:16:33 +00:00
|
|
|
|
releaseToMergeInto.LastUpdated = now;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
|
|
|
|
// Update any collection pointers for release to be merged
|
|
|
|
|
var collectionRecords = DbContext.CollectionReleases.Where(x => x.ReleaseId == releaseToMerge.Id);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (collectionRecords?.Any() == true)
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var cr in collectionRecords)
|
|
|
|
|
{
|
|
|
|
|
cr.ReleaseId = releaseToMergeInto.Id;
|
|
|
|
|
cr.LastUpdated = now;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update any existing playlist for release to be merged
|
|
|
|
|
var playListTrackInfos = (from pl in DbContext.PlaylistTracks
|
|
|
|
|
join t in DbContext.Tracks on pl.TrackId equals t.Id
|
|
|
|
|
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
where rm.ReleaseId == releaseToMerge.Id
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
track = t,
|
|
|
|
|
rm,
|
|
|
|
|
pl
|
|
|
|
|
}).ToArray();
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (playListTrackInfos?.Any() == true)
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var playListTrackInfo in playListTrackInfos)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var matchingTrack = await (from t in DbContext.Tracks
|
|
|
|
|
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
where rm.ReleaseId == releaseToMergeInto.Id
|
|
|
|
|
where rm.MediaNumber == playListTrackInfo.rm.MediaNumber
|
|
|
|
|
where t.TrackNumber == playListTrackInfo.track.TrackNumber
|
|
|
|
|
select t).FirstOrDefaultAsync().ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (matchingTrack != null)
|
|
|
|
|
{
|
|
|
|
|
playListTrackInfo.pl.TrackId = matchingTrack.Id;
|
|
|
|
|
playListTrackInfo.pl.LastUpdated = now;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
await DeleteAsync(user, releaseToMerge).ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
2020-06-05 22:49:12 +00:00
|
|
|
|
// Merge all release images
|
|
|
|
|
var releaseCoverFiles = ImageHelper.FindImageTypeInDirectory(new DirectoryInfo(releaseToMergeFolder), ImageType.Release);
|
|
|
|
|
if (releaseCoverFiles.Any())
|
2020-01-28 03:44:13 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
foreach (var releaseCoverFile in releaseCoverFiles)
|
2020-01-28 03:44:13 +00:00
|
|
|
|
{
|
2020-06-05 22:49:12 +00:00
|
|
|
|
var imagePath = Path.Combine(releaseToMergeIntoDirectory.FullName, releaseCoverFile.Name);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (File.Exists(imagePath))
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (ImageHelper.IsImageBetterQuality(releaseCoverFile.FullName, imagePath))
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
2020-06-05 22:49:12 +00:00
|
|
|
|
releaseCoverFile.MoveTo(imagePath, true);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-05 22:49:12 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
releaseCoverFile.MoveTo(imagePath);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
}
|
2020-06-05 22:49:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var secondaryReleaseCoverFiles = ImageHelper.FindImageTypeInDirectory(new DirectoryInfo(releaseToMergeFolder), ImageType.ReleaseSecondary);
|
|
|
|
|
if (secondaryReleaseCoverFiles.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var secondaryReleaseCoverFile in secondaryReleaseCoverFiles)
|
|
|
|
|
{
|
|
|
|
|
var imagePath = Path.Combine(releaseToMergeIntoDirectory.FullName, secondaryReleaseCoverFile.Name);
|
|
|
|
|
if (File.Exists(imagePath))
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
2020-06-05 22:49:12 +00:00
|
|
|
|
if (ImageHelper.IsImageBetterQuality(secondaryReleaseCoverFile.FullName, imagePath))
|
|
|
|
|
{
|
|
|
|
|
secondaryReleaseCoverFile.MoveTo(imagePath, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
secondaryReleaseCoverFile.MoveTo(imagePath, true);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
2020-06-05 22:49:12 +00:00
|
|
|
|
var releaseToMergeInfoDirectory = new DirectoryInfo(releaseToMergeFolder);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (releaseToMergeInfoDirectory.Exists)
|
2020-06-05 22:49:12 +00:00
|
|
|
|
{
|
|
|
|
|
releaseToMergeInfoDirectory.Delete(true);
|
|
|
|
|
Logger.LogWarning("x Deleted Folder [{0}]", releaseToMergeInfoDirectory);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
}
|
2020-06-05 22:49:12 +00:00
|
|
|
|
|
2019-07-07 03:16:33 +00:00
|
|
|
|
// Clear cache regions for manipulated records
|
|
|
|
|
CacheManager.ClearRegion(releaseToMergeInto.CacheRegion);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (releaseToMergeInto.Artist != null)
|
|
|
|
|
{
|
|
|
|
|
CacheManager.ClearRegion(releaseToMergeInto.Artist.CacheRegion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (releaseToMerge.Artist != null)
|
|
|
|
|
{
|
|
|
|
|
CacheManager.ClearRegion(releaseToMerge.Artist.CacheRegion);
|
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
2020-06-05 22:49:12 +00:00
|
|
|
|
// Rescan release
|
2020-06-07 22:46:24 +00:00
|
|
|
|
await ScanReleaseFolderAsync(user, releaseToMergeInto.RoadieId, false, releaseToMergeInto).ConfigureAwait(false);
|
2020-06-05 22:49:12 +00:00
|
|
|
|
|
2019-07-07 03:16:33 +00:00
|
|
|
|
sw.Stop();
|
|
|
|
|
result = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogError(ex,
|
|
|
|
|
$"MergeReleases ReleaseToMerge `{releaseToMerge}`, ReleaseToMergeInto `{releaseToMergeInto}`, addAsMedia [{addAsMedia}]");
|
|
|
|
|
resultErrors.Add(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
Data = result,
|
|
|
|
|
IsSuccess = result,
|
|
|
|
|
Errors = resultErrors,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<FileOperationResult<byte[]>> ReleaseZippedAsync(User roadieUser, Guid id)
|
2018-11-24 16:18:03 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var release = await GetRelease(id).ConfigureAwait(false);
|
2018-11-24 16:18:03 +00:00
|
|
|
|
if (release == null)
|
2020-06-07 22:46:24 +00:00
|
|
|
|
{
|
|
|
|
|
return new FileOperationResult<byte[]>(true, $"Release Not Found [{id}]");
|
|
|
|
|
}
|
2018-11-24 16:18:03 +00:00
|
|
|
|
|
|
|
|
|
byte[] zipBytes = null;
|
|
|
|
|
string zipFileName = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
var artistFolder = release.Artist.ArtistFileFolder(Configuration);
|
2018-11-24 16:18:03 +00:00
|
|
|
|
var releaseFolder = release.ReleaseFileFolder(artistFolder);
|
2018-12-08 19:47:19 +00:00
|
|
|
|
if (!Directory.Exists(releaseFolder))
|
2018-11-24 16:18:03 +00:00
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogCritical($"Release Folder [{releaseFolder}] not found for Release `{release}`");
|
2020-06-07 22:46:24 +00:00
|
|
|
|
return new FileOperationResult<byte[]>(true, $"Release Folder Not Found [{id}]");
|
2018-11-24 16:18:03 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2018-11-24 16:18:03 +00:00
|
|
|
|
var releaseFiles = Directory.GetFiles(releaseFolder);
|
2019-06-30 22:14:36 +00:00
|
|
|
|
using (var zipStream = new MemoryStream())
|
2018-11-24 16:18:03 +00:00
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
using (var zip = new ZipArchive(zipStream, ZipArchiveMode.Create))
|
2018-11-24 16:18:03 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var releaseFile in releaseFiles)
|
|
|
|
|
{
|
|
|
|
|
var fileInfo = new FileInfo(releaseFile);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (string.Equals(fileInfo.Extension, ".mp3", StringComparison.OrdinalIgnoreCase) || string.Equals(fileInfo.Extension, ".jpg", StringComparison.OrdinalIgnoreCase))
|
2018-11-24 16:18:03 +00:00
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var entry = zip.CreateEntry(fileInfo.Name);
|
|
|
|
|
using (var entryStream = entry.Open())
|
2018-11-24 16:18:03 +00:00
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
using (var s = fileInfo.OpenRead())
|
2018-11-24 16:18:03 +00:00
|
|
|
|
{
|
|
|
|
|
s.CopyTo(entryStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2018-11-24 16:18:03 +00:00
|
|
|
|
zipBytes = zipStream.ToArray();
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-11-09 17:43:39 +00:00
|
|
|
|
zipFileName = $"{release.Artist.Name}_{release.SortTitleValue}.zip".ToFileNameFriendly();
|
2019-08-03 22:59:20 +00:00
|
|
|
|
Logger.LogTrace(
|
2019-06-30 22:14:36 +00:00
|
|
|
|
$"User `{roadieUser}` downloaded Release `{release}` ZipFileName [{zipFileName}], Zip Size [{zipBytes?.Length}]");
|
2018-11-24 16:18:03 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogError(ex, "Error creating zip for Release `{0}`", release.ToString());
|
2018-11-24 16:18:03 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-11-24 21:58:38 +00:00
|
|
|
|
return new FileOperationResult<byte[]>
|
2018-11-24 16:18:03 +00:00
|
|
|
|
{
|
|
|
|
|
IsSuccess = zipBytes != null,
|
|
|
|
|
Data = zipBytes,
|
2018-12-08 19:47:19 +00:00
|
|
|
|
AdditionalData = new Dictionary<string, object> { { "ZipFileName", zipFileName } }
|
2019-11-24 21:58:38 +00:00
|
|
|
|
};
|
2018-11-24 16:18:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 03:16:33 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// For the given ReleaseId, Scan folder adding new, removing not found and updating DB tracks for tracks found
|
|
|
|
|
/// </summary>
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<bool>> ScanReleaseFolderAsync(Library.Identity.User user, Guid releaseId, bool doJustInfo, data.Release releaseToScan = null)
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
SimpleContract.Requires<ArgumentOutOfRangeException>((releaseId != Guid.Empty && releaseToScan == null) || releaseToScan != null, "Invalid ReleaseId");
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
|
|
|
|
_addedTrackIds.Clear();
|
|
|
|
|
|
|
|
|
|
var result = false;
|
|
|
|
|
var resultErrors = new List<Exception>();
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var modifiedRelease = false;
|
|
|
|
|
string releasePath = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var release = releaseToScan ?? DbContext.Releases
|
|
|
|
|
.Include(x => x.Artist)
|
|
|
|
|
.Include(x => x.Labels)
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == releaseId);
|
|
|
|
|
if (release == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogCritical("Unable To Find Release [{0}]", releaseId);
|
|
|
|
|
return new OperationResult<bool>();
|
|
|
|
|
}
|
2019-08-06 03:13:00 +00:00
|
|
|
|
var now = DateTime.UtcNow;
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
|
|
|
|
// This is recorded from metadata and if set then used to gauage if the release is complete
|
|
|
|
|
short? totalTrackCount = null;
|
|
|
|
|
short totalMissingCount = 0;
|
|
|
|
|
releasePath = release.ReleaseFileFolder(release.Artist.ArtistFileFolder(Configuration));
|
|
|
|
|
var releaseDirectory = new DirectoryInfo(releasePath);
|
2019-11-18 17:26:27 +00:00
|
|
|
|
data.Track firstTrack = null;
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (!Directory.Exists(releasePath))
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-08-06 03:13:00 +00:00
|
|
|
|
// Get the First Track for the Release from DB (if any) and see if the path on it is set, if so rename to "correct" path
|
|
|
|
|
// this happens with the logic change from folder naming and almost always case change which causes issues with Linux systems.
|
2019-11-18 17:26:27 +00:00
|
|
|
|
firstTrack = (from t in DbContext.Tracks
|
2020-01-28 03:44:13 +00:00
|
|
|
|
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
where rm.ReleaseId == release.Id
|
|
|
|
|
select t).FirstOrDefault();
|
2019-08-07 13:44:26 +00:00
|
|
|
|
if (firstTrack?.FilePath != null)
|
2019-08-06 03:13:00 +00:00
|
|
|
|
{
|
|
|
|
|
var trackPath = Path.GetDirectoryName(firstTrack.PathToTrack(Configuration));
|
|
|
|
|
if (Directory.Exists(trackPath) && !releasePath.Equals(trackPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.Move(trackPath, releasePath);
|
|
|
|
|
var releaseTracks = (from t in DbContext.Tracks
|
|
|
|
|
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
where rm.ReleaseId == release.Id
|
|
|
|
|
select t).ToArray();
|
|
|
|
|
foreach (var releaseTrack in releaseTracks)
|
|
|
|
|
{
|
2019-08-06 13:50:25 +00:00
|
|
|
|
releaseTrack.FilePath = FolderPathHelper.TrackPath(Configuration, release.Artist, release, releaseTrack);
|
|
|
|
|
if (!releaseTrack.DoesFileForTrackExist(Configuration))
|
|
|
|
|
{
|
|
|
|
|
releaseTrack.Hash = null;
|
|
|
|
|
releaseTrack.Status = Statuses.Missing;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
releaseTrack.Hash = HashHelper.CreateMD5(release.RoadieId + releaseTrack.GetHashCode().ToString());
|
|
|
|
|
releaseTrack.Status = Statuses.Ok;
|
|
|
|
|
}
|
2019-08-06 03:13:00 +00:00
|
|
|
|
releaseTrack.LastUpdated = now;
|
|
|
|
|
}
|
|
|
|
|
release.LastUpdated = now;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-08-06 03:13:00 +00:00
|
|
|
|
Logger.LogWarning($"Release `{ release }`: Updated Release Track Path From [{ trackPath }] to [{ releasePath }]");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.LogWarning($"Unable To Find Release Folder [{releasePath}], and Track path [{ trackPath }] For Release `{release}`");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.LogWarning($"Unable To Find Release Folder [{releasePath}] For Release `{release}`");
|
|
|
|
|
}
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-08-07 13:44:26 +00:00
|
|
|
|
|
2019-07-07 03:16:33 +00:00
|
|
|
|
#region Get Tracks for Release from DB and set as missing any not found in Folder
|
|
|
|
|
|
|
|
|
|
foreach (var releaseMedia in DbContext.ReleaseMedias.Where(x => x.ReleaseId == release.Id).ToArray())
|
|
|
|
|
{
|
|
|
|
|
var foundMissingTracks = false;
|
2019-07-31 22:42:49 +00:00
|
|
|
|
foreach (var existingTrack in DbContext.Tracks.Where(x => x.ReleaseMediaId == releaseMedia.Id).ToArray())
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
|
|
|
|
var trackPath = existingTrack.PathToTrack(Configuration);
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(trackPath))
|
|
|
|
|
{
|
2019-07-31 22:42:49 +00:00
|
|
|
|
Logger.LogWarning("Track `{0}`, File [{1}] Not Found.", existingTrack.ToString(), trackPath);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (!doJustInfo)
|
|
|
|
|
{
|
|
|
|
|
existingTrack.UpdateTrackMissingFile(now);
|
|
|
|
|
foundMissingTracks = true;
|
|
|
|
|
modifiedRelease = true;
|
|
|
|
|
totalMissingCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 22:42:49 +00:00
|
|
|
|
if (foundMissingTracks)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion Get Tracks for Release from DB and set as missing any not found in Folder
|
|
|
|
|
|
|
|
|
|
#region Scan Folder and Add or Update Existing Tracks from Files
|
|
|
|
|
|
2019-08-07 13:44:26 +00:00
|
|
|
|
var existingReleaseMedia = DbContext.ReleaseMedias.Include(x => x.Tracks)
|
|
|
|
|
.Where(x => x.ReleaseId == release.Id)
|
|
|
|
|
.ToList();
|
2019-07-07 03:16:33 +00:00
|
|
|
|
var foundInFolderTracks = new List<data.Track>();
|
|
|
|
|
short totalNumberOfTracksFound = 0;
|
|
|
|
|
// This is the number of tracks metadata says the release should have (releaseMediaNumber, TotalNumberOfTracks)
|
|
|
|
|
var releaseMediaTotalNumberOfTracks = new Dictionary<short, short?>();
|
|
|
|
|
var releaseMediaTracksFound = new Dictionary<int, short>();
|
|
|
|
|
if (Directory.Exists(releasePath))
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
foreach (var file in releaseDirectory.GetFiles("*.mp3", SearchOption.AllDirectories))
|
|
|
|
|
{
|
|
|
|
|
int? trackArtistId = null;
|
|
|
|
|
string partTitles = null;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var audioMetaData = await AudioMetaDataHelper.GetInfo(file, doJustInfo).ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
// This is the path for the new track not in the database but the found MP3 file to be added to library
|
2019-11-09 17:43:39 +00:00
|
|
|
|
var trackPath = FolderPathHelper.TrackPath(Configuration, release.Artist.Id, release.Artist.SortNameValue, release.SortTitleValue, release.ReleaseDate.Value, audioMetaData.Title, audioMetaData.TrackNumber.Value);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
|
|
|
|
if (audioMetaData.IsValid)
|
|
|
|
|
{
|
2019-07-31 22:42:49 +00:00
|
|
|
|
var trackHash = HashHelper.CreateMD5(release.ArtistId + file.LastWriteTimeUtc.GetHashCode().ToString() + audioMetaData.GetHashCode());
|
2019-07-07 03:16:33 +00:00
|
|
|
|
totalNumberOfTracksFound++;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
totalTrackCount ??= (short)(audioMetaData.TotalTrackNumbers ?? 0);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
var releaseMediaNumber = (short)(audioMetaData.Disc ?? 1);
|
|
|
|
|
if (!releaseMediaTotalNumberOfTracks.ContainsKey(releaseMediaNumber))
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
|
|
|
|
releaseMediaTotalNumberOfTracks.Add(releaseMediaNumber, (short)(audioMetaData.TotalTrackNumbers ?? 0));
|
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
else
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
|
|
|
|
releaseMediaTotalNumberOfTracks[releaseMediaNumber] = releaseMediaTotalNumberOfTracks[releaseMediaNumber].TakeLarger((short)(audioMetaData.TotalTrackNumbers ?? 0));
|
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var releaseMedia = existingReleaseMedia.Find(x => x.MediaNumber == releaseMediaNumber);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (releaseMedia == null)
|
|
|
|
|
{
|
|
|
|
|
// New ReleaseMedia - Not Found In Database
|
|
|
|
|
releaseMedia = new data.ReleaseMedia
|
|
|
|
|
{
|
|
|
|
|
ReleaseId = release.Id,
|
|
|
|
|
Status = Statuses.Incomplete,
|
|
|
|
|
MediaNumber = releaseMediaNumber
|
|
|
|
|
};
|
|
|
|
|
DbContext.ReleaseMedias.Add(releaseMedia);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
existingReleaseMedia.Add(releaseMedia);
|
|
|
|
|
modifiedRelease = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Existing ReleaseMedia Found
|
|
|
|
|
releaseMedia.LastUpdated = now;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 22:42:49 +00:00
|
|
|
|
var track = releaseMedia.Tracks.FirstOrDefault(x => x.TrackNumber == audioMetaData.TrackNumber);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (track == null)
|
|
|
|
|
{
|
|
|
|
|
// New Track - Not Found In Database
|
|
|
|
|
track = new data.Track
|
|
|
|
|
{
|
|
|
|
|
Status = Statuses.New,
|
|
|
|
|
FilePath = trackPath,
|
|
|
|
|
FileName = file.Name,
|
|
|
|
|
FileSize = (int)file.Length,
|
|
|
|
|
Hash = trackHash,
|
|
|
|
|
MusicBrainzId = audioMetaData.MusicBrainzId,
|
|
|
|
|
AmgId = audioMetaData.AmgId,
|
|
|
|
|
SpotifyId = audioMetaData.SpotifyId,
|
|
|
|
|
Title = audioMetaData.Title,
|
|
|
|
|
TrackNumber = audioMetaData.TrackNumber ?? totalNumberOfTracksFound,
|
|
|
|
|
Duration = audioMetaData.Time != null
|
|
|
|
|
? (int)audioMetaData.Time.Value.TotalMilliseconds
|
|
|
|
|
: 0,
|
|
|
|
|
ReleaseMediaId = releaseMedia.Id,
|
|
|
|
|
ISRC = audioMetaData.ISRC,
|
|
|
|
|
LastFMId = audioMetaData.LastFmId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (audioMetaData.TrackArtist != null)
|
|
|
|
|
{
|
|
|
|
|
if (audioMetaData.TrackArtists.Count() == 1)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var trackArtistData = await ArtistLookupEngine.GetByName(new AudioMetaData { Artist = audioMetaData.TrackArtist }, true).ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (trackArtistData.IsSuccess && release.ArtistId != trackArtistData.Data.Id)
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
trackArtistId = trackArtistData.Data.Id;
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-08-01 00:10:00 +00:00
|
|
|
|
if (release.ArtistId == trackArtistData.Data.Id)
|
|
|
|
|
{
|
|
|
|
|
trackArtistId = null;
|
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
else if (audioMetaData.TrackArtists.Any())
|
|
|
|
|
{
|
2019-07-31 22:42:49 +00:00
|
|
|
|
partTitles = string.Join(AudioMetaData.ArtistSplitCharacter.ToString(), audioMetaData.TrackArtists);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
partTitles = audioMetaData.TrackArtist;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var alt = track.Title.ToAlphanumericName();
|
2019-07-31 22:42:49 +00:00
|
|
|
|
track.AlternateNames = !alt.Equals(audioMetaData.Title, StringComparison.OrdinalIgnoreCase)
|
2019-07-07 03:16:33 +00:00
|
|
|
|
? track.AlternateNames.AddToDelimitedList(new[] { alt })
|
|
|
|
|
: null;
|
|
|
|
|
track.ArtistId = trackArtistId;
|
|
|
|
|
track.PartTitles = partTitles;
|
|
|
|
|
DbContext.Tracks.Add(track);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
_addedTrackIds.Add(track.Id);
|
|
|
|
|
modifiedRelease = true;
|
|
|
|
|
}
|
|
|
|
|
else if (string.IsNullOrEmpty(track.Hash) || trackHash != track.Hash)
|
|
|
|
|
{
|
|
|
|
|
if (audioMetaData.TrackArtist != null)
|
|
|
|
|
{
|
|
|
|
|
if (audioMetaData.TrackArtists.Count() == 1)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var trackArtistData = await ArtistLookupEngine.GetByName(new AudioMetaData { Artist = audioMetaData.TrackArtist }, true).ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (trackArtistData.IsSuccess && release.ArtistId != trackArtistData.Data.Id)
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
trackArtistId = trackArtistData.Data.Id;
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
else if (audioMetaData.TrackArtists.Any())
|
|
|
|
|
{
|
2019-07-31 22:42:49 +00:00
|
|
|
|
partTitles = string.Join(AudioMetaData.ArtistSplitCharacter.ToString(), audioMetaData.TrackArtists);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
partTitles = audioMetaData.TrackArtist;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
track.Title = audioMetaData.Title;
|
|
|
|
|
track.Duration = audioMetaData.Time != null
|
|
|
|
|
? (int)audioMetaData.Time.Value.TotalMilliseconds
|
|
|
|
|
: 0;
|
|
|
|
|
track.TrackNumber = audioMetaData.TrackNumber ?? totalNumberOfTracksFound;
|
|
|
|
|
track.ArtistId = trackArtistId;
|
|
|
|
|
track.PartTitles = partTitles;
|
|
|
|
|
track.Hash = trackHash;
|
|
|
|
|
track.FileName = file.Name;
|
|
|
|
|
track.FileSize = (int)file.Length;
|
|
|
|
|
track.FilePath = trackPath;
|
|
|
|
|
track.Status = Statuses.Ok;
|
|
|
|
|
track.LastUpdated = now;
|
|
|
|
|
var alt = track.Title.ToAlphanumericName();
|
|
|
|
|
if (!alt.Equals(track.Title, StringComparison.OrdinalIgnoreCase))
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
track.AlternateNames = track.AlternateNames.AddToDelimitedList(new[] { alt });
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
track.TrackNumber = audioMetaData.TrackNumber ?? -1;
|
|
|
|
|
track.LastUpdated = now;
|
|
|
|
|
modifiedRelease = true;
|
|
|
|
|
}
|
|
|
|
|
else if (track.Status != Statuses.Ok)
|
|
|
|
|
{
|
|
|
|
|
track.Status = Statuses.Ok;
|
|
|
|
|
track.LastUpdated = now;
|
|
|
|
|
modifiedRelease = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foundInFolderTracks.Add(track);
|
|
|
|
|
if (releaseMediaTracksFound.ContainsKey(releaseMedia.Id))
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
releaseMediaTracksFound[releaseMedia.Id]++;
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
else
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
releaseMediaTracksFound[releaseMedia.Id] = 1;
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-07-31 22:42:49 +00:00
|
|
|
|
Logger.LogWarning("Release Track File Has Invalid MetaData `{0}`", audioMetaData.ToString());
|
2019-07-07 03:16:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
else
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.LogWarning("Unable To Find Releaes Path [{0}] For Release `{1}`", releasePath, release.ToString());
|
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
var releaseMediaNumbersFound = new List<short?>();
|
|
|
|
|
foreach (var kp in releaseMediaTracksFound)
|
|
|
|
|
{
|
|
|
|
|
var releaseMedia = DbContext.ReleaseMedias.FirstOrDefault(x => x.Id == kp.Key);
|
|
|
|
|
if (releaseMedia != null)
|
|
|
|
|
{
|
|
|
|
|
if (!releaseMediaNumbersFound.Any(x => x == releaseMedia.MediaNumber))
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
releaseMediaNumbersFound.Add(releaseMedia.MediaNumber);
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
var releaseMediaFoundInFolderTrackNumbers = foundInFolderTracks
|
|
|
|
|
.Where(x => x.ReleaseMediaId == releaseMedia.Id).Select(x => x.TrackNumber).OrderBy(x => x)
|
|
|
|
|
.ToArray();
|
2019-07-31 22:42:49 +00:00
|
|
|
|
var areTracksForRelaseMediaSequential = releaseMediaFoundInFolderTrackNumbers.Zip(releaseMediaFoundInFolderTrackNumbers.Skip(1), (a, b) => a + 1 == b).All(x => x);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (!areTracksForRelaseMediaSequential)
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-08-11 19:11:29 +00:00
|
|
|
|
Logger.LogTrace($"ReleaseMedia [{ releaseMedia.Id }] Track Numbers Are Not Sequential [{ string.Join(",", releaseMediaFoundInFolderTrackNumbers) }]");
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
releaseMedia.TrackCount = kp.Value;
|
|
|
|
|
releaseMedia.LastUpdated = now;
|
|
|
|
|
releaseMedia.Status = areTracksForRelaseMediaSequential ? Statuses.Ok : Statuses.Incomplete;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
modifiedRelease = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 22:42:49 +00:00
|
|
|
|
var foundInFolderTrackNumbers = foundInFolderTracks.Select(x => x.TrackNumber).OrderBy(x => x).ToArray();
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (modifiedRelease || !foundInFolderTrackNumbers.Length.Equals(release.TrackCount) ||
|
|
|
|
|
releaseMediaNumbersFound.Count != (release.MediaCount ?? 0))
|
2019-07-07 03:16:33 +00:00
|
|
|
|
{
|
2019-07-31 22:42:49 +00:00
|
|
|
|
var areTracksForRelaseSequential = foundInFolderTrackNumbers.Zip(foundInFolderTrackNumbers.Skip(1), (a, b) => a + 1 == b).All(x => x);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var maxFoundInFolderTrackNumbers = foundInFolderTrackNumbers.Length > 0 ? foundInFolderTrackNumbers.Max() : (short)0;
|
2019-07-07 03:16:33 +00:00
|
|
|
|
release.Status = areTracksForRelaseSequential ? Statuses.Ok : Statuses.Incomplete;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
release.TrackCount = (short)foundInFolderTrackNumbers.Length;
|
|
|
|
|
release.MediaCount = (short)releaseMediaNumbersFound.Count;
|
2019-07-07 03:16:33 +00:00
|
|
|
|
if (release.TrackCount < maxFoundInFolderTrackNumbers)
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
release.TrackCount = maxFoundInFolderTrackNumbers;
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
2019-07-07 03:16:33 +00:00
|
|
|
|
release.LibraryStatus = release.TrackCount > 0 && release.TrackCount == totalNumberOfTracksFound
|
|
|
|
|
? LibraryStatus.Complete
|
|
|
|
|
: LibraryStatus.Incomplete;
|
|
|
|
|
release.LastUpdated = now;
|
|
|
|
|
release.Status = release.LibraryStatus == LibraryStatus.Complete
|
|
|
|
|
? Statuses.Complete
|
|
|
|
|
: Statuses.Incomplete;
|
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
CacheManager.ClearRegion(release.Artist.CacheRegion);
|
|
|
|
|
CacheManager.ClearRegion(release.CacheRegion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion Scan Folder and Add or Update Existing Tracks from Files
|
|
|
|
|
|
2019-11-21 14:44:05 +00:00
|
|
|
|
var releaseCoverImage = ImageHelper.FindImageTypeInDirectory(releaseDirectory, ImageType.Release).FirstOrDefault();
|
|
|
|
|
var doesReleaseHaveCoverImage = releaseCoverImage != null;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (doesReleaseHaveCoverImage)
|
2019-11-21 14:44:05 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
// See if image file is valid image if not delete it
|
|
|
|
|
if (ImageHelper.ConvertToJpegFormat(File.ReadAllBytes(releaseCoverImage.FullName)) == null)
|
2019-11-21 14:44:05 +00:00
|
|
|
|
{
|
|
|
|
|
releaseCoverImage.Delete();
|
|
|
|
|
doesReleaseHaveCoverImage = false;
|
|
|
|
|
Logger.LogWarning($"Deleted invalid cover image for Release `{ release }`");
|
2020-01-28 03:44:13 +00:00
|
|
|
|
}
|
2019-11-21 14:44:05 +00:00
|
|
|
|
}
|
2019-11-18 17:26:27 +00:00
|
|
|
|
if (!doesReleaseHaveCoverImage)
|
|
|
|
|
{
|
|
|
|
|
// Since no release image found see if first track has image in metadata if so then extract to cover file
|
|
|
|
|
firstTrack = (from t in DbContext.Tracks
|
|
|
|
|
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
where rm.ReleaseId == release.Id
|
|
|
|
|
orderby rm.MediaNumber, t.TrackNumber
|
|
|
|
|
select t).FirstOrDefault();
|
|
|
|
|
if (firstTrack?.FilePath != null)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var metaData = await AudioMetaDataHelper.GetInfo(new FileInfo(firstTrack.PathToTrack(Configuration)), doJustInfo).ConfigureAwait(false);
|
|
|
|
|
if (metaData.Images?.Any() == true)
|
2019-11-18 17:26:27 +00:00
|
|
|
|
{
|
2019-11-21 14:44:05 +00:00
|
|
|
|
var imageBytes = ImageHelper.ConvertToJpegFormat(metaData.Images.FirstOrDefault(x => x.Data != null)?.Data);
|
|
|
|
|
if (imageBytes != null)
|
|
|
|
|
{
|
|
|
|
|
var releaseImageFilename = Path.Combine(releasePath, ImageHelper.ReleaseCoverFilename);
|
|
|
|
|
File.WriteAllBytes(releaseImageFilename, imageBytes);
|
|
|
|
|
Logger.LogInformation($"Extracted cover image from track for Release `{ release}`");
|
|
|
|
|
}
|
2019-11-18 17:26:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 03:16:33 +00:00
|
|
|
|
sw.Stop();
|
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await UpdateReleaseCounts(release.Id, now).ConfigureAwait(false);
|
|
|
|
|
await UpdatePlaylistCountsForRelease(release.Id, now).ConfigureAwait(false);
|
|
|
|
|
await UpdateArtistCountsForRelease(release.Id, now).ConfigureAwait(false);
|
|
|
|
|
await UpdateReleaseRank(release.Id).ConfigureAwait(false);
|
2019-07-31 22:42:49 +00:00
|
|
|
|
|
2019-11-04 03:19:04 +00:00
|
|
|
|
CacheManager.ClearRegion(release.Artist.CacheRegion);
|
|
|
|
|
CacheManager.ClearRegion(release.CacheRegion);
|
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (release.Labels?.Any() == true)
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2019-07-07 03:16:33 +00:00
|
|
|
|
foreach (var label in release.Labels)
|
2019-07-31 22:42:49 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await UpdateLabelCounts(label.Id, now).ConfigureAwait(false);
|
2019-07-31 22:42:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Logger.LogInformation("Scanned Release `{0}` Folder [{1}], Modified Release [{2}], OperationTime [{3}]", release.ToString(), releasePath, modifiedRelease, sw.ElapsedMilliseconds);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
result = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
Logger.LogError(ex, $"ReleasePath [{releasePath}] {ex.Serialize()}");
|
2019-07-07 03:16:33 +00:00
|
|
|
|
resultErrors.Add(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
Data = result,
|
|
|
|
|
IsSuccess = result,
|
|
|
|
|
Errors = resultErrors,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 20:03:52 +00:00
|
|
|
|
public async Task<OperationResult<Image>> SetReleaseImageByUrlAsync(Library.Identity.User user, Guid id, string imageUrl) => await SaveImageBytes(user, id, await WebHelper.BytesForImageUrl(HttpClientFactory, imageUrl).ConfigureAwait(false)).ConfigureAwait(false);
|
2019-07-07 03:16:33 +00:00
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<bool>> UpdateReleaseAsync(Library.Identity.User user, Release model, string originalReleaseFolder = null)
|
2018-12-21 22:59:33 +00:00
|
|
|
|
{
|
2018-12-23 21:36:38 +00:00
|
|
|
|
var didChangeArtist = false;
|
2018-12-21 22:59:33 +00:00
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var errors = new List<Exception>();
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var release = await DbContext.Releases
|
2019-06-30 22:14:36 +00:00
|
|
|
|
.Include(x => x.Artist)
|
|
|
|
|
.Include(x => x.Genres)
|
|
|
|
|
.Include("Genres.Genre")
|
|
|
|
|
.Include(x => x.Labels)
|
|
|
|
|
.Include("Labels.Label")
|
2020-06-07 22:46:24 +00:00
|
|
|
|
.FirstOrDefaultAsync(x => x.RoadieId == model.Id)
|
|
|
|
|
.ConfigureAwait(false);
|
2018-12-21 22:59:33 +00:00
|
|
|
|
if (release == null)
|
2019-08-01 00:10:00 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
return new OperationResult<bool>(true, $"Release Not Found [{model.Id}]");
|
2019-08-01 00:10:00 +00:00
|
|
|
|
}
|
2019-08-03 15:23:46 +00:00
|
|
|
|
// If release is being renamed, see if release already exists for artist with new model supplied name
|
2019-11-17 20:02:19 +00:00
|
|
|
|
var releaseTitle = release.SortTitleValue;
|
|
|
|
|
var releaseModelTitle = model.SortTitleValue;
|
|
|
|
|
if (releaseTitle.ToAlphanumericName() != releaseModelTitle.ToAlphanumericName())
|
2019-08-03 15:23:46 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var existingRelease = await DbContext.Releases.FirstOrDefaultAsync(x => x.Id != release.Id &&
|
2019-11-17 20:02:19 +00:00
|
|
|
|
(x.Title == releaseModelTitle || x.SortTitle == releaseModelTitle) &&
|
2020-06-07 22:46:24 +00:00
|
|
|
|
x.ArtistId == release.ArtistId).ConfigureAwait(false);
|
2019-08-03 15:23:46 +00:00
|
|
|
|
if (existingRelease != null)
|
|
|
|
|
{
|
2019-11-17 20:02:19 +00:00
|
|
|
|
return new OperationResult<bool>($"Release already exists `{ existingRelease }` for Artist with title [{ releaseModelTitle }].");
|
2019-08-03 15:23:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-21 22:59:33 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
2019-07-07 03:16:33 +00:00
|
|
|
|
var artistFolder = release.Artist.ArtistFileFolder(Configuration);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
originalReleaseFolder ??= release.ReleaseFileFolder(artistFolder);
|
2018-12-23 21:36:38 +00:00
|
|
|
|
release.IsLocked = model.IsLocked;
|
|
|
|
|
release.IsVirtual = model.IsVirtual;
|
|
|
|
|
release.Status = SafeParser.ToEnum<Statuses>(model.Status);
|
|
|
|
|
release.Title = model.Title;
|
2019-11-09 17:43:39 +00:00
|
|
|
|
release.SortTitle = model.SortTitle;
|
2019-06-23 15:45:45 +00:00
|
|
|
|
var specialReleaseTitle = model.Title.ToAlphanumericName();
|
|
|
|
|
var alt = new List<string>(model.AlternateNamesList);
|
|
|
|
|
if (!model.AlternateNamesList.Contains(specialReleaseTitle, StringComparer.OrdinalIgnoreCase))
|
2019-08-01 00:10:00 +00:00
|
|
|
|
{
|
2019-06-23 15:45:45 +00:00
|
|
|
|
alt.Add(specialReleaseTitle);
|
2019-08-01 00:10:00 +00:00
|
|
|
|
}
|
2019-06-23 15:45:45 +00:00
|
|
|
|
release.AlternateNames = alt.ToDelimitedList();
|
2018-12-23 21:36:38 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2019-08-11 19:11:29 +00:00
|
|
|
|
data.Artist artist = null;
|
2018-12-23 21:36:38 +00:00
|
|
|
|
if (model?.Artist?.Artist?.Value != null)
|
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
artist = await DbContext.Artists.FirstOrDefaultAsync(x => x.RoadieId == SafeParser.ToGuid(model.Artist.Artist.Value)).ConfigureAwait(false);
|
2018-12-23 21:36:38 +00:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
// Save unaltered image to cover file
|
2019-07-28 04:05:24 +00:00
|
|
|
|
var coverFileName = Path.Combine(release.ReleaseFileFolder(release.Artist.ArtistFileFolder(Configuration)), "cover.jpg");
|
2019-07-25 15:43:11 +00:00
|
|
|
|
File.WriteAllBytes(coverFileName, ImageHelper.ConvertToJpegFormat(releaseImage));
|
2018-12-23 21:36:38 +00:00
|
|
|
|
}
|
2019-08-11 19:11:29 +00:00
|
|
|
|
var releaseFolder = release.ReleaseFileFolder(release.Artist.ArtistFileFolder(Configuration));
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (model.NewSecondaryImagesData?.Any() == true)
|
2019-06-13 16:52:13 +00:00
|
|
|
|
{
|
|
|
|
|
// Additional images to add to artist
|
|
|
|
|
var looper = 0;
|
|
|
|
|
foreach (var newSecondaryImageData in model.NewSecondaryImagesData)
|
|
|
|
|
{
|
|
|
|
|
var releaseSecondaryImage = ImageHelper.ImageDataFromUrl(newSecondaryImageData);
|
|
|
|
|
if (releaseSecondaryImage != null)
|
|
|
|
|
{
|
|
|
|
|
// Ensure is jpeg first
|
|
|
|
|
releaseSecondaryImage = ImageHelper.ConvertToJpegFormat(releaseSecondaryImage);
|
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var releaseImageFilename = Path.Combine(releaseFolder,
|
|
|
|
|
string.Format(ImageHelper.ReleaseSecondaryImageFilename, looper.ToString("00")));
|
2019-06-13 16:52:13 +00:00
|
|
|
|
while (File.Exists(releaseImageFilename))
|
|
|
|
|
{
|
|
|
|
|
looper++;
|
2019-06-30 22:14:36 +00:00
|
|
|
|
releaseImageFilename = Path.Combine(releaseFolder,
|
|
|
|
|
string.Format(ImageHelper.ReleaseSecondaryImageFilename, looper.ToString("00")));
|
2019-06-13 16:52:13 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-06-13 16:52:13 +00:00
|
|
|
|
File.WriteAllBytes(releaseImageFilename, releaseSecondaryImage);
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-06-13 16:52:13 +00:00
|
|
|
|
looper++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (model.Genres?.Any() == true)
|
2018-12-23 21:36:38 +00:00
|
|
|
|
{
|
|
|
|
|
// Remove existing Genres not in model list
|
|
|
|
|
foreach (var genre in release.Genres.ToList())
|
|
|
|
|
{
|
2019-11-24 21:58:38 +00:00
|
|
|
|
var doesExistInModel = model.Genres.Any(x => SafeParser.ToGuid(x.Genre.Value) == genre.Genre.RoadieId);
|
|
|
|
|
if (!doesExistInModel)
|
|
|
|
|
{
|
|
|
|
|
release.Genres.Remove(genre);
|
|
|
|
|
}
|
2018-12-23 21:36:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add new Genres in model not in data
|
|
|
|
|
foreach (var genre in model.Genres)
|
|
|
|
|
{
|
2019-11-24 21:58:38 +00:00
|
|
|
|
var genreId = SafeParser.ToGuid(genre.Genre.Value);
|
2018-12-23 21:36:38 +00:00
|
|
|
|
var doesExistInData = release.Genres.Any(x => x.Genre.RoadieId == genreId);
|
|
|
|
|
if (!doesExistInData)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var g = DbContext.Genres.FirstOrDefault(x => x.RoadieId == genreId);
|
2018-12-23 21:36:38 +00:00
|
|
|
|
if (g != null)
|
2020-01-28 03:44:13 +00:00
|
|
|
|
{
|
2018-12-23 21:36:38 +00:00
|
|
|
|
release.Genres.Add(new data.ReleaseGenre
|
|
|
|
|
{
|
|
|
|
|
ReleaseId = release.Id,
|
|
|
|
|
GenreId = g.Id,
|
|
|
|
|
Genre = g
|
|
|
|
|
});
|
2020-01-28 03:44:13 +00:00
|
|
|
|
}
|
2018-12-23 21:36:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
else if (model.Genres?.Any() != true)
|
2018-12-23 21:36:38 +00:00
|
|
|
|
{
|
|
|
|
|
release.Genres.Clear();
|
2018-12-21 22:59:33 +00:00
|
|
|
|
}
|
2018-12-23 21:36:38 +00:00
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (model.Labels?.Any() != true)
|
2019-11-24 21:58:38 +00:00
|
|
|
|
{
|
|
|
|
|
// Delete all existing labels for release
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var releaseLabelsToDelete = await (from l in DbContext.ReleaseLabels
|
|
|
|
|
where l.ReleaseId == release.Id
|
|
|
|
|
select l)
|
|
|
|
|
.ToArrayAsync()
|
|
|
|
|
.ConfigureAwait(false);
|
2019-11-24 21:58:38 +00:00
|
|
|
|
DbContext.ReleaseLabels.RemoveRange(releaseLabelsToDelete);
|
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
else if (model.Labels?.Any() == true)
|
2019-11-24 21:58:38 +00:00
|
|
|
|
{
|
|
|
|
|
var releaseLabelIds = model.Labels.Select(x => x.Id).ToArray();
|
|
|
|
|
// Delete any labels not in given model (removed by edit operation)
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var releaseLabelsToDelete = await (from l in DbContext.ReleaseLabels
|
|
|
|
|
where l.ReleaseId == release.Id
|
|
|
|
|
where !releaseLabelIds.Contains(l.RoadieId)
|
|
|
|
|
select l)
|
|
|
|
|
.ToArrayAsync()
|
|
|
|
|
.ConfigureAwait(false);
|
2019-11-24 21:58:38 +00:00
|
|
|
|
DbContext.ReleaseLabels.RemoveRange(releaseLabelsToDelete);
|
|
|
|
|
// Update any existing
|
2020-01-28 03:44:13 +00:00
|
|
|
|
foreach (var label in model.Labels)
|
2019-11-24 21:58:38 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var trackLabel = await DbContext.ReleaseLabels.FirstOrDefaultAsync(x => x.RoadieId == label.Id).ConfigureAwait(false);
|
2019-11-24 21:58:38 +00:00
|
|
|
|
if (trackLabel == null)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
// Add new
|
2019-11-24 21:58:38 +00:00
|
|
|
|
trackLabel = new data.ReleaseLabel
|
|
|
|
|
{
|
|
|
|
|
ReleaseId = release.Id,
|
|
|
|
|
CreatedDate = now
|
|
|
|
|
};
|
|
|
|
|
DbContext.ReleaseLabels.Add(trackLabel);
|
|
|
|
|
}
|
|
|
|
|
trackLabel.CatalogNumber = label.CatalogNumber;
|
|
|
|
|
trackLabel.BeginDate = label.BeginDate;
|
|
|
|
|
trackLabel.EndDate = label.EndDate;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var releaseLabel = await GetLabel(SafeParser.ToGuid(label.Label.Label.Value).Value).ConfigureAwait(false);
|
2019-11-24 21:58:38 +00:00
|
|
|
|
trackLabel.LabelId = releaseLabel.Id;
|
|
|
|
|
trackLabel.IsLocked = label.IsLocked;
|
|
|
|
|
trackLabel.Status = SafeParser.ToEnum<Statuses>(label.Status);
|
|
|
|
|
trackLabel.LastUpdated = now;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
if (model.Credits?.Any() != true)
|
2019-11-24 21:58:38 +00:00
|
|
|
|
{
|
|
|
|
|
// Delete all existing credits for release
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var releaseCreditsToDelete = await (from c in DbContext.Credits
|
|
|
|
|
where c.ReleaseId == release.Id
|
|
|
|
|
select c)
|
|
|
|
|
.ToArrayAsync()
|
|
|
|
|
.ConfigureAwait(false);
|
2019-11-24 21:58:38 +00:00
|
|
|
|
DbContext.Credits.RemoveRange(releaseCreditsToDelete);
|
|
|
|
|
}
|
2020-01-28 03:44:13 +00:00
|
|
|
|
else if (model.Credits?.Any() == true)
|
2018-12-23 21:36:38 +00:00
|
|
|
|
{
|
2019-11-24 21:58:38 +00:00
|
|
|
|
var releaseCreditIds = model.Credits.Select(x => x.Id).ToArray();
|
|
|
|
|
// Delete any credits not given in model (removed by edit operation)
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var releaseCreditsToDelete = await (from c in DbContext.Credits
|
|
|
|
|
where c.TrackId == release.Id
|
|
|
|
|
where !releaseCreditIds.Contains(c.RoadieId)
|
|
|
|
|
select c)
|
|
|
|
|
.ToArrayAsync()
|
|
|
|
|
.ConfigureAwait(false);
|
2019-11-24 21:58:38 +00:00
|
|
|
|
DbContext.Credits.RemoveRange(releaseCreditsToDelete);
|
|
|
|
|
// Update any existing
|
|
|
|
|
foreach (var credit in model.Credits)
|
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var trackCredit = await DbContext.Credits.FirstOrDefaultAsync(x => x.RoadieId == credit.Id).ConfigureAwait(false);
|
2019-11-24 21:58:38 +00:00
|
|
|
|
if (trackCredit == null)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
// Add new
|
2019-11-24 21:58:38 +00:00
|
|
|
|
trackCredit = new data.Credit
|
|
|
|
|
{
|
|
|
|
|
ReleaseId = release.Id,
|
|
|
|
|
CreatedDate = now
|
|
|
|
|
};
|
|
|
|
|
DbContext.Credits.Add(trackCredit);
|
|
|
|
|
}
|
|
|
|
|
data.Artist artistForCredit = null;
|
|
|
|
|
if (credit.Artist != null)
|
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
artistForCredit = await GetArtist(credit.Artist.Id).ConfigureAwait(false);
|
2019-11-24 21:58:38 +00:00
|
|
|
|
}
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var creditCategory = await DbContext.CreditCategory.FirstOrDefaultAsync(x => x.RoadieId.ToString() == credit.Category.Value).ConfigureAwait(false);
|
2019-11-24 21:58:38 +00:00
|
|
|
|
trackCredit.CreditCategoryId = creditCategory.Id;
|
|
|
|
|
trackCredit.ArtistId = artistForCredit == null ? null : (int?)artistForCredit.Id;
|
|
|
|
|
trackCredit.IsLocked = credit.IsLocked;
|
|
|
|
|
trackCredit.Status = SafeParser.ToEnum<Statuses>(credit.Status);
|
|
|
|
|
trackCredit.CreditToName = artistForCredit == null ? credit.CreditName : null;
|
|
|
|
|
trackCredit.Description = credit.Description;
|
|
|
|
|
trackCredit.URLs = credit.URLs;
|
|
|
|
|
trackCredit.Tags = credit.Tags;
|
|
|
|
|
trackCredit.LastUpdated = now;
|
|
|
|
|
}
|
2018-12-23 21:36:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 22:59:33 +00:00
|
|
|
|
release.LastUpdated = now;
|
2020-01-28 03:44:13 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
await CheckAndChangeReleaseTitle(release, originalReleaseFolder).ConfigureAwait(false);
|
2019-08-22 20:33:07 +00:00
|
|
|
|
// Update release ID3 Tags
|
2019-08-11 19:11:29 +00:00
|
|
|
|
foreach (var mp3 in Directory.GetFiles(releaseFolder, "*.mp3", SearchOption.AllDirectories))
|
|
|
|
|
{
|
|
|
|
|
var trackFileInfo = new FileInfo(mp3);
|
2020-01-28 03:44:13 +00:00
|
|
|
|
var audioMetaData = await AudioMetaDataHelper.GetInfo(trackFileInfo).ConfigureAwait(false);
|
2019-08-11 19:11:29 +00:00
|
|
|
|
if (audioMetaData != null)
|
|
|
|
|
{
|
|
|
|
|
audioMetaData.Year = release.ReleaseYear;
|
|
|
|
|
audioMetaData.Release = release.Title;
|
2019-08-22 20:33:07 +00:00
|
|
|
|
if (artist != null)
|
2019-08-11 19:11:29 +00:00
|
|
|
|
{
|
|
|
|
|
audioMetaData.Artist = artist.Name;
|
|
|
|
|
}
|
|
|
|
|
AudioMetaDataHelper.WriteTags(audioMetaData, trackFileInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
CacheManager.ClearRegion(release.CacheRegion);
|
2019-11-04 03:19:04 +00:00
|
|
|
|
Logger.LogInformation($"UpdateRelease `{release}` By User `{user}`: Edited Artist [{didChangeArtist}]");
|
2018-12-21 22:59:33 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogError(ex);
|
2018-12-21 22:59:33 +00:00
|
|
|
|
errors.Add(ex);
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2018-12-21 22:59:33 +00:00
|
|
|
|
sw.Stop();
|
|
|
|
|
|
2018-12-23 21:36:38 +00:00
|
|
|
|
return new OperationResult<bool>
|
2018-12-21 22:59:33 +00:00
|
|
|
|
{
|
2020-01-28 03:44:13 +00:00
|
|
|
|
IsSuccess = errors.Count == 0,
|
|
|
|
|
Data = errors.Count == 0,
|
2018-12-21 22:59:33 +00:00
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Errors = errors
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<Image>> UploadReleaseImageAsync(Library.Identity.User user, Guid id, IFormFile file)
|
2018-12-23 21:36:38 +00:00
|
|
|
|
{
|
|
|
|
|
var bytes = new byte[0];
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
file.CopyTo(ms);
|
|
|
|
|
bytes = ms.ToArray();
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2020-01-28 03:44:13 +00:00
|
|
|
|
return await SaveImageBytes(user, id, bytes).ConfigureAwait(false);
|
2018-12-23 21:36:38 +00:00
|
|
|
|
}
|
2018-11-12 00:28:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|