internal static ImageStoreFile GetFile(Guid id) { var connection = DatabaseConnection.Current; using (var command = new SqlCommand("Select [FolderId],[Path],[FileName],[ExtensionId],[ImageHash],[Sha1Hash],[FileSize],[FileState],[ImageComparedThreshold] from [File] Where [Id]=@Id")) { command.Connection = connection; command.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier) { Value = id }); using (var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) { ImageStoreFile result; if (reader.Read()) { result = new ImageStoreFile(id, (Guid)reader[0], (string)reader[1], (string)reader[2], (Guid)reader[3]) { ImageHash = DBNullableReader.ConvertFromReferenceType <byte[]>(reader[4]), Sha1Hash = DBNullableReader.ConvertFromReferenceType <byte[]>(reader[5]), FileSize = (int)reader[6], FileStateCode = (int)reader[7], ImageComparedThreshold = (float)reader[8] }; } else { result = null; } reader.Close(); return(result); } } }
internal static int UpdateRecord(ImageStoreFile file) { var connection = DatabaseConnection.Current; using (var command = new SqlCommand("Update [File] Set [ImageHash]=@ImageHash, [Sha1Hash]=@Sha1Hash, [FileSize]=@FileSize, [FileState]=@FileState, [ImageComparedThreshold]=@ImageComparedThreshold where [Id]=@Id")) { command.Connection = connection; command.Parameters.Add(new SqlParameter("@Id", System.Data.SqlDbType.UniqueIdentifier) { Value = file.Id }); command.Parameters.Add(new SqlParameter("@ImageHash", System.Data.SqlDbType.Binary, 40) { Value = DBNullableReader.NullCheck(file.ImageHash) }); command.Parameters.Add(new SqlParameter("@Sha1Hash", System.Data.SqlDbType.Binary, 20) { Value = DBNullableReader.NullCheck(file.Sha1Hash) }); command.Parameters.Add(new SqlParameter("@FileSize", System.Data.SqlDbType.Int) { Value = file.FileSize }); command.Parameters.Add(new SqlParameter("@FileState", System.Data.SqlDbType.Int) { Value = file.FileStateCode }); command.Parameters.Add(new SqlParameter("@ImageComparedThreshold", System.Data.SqlDbType.Real) { Value = file.ImageComparedThreshold }); return(command.ExecuteNonQuery()); } }
internal static IEnumerable <ImageStoreFile> GetAllFilesWithoutData(Guid folderId, int?top) { var connection = DatabaseConnection.Current; var text = " [Id],[Path],[FileName],[ExtensionId] from [File] where [FolderId]=@FolderId order by [Path],[FileName],[ExtensionId]"; using (var command = new SqlCommand()) { command.Connection = connection; command.CommandTimeout = 0; if (top != null) { command.CommandText = "SELECT TOP " + top.Value.ToString() + text; } else { command.CommandText = "SELECT" + text; } command.Parameters.Add(new SqlParameter("@FolderId", System.Data.SqlDbType.UniqueIdentifier) { Value = folderId }); using (var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) { while (reader.Read()) { ImageStoreFile line = new ImageStoreFile((Guid)reader[0], folderId, (string)reader[1], (string)reader[2], (Guid)reader[3]); yield return(line); } reader.Close(); } } }
protected override void ProcessRecord() { if (Path == null) { throw new ArgumentNullException(nameof(Path)); } if (FileName == null) { throw new ArgumentNullException(nameof(FileName)); } var connection = DatabaseConnection.Current; using (var command = new SqlCommand("Select [Id],[Path],[FileName],[ImageHash],[Sha1Hash],[FileSize],[FileState],[ImageComparedThreshold] from [File] Where [FolderId]=@FolderId and [Path]=@Path and [FileName]=@FileName and [ExtensionId]=@ExtensionId")) { command.Connection = connection; command.CommandTimeout = 0; command.Parameters.Add(new SqlParameter("@FolderId", System.Data.SqlDbType.UniqueIdentifier) { Value = FolderId }); command.Parameters.Add(new SqlParameter("@Path", System.Data.SqlDbType.NVarChar, 256) { Value = Path }); command.Parameters.Add(new SqlParameter("@FileName", System.Data.SqlDbType.NVarChar, 256) { Value = FileName }); command.Parameters.Add(new SqlParameter("@ExtensionId", System.Data.SqlDbType.UniqueIdentifier) { Value = ExtensionId }); using (var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) { if (reader.Read()) { ImageStoreFile line = new ImageStoreFile((Guid)reader[0], FolderId, (string)reader[1], (string)reader[2], ExtensionId) { ImageHash = DBNullableReader.ConvertFromReferenceType <byte[]>(reader[3]), Sha1Hash = DBNullableReader.ConvertFromReferenceType <byte[]>(reader[4]), FileSize = (int)reader[5], FileStateCode = (int)reader[6], ImageComparedThreshold = (float)reader[7] }; WriteObject(line); } else { WriteObject(null); } reader.Close(); } } }
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); }
protected override void ProcessRecord() { var connection = DatabaseConnection.Current; using (var command = new SqlCommand(" [Id],[FolderId],[Path],[FileName],[ExtensionId],[ImageHash],[Sha1Hash],[FileSize],[FileState],[ImageComparedThreshold] from [File]")) { command.Connection = connection; command.CommandTimeout = 0; if (Top.HasValue) { command.CommandText = "SELECT TOP " + Top.Value.ToString() + command.CommandText; } else { command.CommandText = "SELECT" + command.CommandText; } WhereCauseBuilder whereCauseBuilder = new WhereCauseBuilder(command.Parameters); whereCauseBuilder.AddUniqueIdentifierComparingCause("FolderId", FolderId); whereCauseBuilder.AddStringComparingCause("Path", Path, PathPropertyComparingModes); whereCauseBuilder.AddStringComparingCause("FileName", FileName, FileNamePropertyComparingModes); whereCauseBuilder.AddUniqueIdentifierComparingCause("ExtensionId", ExtensionId); whereCauseBuilder.AddBinaryComparingCause("ImageHash", ImageHash, ImageHashIsNull.IsPresent, 40); whereCauseBuilder.AddBinaryComparingCause("Sha1Hash", Sha1Hash, Sha1HashIsNull.IsPresent, 20); whereCauseBuilder.AddIntComparingCause("FileSize", FileSize, FileSizeGreaterOrEqual, FileSizeLessOrEqual); List <int> fileStates = new List <int>(); if (IncludesNewFile.IsPresent) { fileStates.Add(0); } if (IncludesNotImage.IsPresent) { fileStates.Add(1); } if (IncludesNotReadable.IsPresent) { fileStates.Add(2); } if (IncludesSizeZero.IsPresent) { fileStates.Add(254); } if (IncludesComputed.IsPresent) { fileStates.Add(255); } if (fileStates.Count != 5) { whereCauseBuilder.AddIntInRangeCause("FileState", fileStates); } whereCauseBuilder.AddRealComparingCause("ImageComparedThreshold", ImageComparedThreshold, ImageComparedThresholdGreaterOrEqual, ImageComparedThresholdLessOrEqual); command.CommandText += whereCauseBuilder.ToFullWhereCommand(); command.CommandText += " order by [FolderId], [Path], [FileName], [ExtensionId], [FileState]"; List <ImageStoreFile> result = new List <ImageStoreFile>(); using (var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) { while (reader.Read()) { ImageStoreFile line = new ImageStoreFile((Guid)reader[0], (Guid)reader[1], (string)reader[2], (string)reader[3], (Guid)reader[4]) { ImageHash = DBNullableReader.ConvertFromReferenceType <byte[]>(reader[5]), Sha1Hash = DBNullableReader.ConvertFromReferenceType <byte[]>(reader[6]), FileSize = (int)reader[7], FileStateCode = (int)reader[8], ImageComparedThreshold = (float)reader[9] }; result.Add(line); } reader.Close(); } WriteObject(result); } }
internal static IEnumerable <ImageStoreFile> GetAllFilesWithoutData(Guid folderId, int?top, bool onlyNew, bool includingComputed, bool includingNotImage, bool includingNotReadable, bool includingSizeZero) { var connection = DatabaseConnection.Current; string text; if (top != null) { text = "SELECT TOP " + top.Value.ToString(); } else { text = "SELECT"; } text += " [Id],[Path],[FileName],[ExtensionId],[FileState] from [File] where [FolderId]=@FolderId and "; if (onlyNew) { text += "[FileState]=0"; } else { List <string> states = new List <string>(); if (includingComputed) { states.Add("[FileState]=255"); } if (includingNotImage) { states.Add("[FileState]=1"); } if (includingNotReadable) { states.Add("[FileState]=2"); } if (includingSizeZero) { states.Add("[FileState]=254"); } if (states.Count == 1) { text += states[0]; } else { text += "(" + string.Join(" or ", states) + ")"; } } text += " order by [Path],[FileName],[ExtensionId]"; using (var command = new SqlCommand(text)) { command.Connection = connection; command.CommandTimeout = 0; command.Parameters.Add(new SqlParameter("@FolderId", System.Data.SqlDbType.UniqueIdentifier) { Value = folderId }); using (var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) { while (reader.Read()) { ImageStoreFile line = new ImageStoreFile((Guid)reader[0], folderId, (string)reader[1], (string)reader[2], (Guid)reader[3]) { FileStateCode = (int)reader[4] }; yield return(line); } reader.Close(); } } }