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

            return(mediaFile);
        }
Пример #2
0
        public TreeNode <MediaFolderNode> GetRootNode()
        {
            var cacheKey = FolderTreeKey;

            var root = _cache.Get(cacheKey, () =>
            {
                var query = from x in _folderRepo.TableUntracked
                            orderby x.ParentId, x.Name
                select x;

                var unsortedNodes = query.ToList().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.GetAlbumByName(album.Name);
                        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);
            }, FolderTreeCacheDuration);

            return(root);
Пример #3
0
        protected virtual void TrackManyCore(IEnumerable <MediaTrack> tracks, string albumName, bool isMigration)
        {
            Guard.NotNull(tracks, nameof(tracks));

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

            var ctx = _dbContext;

            using (var scope = new DbContextScope(ctx,
                                                  validateOnSave: false,
                                                  hooksEnabled: false,
                                                  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 = ctx.Set <MediaFile>().Include(x => x.Tracks).Where(x => mediaFileIds.Contains(x.Id));
                if (isMigration)
                {
                    query = query.Where(x => x.Version == 1);
                }
                var files = query.ToDictionary(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 (isMigration)
                        {
                            // set album id as folder id (during initial migration 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);
                                _dbContext.ChangeState(dbTrack, System.Data.Entity.EntityState.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 = ctx.SaveChanges();

                if (num > 0)
                {
                    // Breathe
                    ctx.DetachEntities <MediaFile>(deep: true);
                }
            }
        }
Пример #4
0
 public DuplicateMediaFolderException(string message, MediaFolderNode dupeFolder) : base(message)
 {
     Folder = dupeFolder;
 }
Пример #5
0
 public DuplicateMediaFolderException DuplicateFolder(string fullPath, MediaFolderNode dupeFolder)
 {
     return(new DuplicateMediaFolderException(T("Admin.Media.Exception.DuplicateFolder", fullPath), dupeFolder));
 }
        public TreeNode <MediaFolderNode> GetRootNode()
        {
            var cacheKey = FolderTreeKey;

            var root = _cache.Get(cacheKey, () =>
            {
                var query = from x in _folderRepository.TableUntracked
                            orderby x.ParentId, x.Name
                select x;

                var unsortedNodes = query.ToList().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.GetAlbumByName(album.Name);
                        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);
            }, FolderTreeCacheDuration);

            return(root);

            void AddChildTreeNodes(TreeNode <MediaFolderNode> parentNode, int parentId, Multimap <int, MediaFolderNode> nodeMap)
            {
                var parent = parentNode?.Value;

                if (parent == null)
                {
                    return;
                }

                var nodes = Enumerable.Empty <MediaFolderNode>();

                if (nodeMap.ContainsKey(parentId))
                {
                    nodes = parentId == 0
                        ? nodeMap[parentId].OrderBy(x => x.Order)
                        : nodeMap[parentId].OrderBy(x => x.Name);
                }

                foreach (var node in nodes)
                {
                    var newNode = new TreeNode <MediaFolderNode>(node);

                    // Inherit some props from parent node
                    if (!node.IsAlbum)
                    {
                        node.AlbumName       = parent.AlbumName;
                        node.CanDetectTracks = parent.CanDetectTracks;
                        node.IncludePath     = parent.IncludePath;
                        node.Path            = (parent.Path + "/" + (node.Slug.NullEmpty() ?? node.Name)).Trim('/').ToLower();
                    }

                    // We gonna query nodes by path also, therefore we need 2 keys per node (FolderId and computed path)
                    newNode.Id = new object[] { node.Id, node.Path };

                    parentNode.Append(newNode);

                    AddChildTreeNodes(newNode, node.Id, nodeMap);
                }
            }
        }