protected override void ProcessRecord()
        {
            if (File == null)
            {
                throw new ArgumentNullException(nameof(File));
            }

            bool isRecomputing;

            if (File.FileState == FileState.New)
            {
                isRecomputing = false;
            }
            else
            {
                if (Recompute.IsPresent)
                {
                    isRecomputing = true;
                }
                else
                {
                    ThrowTerminatingError(new ErrorRecord(new InvalidOperationException("State of this file is not set to New and -" + nameof(Recompute) + " is not set."), "ImageStore Measure File", ErrorCategory.InvalidOperation, File.Id));
                    isRecomputing = true; //Place here for avoiding compiling error. Won't be executed.
                }
            }

            var extension = ExtensionHelper.GetExtensionName(File.ExtensionId, out var extensionIsImage, out var extensionIgnored);

            if (extension == null)
            {
                throw new ArgumentException("Extension cannot be found.", nameof(File));
            }

            if (extensionIgnored)
            {
                throw new ArgumentException("Extension of this file is set to ignored.", nameof(File));
            }

            var folder = FolderHelper.GetFolderPath(File.FolderId, out _);

            if (folder == null)
            {
                throw new ArgumentException("Folder cannot be found.", nameof(File));
            }
            if (!folder.EndsWith(DirectorySeparatorString.Value))
            {
                folder += DirectorySeparatorString.Value;
            }

            Digest imageHash;

            byte[]    sha1Hash;
            int       fileSize;
            FileState fileState;

            using (var computer = new MeasureFileHelper())
            {
                var fullFilePath = FileHelper.GetFullFilePath(folder, File.Path, File.FileName, extension);
                var ex           = computer.ProcessFile(fullFilePath, extensionIsImage, out imageHash, out sha1Hash, out fileSize, out fileState);
                if (ex != null)
                {
                    WriteError(ex);
                }
            }

            var newFile = new ImageStoreFile(File.Id, File.FolderId, File.Path, File.FileName, File.ExtensionId)
            {
                ImageHashDigest        = imageHash,
                Sha1Hash               = sha1Hash,
                FileSize               = fileSize,
                FileState              = fileState,
                ImageComparedThreshold = 0
            };

            if (UpdateFileCmdlet.UpdateRecord(newFile) <= 0)
            {
                WriteError(new ErrorRecord(new InvalidOperationException("Cannot update this file."), "ImageStore - Measuring", ErrorCategory.WriteError, null));
            }

            if (isRecomputing)
            {
                var connection = DatabaseConnection.Current;
                using (var commandToDeleteSame = new SqlCommand("Delete from [SameFile] Where [FileId]=@Id"))
                    using (var commandToDeleteSimilar = new SqlCommand("Delete from [SimilarFile] Where [File1Id]=@Id or [File2Id]=@Id"))
                    {
                        commandToDeleteSame.Connection = connection;
                        commandToDeleteSame.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier)
                        {
                            Value = File.Id
                        });
                        commandToDeleteSame.ExecuteNonQuery();

                        commandToDeleteSimilar.Connection = connection;
                        commandToDeleteSimilar.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier)
                        {
                            Value = File.Id
                        });
                        commandToDeleteSimilar.ExecuteNonQuery();
                    }
            }
            WriteObject(newFile);
        }
Exemple #2
0
        int Compute(ImageStoreFolder folder, int?limit, bool remeasuringDisabled, bool remeasuringComputed, bool remeasuringNotImage, bool remeasuringNotReadable, bool remeasuringSizeZero)
        {
            WriteInformation("Measuring folder: " + folder.Name, new string[] { "MeasureFiles", "MeasureFolder" });

            //get records from database
            //path, isImage, id, isNew
            files = new List <Tuple <string, bool, Guid, bool> >();
            string baseFolder = folder.Path;

            if (!baseFolder.EndsWith(DirectorySeparatorString.Value))
            {
                baseFolder += DirectorySeparatorString.Value;
            }

            WriteVerbose("Reading records from database...");

            foreach (var file in FileHelper.GetAllFilesWithoutData(folder.Id, limit,
                                                                   remeasuringDisabled,
                                                                   remeasuringComputed,
                                                                   remeasuringNotImage,
                                                                   remeasuringNotReadable,
                                                                   remeasuringSizeZero))
            {
                var extension = extensionsById[file.ExtensionId];
                if (extension.Ignored)
                {
                    continue;
                }

                string fullPath = FileHelper.GetFullFilePath(baseFolder, file.Path, file.FileName, extension.Extension);

                files.Add(new Tuple <string, bool, Guid, bool>(fullPath, extension.IsImage, file.Id, file.FileState == FileState.New));
            }

            filesCount       = files.Count;
            filesCountSingle = filesCount;
            filesCountText   = files.Count.ToString();
            if (filesCount == 0)
            {
                files = null;
                WriteVerbose("Nothing to be measuring in this folder.");
                return(0);
            }
            else if (filesCount == 1)
            {
                WriteVerbose("One record is read from database.");
            }
            else
            {
                WriteVerbose(filesCountText + " records are read from database.");
            }

            WriteVerbose("Starting measuring...");

            outputs = new BlockingCollection <Tuple <int, string> >();

            finishedCount = 0;

            Thread writing = new Thread(WriteDatabase);

            using (var measuring = new Task(ComputeAll, TaskCreationOptions.LongRunning))
                using (toWrite = new BlockingCollection <Tuple <Guid, byte[], byte[], int, FileState, bool> >())
                {
                    writing.Start();
                    measuring.Start();

                    while (true)
                    {
                        try
                        {
                            var itemToDisplay = outputs.Take();
                            if (itemToDisplay.Item1 == 2)
                            {
                                //Other exception
                                WriteWarning(itemToDisplay.Item2);
                            }
                            else
                            {
                                if (itemToDisplay.Item1 == 0)
                                {
                                    //file finished
                                    WriteVerbose(itemToDisplay.Item2);
                                }
                                else //1
                                {
                                    //file failed
                                    WriteWarning(itemToDisplay.Item2);
                                }
                            }
                        }
                        catch (InvalidOperationException) { break; }
                    }

                    measuring.Wait();
                    writing.Join();
                }
            toWrite.Dispose();
            toWrite = null;
            outputs.Dispose();
            outputs = null;
            files   = null;

            return(filesCount);
        }