Example #1
0
        void infoGatherLoop()
        {
            while (token.IsCancellationRequested == false)
            {
                Location location = locationQueue.Take(token);

                using (MetadataDbCommands mediaCommand = new MetadataDbCommands())
                {                                   
                    location.NrImported = mediaCommand.getNrMetadataInDirectory(location.FullName);
                }
            }
          
        }
Example #2
0
        public bool export_WLock(CancellationToken token)
        {           
            if (ItemState == MediaItemState.DELETED || Metadata == null || Metadata.IsImported == false)
            {
                return (false);
            }

            using (MetadataDbCommands metadataCommands = new MetadataDbCommands())
            {
                metadataCommands.delete(Metadata);
                   
            }               
           
            QueueOnPropertyChangedEvent("Metadata");
            return (true);
        }
Example #3
0
        public bool import_WLock(CancellationToken token)
        {          
            if (ItemState == MediaItemState.DELETED || Metadata == null || Metadata.IsImported == true)
            {
                return (false);
            }

            using (MetadataDbCommands metadataCommands = new MetadataDbCommands())
            {                   
                Metadata = metadataCommands.create(Metadata);
                   
            }                
            
            return (true);
        }
Example #4
0
        /// <summary>
        /// Returns true if deleted file was imported otherwise false
        /// </summary>
        /// <returns></returns>
        public bool delete_WLock()
        {           
            bool isImported = false;

            if (ItemState == MediaItemState.DELETED)
            {
                return (isImported);
            }

            FileUtils fileUtils = new FileUtils();

            if (ItemState != MediaItemState.FILE_NOT_FOUND)
            {
                fileUtils.deleteFile(Location);
            }

            if (Metadata != null && Metadata.IsImported)
            {
                using (MetadataDbCommands metadataCommands = new MetadataDbCommands())
                {
                    metadataCommands.delete(Metadata);
                }

                Metadata = null;

                isImported = true;
            }

            ItemState = MediaItemState.DELETED;
            //Factory.deleteFromDictionary(location);
            Logger.Log.Info("Deleted: " + Location);

            return (isImported);            
           
        }
Example #5
0
        // update metadata from disk
        void reloadFromDisk_URLock(CancellationToken token)
        {
            EnterWriteLock();
            ItemState = MediaItemState.LOADING;
            ExitWriteLock(false);

            MetadataFactory.ReadOptions readOptions = MetadataFactory.ReadOptions.READ_FROM_DISK;

            Thumbnail thumb = null;    
            int id = 0;

            if (Metadata != null)
            {                
                id = Metadata.Id;

                if (Metadata.Thumbnail == null)
                {
                    // generate thumbnail
                    readOptions |= MetadataFactory.ReadOptions.GENERATE_THUMBNAIL;

                } else {

                    // save current thumb
                    thumb = Metadata.Thumbnail;                  
                }                
            }

            readMetadata_URLock(readOptions, token);

            if (Metadata != null && thumb != null)
            {
                // restore thumbnails
                Metadata.Thumbnail = thumb;
            }

            if (Metadata != null && id != 0)
            {
                // update changes to database
                using (MetadataDbCommands metadataCommands = new MetadataDbCommands())
                {
                    Metadata.Id = id;                    
                    Metadata = metadataCommands.update(Metadata);
                                     
                }
               
            }

        }
        private async Task clearMedia()
        {
            MessageBoxResult result = MessageBox.Show("Are you sure you want to clear all Media from the database?", "Clear All Media", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
            if (result == MessageBoxResult.No) return;

            List<BaseMetadata> media;

            using (MetadataDbCommands mediaCommands = new MetadataDbCommands())
            {
                media = mediaCommands.getAllMetadata();
            }

            List<MediaFileItem> items = new List<MediaFileItem>();

            foreach (BaseMetadata m in media)
            {
                items.Add(MediaFileItem.Factory.create(m.Location));
            }

            ExportProgressViewModel export = new ExportProgressViewModel(MediaFileWatcher.Instance.MediaFileState);

            CancellableOperationProgressView exportView = new CancellableOperationProgressView();
            exportView.DataContext = export;
            exportView.ShowDialog();
            await export.exportAsync(items);

            NrMedia = 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);
        }
Example #8
0
        public static void write(BaseMetadata metadata, WriteOptions options, CancellableOperationProgressBase progress = null)
        {
           
            if (options.HasFlag(WriteOptions.AUTO) || options.HasFlag(WriteOptions.WRITE_TO_DISK))
            {
                MetadataFileFactory.write(metadata, progress);                
            }

            if (metadata.IsImported && (options.HasFlag(WriteOptions.AUTO) || options.HasFlag(WriteOptions.WRITE_TO_DATABASE)))
            {
                using (MetadataDbCommands metadataCommands = new MetadataDbCommands())
                {
                    metadata = metadataCommands.update(metadata);
                }
            }

            metadata.IsModified = false;
            
        }
Example #9
0
        private static BaseMetadata readMetadataFromDatabase(string location, ReadOptions options, CancellationToken token, int timeoutSeconds)
        {
            BaseMetadata metadata = null;

            using (MetadataDbCommands metadataCommands = new MetadataDbCommands())
            {

                metadata = metadataCommands.findMetadataByLocation(location);

                if (metadata != null)
                {
                    metadata.IsImported = true;
                                     
                    if (options.HasFlag(ReadOptions.LEAVE_STREAM_OPENED_AFTER_READ))
                    {
                        Stream data = FileUtils.waitForFileAccess(location, FileAccess.Read,
                            FILE_OPEN_ASYNC_TIMEOUT_MS, token);

                        metadata.Data = data;
                    }

                    // check if metadata stored in the database is outdated                
                    FileInfo info = new FileInfo(location);
                    info.Refresh();

                    if (info.Exists == false)
                    {
                        metadata.MetadataReadError = new FileNotFoundException("File not found", location);
                        return (metadata);
                    }

                    if ((info.LastWriteTime - metadata.LastModifiedDate) > TimeSpan.FromSeconds(10))
                    {
                        // metadata is outdated so update in database
                        Logger.Log.Info("Updated: " + location + " - Database timestamp: " + metadata.LastModifiedDate.ToString() + " Disk timestamp: " + info.LastWriteTime.ToString());
                        int id = metadata.Id;
                        metadata = MetadataFileFactory.read(location, options, token, timeoutSeconds);

                        if (metadata != null)
                        {
                            metadata.IsImported = true;
                            metadata.Id = id;
                            write(metadata, WriteOptions.WRITE_TO_DATABASE, null);
                        }

                    }

                    if (info.Attributes.HasFlag(FileAttributes.ReadOnly))
                    {
                        metadata.IsReadOnly = true;
                    }

                }
            }

            return (metadata);
        }