Exemple #1
0
        public static Task TrackAsync(this IMediaTracker tracker, MediaTrack track)
        {
            if (track == null)
            {
                return(Task.CompletedTask);
            }

            return(tracker.TrackManyAsync(new[] { track }));
        }
        protected virtual async Task TrackSingleAsync(BaseEntity entity, int mediaFileId, string propertyName, MediaTrackOperation operation)
        {
            Guard.NotNull(entity, nameof(entity));

            if (mediaFileId < 1 || entity.IsTransientRecord())
            {
                return;
            }

            var file = await _db.MediaFiles.FindByIdAsync(mediaFileId);

            if (file != null)
            {
                var album = _folderService.FindAlbum(file)?.Value;
                if (album == null)
                {
                    throw new InvalidOperationException(T("Admin.Media.Exception.TrackUnassignedFile"));
                }
                else if (!album.CanDetectTracks)
                {
                    // No support for tracking on album level, so get outta here.
                    return;
                }

                var track = new MediaTrack
                {
                    EntityId    = entity.Id,
                    EntityName  = entity.GetEntityName(),
                    MediaFileId = mediaFileId,
                    Property    = propertyName,
                    Album       = album.Name
                };

                if (operation == MediaTrackOperation.Track)
                {
                    file.Tracks.Add(track);
                }
                else
                {
                    var dbTrack = file.Tracks.FirstOrDefault(x => x == track);
                    if (dbTrack != null)
                    {
                        file.Tracks.Remove(track);
                        _db.TryChangeState(dbTrack, EfState.Deleted);
                    }
                }

                if (file.Tracks.Count > 0)
                {
                    // A file with tracks can NEVER be transient
                    file.IsTransient = false;
                }
                else if (_makeFilesTransientWhenOrphaned)
                {
                    // But an untracked file can OPTIONALLY be transient
                    file.IsTransient = true;
                }

                await _db.SaveChangesAsync();
            }
        }