Exemple #1
0
        protected MediaFileInfo ConvertMediaFile(MediaFile file, MediaFolderNode folder)
        {
            var mediaFile = new MediaFileInfo(file, _storageProvider, _urlGenerator, folder?.Path)
            {
                ThumbSize = _mediaSettings.ProductThumbPictureSize
            };

            return(mediaFile);
        }
        public Task <TreeNode <MediaFolderNode> > GetRootNodeAsync()
        {
            var root = _cache.GetAsync(FolderTreeKey, async(o) =>
            {
                o.ExpiresIn(FolderTreeCacheDuration);

                var query = from x in _db.MediaFolders
                            orderby x.ParentId, x.Name
                select x;

                var unsortedNodes = (await query.ToListAsync()).Select(x =>
                {
                    var item = new MediaFolderNode
                    {
                        Id              = x.Id,
                        Name            = x.Name,
                        ParentId        = x.ParentId,
                        CanDetectTracks = x.CanDetectTracks,
                        FilesCount      = x.FilesCount,
                        Slug            = x.Slug
                    };

                    if (x is MediaAlbum album)
                    {
                        item.IsAlbum     = true;
                        item.AlbumName   = album.Name;
                        item.Path        = album.Name;
                        item.ResKey      = album.ResKey;
                        item.IncludePath = album.IncludePath;
                        item.Order       = album.Order ?? 0;

                        var albumInfo = _albumRegistry.GetAlbumByNameAsync(album.Name).Await();
                        if (albumInfo != null)
                        {
                            var displayHint   = albumInfo.DisplayHint;
                            item.Color        = displayHint.Color;
                            item.OverlayColor = displayHint.OverlayColor;
                            item.OverlayIcon  = displayHint.OverlayIcon;
                        }
                    }

                    return(item);
                });

                var nodeMap  = unsortedNodes.ToMultimap(x => x.ParentId ?? 0, x => x);
                var rootNode = new TreeNode <MediaFolderNode>(new MediaFolderNode {
                    Name = "Root", Id = 0
                });

                AddChildTreeNodes(rootNode, 0, nodeMap);

                return(rootNode);
            });

            return(root);
        protected virtual async Task TrackManyCoreAsync(IEnumerable <MediaTrack> tracks, string albumName)
        {
            Guard.NotNull(tracks, nameof(tracks));

            if (!tracks.Any())
            {
                return;
            }

            using (var scope = new DbContextScope(_db, minHookImportance: HookImportance.Important, autoDetectChanges: false))
            {
                // Get the album (necessary later to set FolderId)...
                MediaFolderNode albumNode = albumName.HasValue()
                    ? _folderService.GetNodeByPath(albumName)?.Value
                    : null;

                // Get distinct ids of all detected files...
                var mediaFileIds = tracks.Select(x => x.MediaFileId).Distinct().ToArray();

                // fetch these files from database...
                var query = _db.MediaFiles
                            .Include(x => x.Tracks)
                            .Where(x => mediaFileIds.Contains(x.Id));

                var isInstallation = !EngineContext.Current.Application.IsInstalled;
                if (isInstallation)
                {
                    query = query.Where(x => x.Version == 1);
                }

                var files = await query.ToDictionaryAsync(x => x.Id);

                // for each media file relation to an entity...
                foreach (var track in tracks)
                {
                    // fetch the file from local dictionary by its id...
                    if (files.TryGetValue(track.MediaFileId, out var file))
                    {
                        if (isInstallation)
                        {
                            // set album id as folder id (during installation there are no sub-folders)
                            file.FolderId = albumNode?.Id;

                            // remember that we processed tracks for this file already
                            file.Version = 2;
                        }

                        if (track.Album.IsEmpty())
                        {
                            if (albumNode != null)
                            {
                                // Overwrite track album if scope album was passed.
                                track.Album = albumNode.Name;
                            }
                            else if (file.FolderId.HasValue)
                            {
                                // Determine album from file
                                albumNode   = _folderService.FindAlbum(file)?.Value;
                                track.Album = albumNode?.Name;
                            }
                        }

                        if (track.Album.IsEmpty())
                        {
                            continue; // cannot track without album
                        }
                        if ((albumNode ?? _folderService.FindAlbum(file)?.Value)?.CanDetectTracks == false)
                        {
                            continue; // should not track in albums that do not support track detection
                        }
                        // add or remove the track from file
                        if (track.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;
                        }
                    }
                }

                // Save whole batch to database
                int num = await _db.SaveChangesAsync();

                if (num > 0)
                {
                    // Breathe
                    _db.DetachEntities <MediaFile>(deep: true);
                }
            }
        }
Exemple #4
0
 public DuplicateMediaFolderException(string message, MediaFolderNode dupeFolder) : base(message)
 {
     Folder = dupeFolder;
 }
Exemple #5
0
 public DuplicateMediaFolderException DuplicateFolder(string fullPath, MediaFolderNode dupeFolder)
 {
     return(new DuplicateMediaFolderException(T("Admin.Media.Exception.DuplicateFolder", fullPath), dupeFolder));
 }