private string GetSessionId(IDataRecord record)
        {
            // See if we can use the name of the delivered file
            object result = record["DeliveredFileName"];

            if (result != null)
            {
                string fileName = result.ToString();
                if (ScanFilePrefix.MatchesPattern(fileName))
                {
                    ScanFilePrefix prefix = ScanFilePrefix.Parse(fileName);
                    return(prefix.SessionId);
                }
            }

            // See if we can use the fax number
            result = record["FaxNumber"];
            if (result != null)
            {
                try
                {
                    ScanFilePrefix prefix = ScanFilePrefix.ParseFromFax(result.ToString(), "u");
                    return(prefix.SessionId);
                }
                catch (FormatException)
                {
                    // Wrong format - do nothing
                }
            }

            // Use the last session we saw
            return(_sessionId);
        }
Пример #2
0
        private void ProcessDocument(SharePointDocument document, string filePath)
        {
            TraceFactory.Logger.Debug("Found file: " + document.FileName);

            try
            {
                string         fileName   = Path.GetFileName(filePath);
                ScanFilePrefix filePrefix = ScanFilePrefix.Parse(fileName);

                // Create the log for this file
                DigitalSendJobOutputLogger log = new DigitalSendJobOutputLogger(fileName, filePrefix.ToString(), filePrefix.SessionId);
                log.FileSentDateTime = document.Created.LocalDateTime;
                log.FileLocation     = $@"{_library.SiteUrl.Host}\{_library.Name}";

                // Validate and analyze the file
                OutputProcessor  processor = new OutputProcessor(filePath);
                ValidationResult result    = null;
                Retry.WhileThrowing(
                    () => result = processor.Validate(Configuration),
                    10,
                    TimeSpan.FromSeconds(2),
                    new List <Type>()
                {
                    typeof(IOException)
                });

                // If the validation failed, there is a small chance that the HPS file arrived
                // a little later than the PDF and didn't get downloaded at the same time.
                // Check the server to see if that's the case - if so, run the validation again
                if (!result.Succeeded && result.Message.Contains("metadata", StringComparison.OrdinalIgnoreCase))
                {
                    if (FindMetadataFile(document))
                    {
                        result = processor.Validate(Configuration);
                    }
                }

                DocumentProperties properties = processor.GetProperties();
                log.FileSizeBytes = properties.FileSize;
                log.PageCount     = properties.Pages;
                log.SetErrorMessage(result);

                // Clean up the file
                processor.ApplyRetention(Configuration, result.Succeeded);

                // Send the output log
                new DataLogger(GetLogServiceHost(filePrefix.SessionId)).Submit(log);
            }
            catch (IOException ex)
            {
                LogProcessFileError(filePath, ex);
            }
            catch (FormatException ex)
            {
                LogProcessFileError(filePath, ex);
            }
        }
