/// <summary> /// Performs basic validation on the file being analyzed by this <see cref="FileAnalyzer" />. /// </summary> /// <returns>A <see cref="FileValidationResult" /> object representing the result of validation.</returns> public override FileValidationResult Validate() { try { using (ExcelApp app = new ExcelApp()) { app.Open(File); } return(FileValidationResult.Pass); } catch (COMException ex) { LogWarn("File failed to open: " + ex.Message); return(FileValidationResult.Fail(ex.Message)); } }
/// <summary> /// Performs basic validation on the file being analyzed by this <see cref="FileAnalyzer" />. /// </summary> /// <returns>A <see cref="FileValidationResult" /> object representing the result of validation.</returns> public override FileValidationResult Validate() { try { LogDebug($"Loading TIF image: {File.Name}"); using (Image tiff = Image.FromFile(File.FullName)) { return(FileValidationResult.Pass); } } catch (OutOfMemoryException) { LogWarn("Unable to load image."); return(FileValidationResult.Fail("The file is not a valid TIF document.")); } }
/// <summary> /// Performs basic validation on the file being analyzed by this <see cref="FileAnalyzer" />. /// </summary> /// <returns>A <see cref="FileValidationResult" /> object representing the result of validation.</returns> public override FileValidationResult Validate() { string firstLine = System.IO.File.ReadLines(File.FullName).FirstOrDefault(); // If the first line is empty, the file is invalid. if (string.IsNullOrEmpty(firstLine)) { return(FileValidationResult.Fail("Empty file.")); } // A well-formed PDF must start with this text. if (!firstLine.StartsWith("%PDF-", StringComparison.OrdinalIgnoreCase)) { return(FileValidationResult.Fail("Malformed first line.")); } // There should be an EOF flag at the end. string lastLine = System.IO.File.ReadLines(File.FullName).Last(); if (lastLine != "%%EOF") { return(FileValidationResult.Fail("File did not include EOF flag.")); } // Finally, use the PDF library to load the file and ensure there were no other problems. try { LogDebug($"Loading PDF file: {File.Name}"); using (PdfDocument doc = PdfReader.Open(File.FullName, PdfDocumentOpenMode.ReadOnly)) { return(FileValidationResult.Pass); } } catch (Exception ex) when(ex is PdfSharpException || ex is InvalidOperationException) { LogWarn("File failed to open: " + ex.Message); return(FileValidationResult.Fail(ex.Message)); } }