Ejemplo n.º 1
0
        private bool iterateFiles(System.IO.DirectoryInfo location, object state)
        {
            if (CancellationToken.IsCancellationRequested)
            {
                return(false);
            }

            Tuple <ScanLocation, ObservableCollection <ScanLocation> > locationArgs = state as Tuple <ScanLocation, ObservableCollection <ScanLocation> >;

            ScanLocation scanLocation = locationArgs.Item1;
            ObservableCollection <ScanLocation> excludeLocations = locationArgs.Item2;

            FileInfo[] files = null;

            try
            {
                files = location.GetFiles("*.*");
            }
            catch (UnauthorizedAccessException e)
            {
                Logger.Log.Warn(e.Message);
            }
            catch (DirectoryNotFoundException e)
            {
                Logger.Log.Warn(e.Message);
            }

            List <BaseMetadata> staleItems = new List <BaseMetadata>();

            using (MetadataDbCommands metadataCommands = new MetadataDbCommands())
            {
                staleItems = metadataCommands.getAllMetadataInDirectory(location.FullName);
            }

            foreach (FileInfo info in files)
            {
                if (CancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                if (MediaViewer.Model.Utils.MediaFormatConvert.isMediaFile(info.Name))
                {
                    staleItems.RemoveAll(x => x.Name.Equals(info.Name));
                }

                String addItem = null;

                switch (scanLocation.MediaType)
                {
                case Search.MediaType.All:
                {
                    if (MediaViewer.Model.Utils.MediaFormatConvert.isMediaFile(info.Name))
                    {
                        addItem = info.FullName;
                    }
                    break;
                }

                case Search.MediaType.Images:
                {
                    if (MediaFormatConvert.isImageFile(info.Name))
                    {
                        addItem = info.FullName;
                    }
                    break;
                }

                case Search.MediaType.Video:
                {
                    if (MediaFormatConvert.isVideoFile(info.Name))
                    {
                        addItem = info.FullName;
                    }
                    break;
                }

                case Search.MediaType.Audio:
                {
                    if (MediaFormatConvert.isAudioFile(info.Name))
                    {
                        addItem = info.FullName;
                    }
                    break;
                }
                }

                if (addItem != null)
                {
                    String path = FileUtils.getPathWithoutFileName(addItem);

                    bool excluded = false;

                    foreach (ScanLocation excludeLocation in excludeLocations)
                    {
                        if (excludeLocation.IsRecursive)
                        {
                            if (path.StartsWith(excludeLocation.Location))
                            {
                                if (excludeLocation.MediaType == Search.MediaType.All ||
                                    (excludeLocation.MediaType == Search.MediaType.Images && MediaFormatConvert.isImageFile(addItem)) ||
                                    (excludeLocation.MediaType == Search.MediaType.Video && MediaFormatConvert.isVideoFile(addItem)))
                                {
                                    excluded = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (path.Equals(excludeLocation.Location))
                            {
                                if (excludeLocation.MediaType == Search.MediaType.All ||
                                    (excludeLocation.MediaType == Search.MediaType.Images && MediaFormatConvert.isImageFile(addItem)) ||
                                    (excludeLocation.MediaType == Search.MediaType.Video && MediaFormatConvert.isVideoFile(addItem)))
                                {
                                    excluded = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (!excluded)
                    {
                        importItem(info);
                    }
                }
            }

            if (staleItems.Count > 0)
            {
                using (MetadataDbCommands metadataCommands = new MetadataDbCommands())
                {
                    foreach (BaseMetadata staleItem in staleItems)
                    {
                        metadataCommands.delete(staleItem);
                    }
                }

                Logger.Log.Info("Removed " + staleItems.Count + " stale media items from " + location.FullName);
                InfoMessages.Add("Removed " + staleItems.Count + " stale media items from " + location.FullName);
            }

            return(true);
        }
        private bool iterateFiles(System.IO.DirectoryInfo location, object state)
        {
            if (CancellationToken.IsCancellationRequested)
            {
                return (false);
            }

            Tuple<ScanLocation, ObservableCollection<ScanLocation>> locationArgs = state as Tuple<ScanLocation, ObservableCollection<ScanLocation>>;

            ScanLocation scanLocation = locationArgs.Item1;
            ObservableCollection<ScanLocation> excludeLocations = locationArgs.Item2;
                    
            FileInfo[] files = null;
                    
            try
            {
                files = location.GetFiles("*.*");
            }       
            catch (UnauthorizedAccessException e)
            {           
                Logger.Log.Warn(e.Message);
            }
            catch (DirectoryNotFoundException e)
            {
                Logger.Log.Warn(e.Message);
            }

            List<BaseMetadata> staleItems = new List<BaseMetadata>();

            using(MetadataDbCommands metadataCommands = new MetadataDbCommands()) 
            {
                staleItems = metadataCommands.getAllMetadataInDirectory(location.FullName);            
            }

            foreach (FileInfo info in files)
            {
                if (CancellationToken.IsCancellationRequested) return false;

                if (MediaViewer.Model.Utils.MediaFormatConvert.isMediaFile(info.Name))
                {
                    staleItems.RemoveAll(x => x.Name.Equals(info.Name));
                }

                String addItem = null;

                switch (scanLocation.MediaType)
                {
                    case Search.MediaType.All:
                        {
                            if (MediaViewer.Model.Utils.MediaFormatConvert.isMediaFile(info.Name))
                            {
                                addItem = info.FullName;
                            }
                            break;
                        }
                    case Search.MediaType.Images:
                        {
                            if (MediaFormatConvert.isImageFile(info.Name))
                            {
                                addItem = info.FullName;
                            }
                            break;
                        }
                    case Search.MediaType.Video:
                        {
                            if (MediaFormatConvert.isVideoFile(info.Name))
                            {
                                addItem = info.FullName;
                            }
                            break;
                        }
                    case Search.MediaType.Audio:
                        {
                            if (MediaFormatConvert.isAudioFile(info.Name))
                            {
                                addItem = info.FullName;
                            }
                            break;
                        }
                }

                if (addItem != null)
                {
                    String path = FileUtils.getPathWithoutFileName(addItem);

                    bool excluded = false;

                    foreach (ScanLocation excludeLocation in excludeLocations)
                    {
                        if (excludeLocation.IsRecursive)
                        {
                            if (path.StartsWith(excludeLocation.Location))
                            {
                                if (excludeLocation.MediaType == Search.MediaType.All ||
                                    (excludeLocation.MediaType == Search.MediaType.Images && MediaFormatConvert.isImageFile(addItem)) ||
                                     (excludeLocation.MediaType == Search.MediaType.Video && MediaFormatConvert.isVideoFile(addItem)))
                                {
                                    excluded = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (path.Equals(excludeLocation.Location))
                            {
                                if (excludeLocation.MediaType == Search.MediaType.All ||
                                    (excludeLocation.MediaType == Search.MediaType.Images && MediaFormatConvert.isImageFile(addItem)) ||
                                     (excludeLocation.MediaType == Search.MediaType.Video && MediaFormatConvert.isVideoFile(addItem)))
                                {
                                    excluded = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (!excluded)
                    {
                        importItem(info);
                    }
                }
            }

            if(staleItems.Count > 0) {

                using (MetadataDbCommands metadataCommands = new MetadataDbCommands())
                {
                    foreach (BaseMetadata staleItem in staleItems)
                    {
                        metadataCommands.delete(staleItem);
                    }
                }

                Logger.Log.Info("Removed " + staleItems.Count + " stale media items from " + location.FullName);
                InfoMessages.Add("Removed " + staleItems.Count + " stale media items from " + location.FullName);
            }

            return (true);
        }