Пример #3
0
        /// <summary>
        /// Applies the retention policy from the specified <see cref="OutputMonitorConfig"/> to the file.
        /// </summary>
        /// <param name="options">The validation/retention options.</param>
        /// <param name="isFileValid">if set to <c>true</c> the file should be considered valid.</param>
        public void ApplyRetention(OutputMonitorConfig options, bool isFileValid = true)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            // If the retention option is DoNothing, quit now
            if (options.Retention == RetentionOption.DoNothing)
            {
                return;
            }

            // Determine the "real" name for the file
            string file       = _analyzer.File.FullName;
            string filePrefix = options.RetentionFileName ?? Path.GetFileNameWithoutExtension(file);

            // Determine whether we should retain or delete the output file(s)
            bool retain = options.Retention == RetentionOption.AlwaysRetain ||
                          (options.Retention == RetentionOption.RetainIfCorrupt && isFileValid == false);

            // Apply the retention option for the file
            string retentionDirectory = string.Empty;

            if (retain)
            {
                string session = ScanFilePrefix.Parse(filePrefix).SessionId;
                retentionDirectory = options.RetentionLocation.Replace("{SESSION}", session);
                System.IO.Directory.CreateDirectory(retentionDirectory);
                CopyFile(file, retentionDirectory, filePrefix, Path.GetExtension(file));
            }
            File.Delete(file);

            // If we were expecting a metadata file, apply the retention options there as well
            if (options.LookForMetadataFile)
            {
                string metadataFile = Path.ChangeExtension(file, options.MetadataFileExtension);
                if (Retry.UntilTrue(() => File.Exists(metadataFile), 5, TimeSpan.FromSeconds(1)))
                {
                    if (retain)
                    {
                        CopyFile(metadataFile, retentionDirectory, filePrefix, options.MetadataFileExtension);
                    }
                    File.Delete(metadataFile);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Processes the located file.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="createdEventTime">The created event time.</param>
        protected virtual void ProcessFile(string filePath, DateTime?createdEventTime = null)
        {
            TraceFactory.Logger.Debug("Found file: {0}".FormatWith(filePath));

            try
            {
                string         fileName   = Path.GetFileName(filePath);
                ScanFilePrefix filePrefix = ScanFilePrefix.Parse(fileName);

                // Create the log for this file
                DigitalSendJobOutputLogger log = new DigitalSendJobOutputLogger(fileName, filePrefix.ToString(), filePrefix.SessionId);
                log.FileSentDateTime     = System.IO.Directory.GetCreationTime(filePath);
                log.FileReceivedDateTime = createdEventTime;
                log.FileLocation         = "{0} - {1}".FormatWith(Environment.MachineName, Path.GetDirectoryName(filePath));

                // Validate and analyze the file
                OutputProcessor  processor = new OutputProcessor(filePath);
                ValidationResult result    = null;
                Retry.WhileThrowing(
                    () => result = processor.Validate(Configuration),
                    10,
                    TimeSpan.FromSeconds(2),
                    new Collection <Type>()
                {
                    typeof(IOException)
                });

                DocumentProperties properties = processor.GetProperties();
                log.FileSizeBytes = properties.FileSize;
                log.PageCount     = properties.Pages;
                log.SetErrorMessage(result);

                // Clean up the file
                processor.ApplyRetention(Configuration, result.Succeeded);

                // Send the output log
                new DataLogger(GetLogServiceHost(filePrefix.SessionId)).Submit(log);
            }
            catch (IOException ex)
            {
                LogProcessFileError(filePath, ex);
            }
            catch (FormatException ex)
            {
                LogProcessFileError(filePath, ex);
            }
        }
Пример #5
0
        protected virtual bool ProcessMessage(EmailMessage message)
        {
            EmailAttachment attachment = message.Attachments.FirstOrDefault();

            if (attachment != null)
            {
                // Determine if we need to pull the file name from the subject
                string fileName = attachment.FileName;
                if (!ScanFilePrefix.MatchesPattern(attachment.FileName))
                {
                    fileName = message.Subject + Path.GetExtension(attachment.FileName);
                }
                TraceFactory.Logger.Debug("Found attachment: " + fileName);

                try
                {
                    ScanFilePrefix filePrefix = ScanFilePrefix.Parse(Path.GetFileName(fileName));

                    // Create the log for this file
                    DigitalSendJobOutputLogger log = new DigitalSendJobOutputLogger(fileName, filePrefix.ToString(), filePrefix.SessionId);
                    log.FileSentDateTime     = message.DateTimeSent;
                    log.FileReceivedDateTime = message.DateTimeReceived;
                    log.FileLocation         = _emailAddress.ToString();

                    // Save the attachment locally
                    FileInfo file = attachment.Save(_tempPath, fileName);

                    // Validate and analyze the file
                    OutputProcessor  processor = new OutputProcessor(file.FullName);
                    ValidationResult result    = null;
                    Retry.WhileThrowing(
                        () => result = processor.Validate(base.Configuration),
                        10,
                        TimeSpan.FromSeconds(2),
                        new Collection <Type>()
                    {
                        typeof(IOException)
                    });

                    DocumentProperties properties = processor.GetProperties();
                    log.FileSizeBytes = properties.FileSize;
                    log.PageCount     = properties.Pages;
                    log.SetErrorMessage(result);

                    // Clean up the file
                    processor.ApplyRetention(base.Configuration, result.Succeeded);

                    // One last check - if there was more than one attachment, flag this as an error
                    if (message.Attachments.Count > 1)
                    {
                        log.ErrorMessage += " {0} attachments with one email.".FormatWith(message.Attachments.Count);
                    }

                    // Send the output log
                    new DataLogger(GetLogServiceHost(filePrefix.SessionId)).Submit(log);
                }
                catch (Exception ex)
                {
                    LogProcessFileError(fileName, ex);
                    return(false);
                }
            }
            else
            {
                TraceFactory.Logger.Debug("Found email with subject {0} but no attachments.".FormatWith(message.Subject));
            }

            return(true);
        }