protected override void OnStart(string[] args)
        {
            DirectoryWatcherConfiguration configuration = null;
            string configurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");

            if (File.Exists(configurationPath))
            {
                using (StreamReader r = Helpers.GetStreamReader(configurationPath))
                {
                    string json = r.ReadToEnd();
                    configuration = JsonConvert.DeserializeObject <DirectoryWatcherConfiguration>(json);
                }

                if (configuration != null)
                {
                    _directoryWatcher = new DirectoryWatcher(configuration);
                    _directoryWatcher.Start();
                }
                else
                {
                    EventLogManager.WriteError(new Exception("No configuration."));
                }
            }
            else
            {
                EventLogManager.WriteError(new Exception("No config.json file found."));
            }
        }
Ejemplo n.º 2
0
        private void CopyToDirectory(string sourcePath, string destinationPath, bool allowOverwrite = false)
        {
            if (!Directory.Exists(destinationPath))
            {
                Directory.CreateDirectory(destinationPath);
            }
            else if (!allowOverwrite)
            {
                throw new Exception(String.Format("Directory {0} already exists.", destinationPath));
            }

            FileStream s = null;

            foreach (string filePath in Directory.GetFiles(sourcePath))
            {
                try
                {
                    s = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
                }
                catch (Exception)
                {
                    EventLogManager.WriteError("Cannot open file " + filePath);
                }

                if (s != null)
                {
                    s.Close();
                }

                File.Copy(filePath, System.IO.Path.Combine(destinationPath, System.IO.Path.GetFileName(filePath)));
            }
        }
Ejemplo n.º 3
0
        public void Start()
        {
            if (this._fileSystemWatcher != null)
            {
                #region Process anything sitting the watcher directory initially

                IEnumerable <string> initialDirectories = Directory.GetDirectories(this._fileSystemWatcher.Path).Where(d => d != this._workPath && d != this._backupsPath && d != this._errorsPath);
                if (initialDirectories != null && initialDirectories.Count() > 0)
                {
                    EventLogManager.WriteInformation(String.Format("Processing {0} initial directories.", initialDirectories.Count()));

                    foreach (var initialDirectory in initialDirectories)
                    {
                        ProcessDirectory(initialDirectory);
                        Thread.Sleep(100);
                    }
                }

                #endregion

                this._fileSystemWatcher.EnableRaisingEvents = true;

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("RAMPdfFileService Started");
                sb.AppendLine(String.Format("Watching at: {0}", this._fileSystemWatcher.Path));
                sb.AppendLine(String.Format("Internal buffer size: {0}", this._fileSystemWatcher.InternalBufferSize));
                sb.AppendLine(String.Format("Saving backups: {0}", this.DoSaveBackups));
                sb.AppendLine(String.Format("Saving errors: {0}", this.DoSaveErrors));
                EventLogManager.WriteInformation(sb.ToString());
            }
            else
            {
                EventLogManager.WriteError("Unable to start RAMPdfFileService.  No FileSystemWatcher present.");
            }
        }
Ejemplo n.º 4
0
        public DirectoryWatcher(DirectoryWatcherConfiguration configuration)
        {
            if (!String.IsNullOrWhiteSpace(configuration.WatcherDirectory) && Directory.Exists(configuration.WatcherDirectory))
            {
                this._fileSystemWatcher = new FileSystemWatcher(configuration.WatcherDirectory)
                {
                    NotifyFilter          = NotifyFilters.DirectoryName,
                    IncludeSubdirectories = false,
                    InternalBufferSize    = configuration.InternalBufferSize
                };
                this.DoSaveBackups = configuration.DoSaveBackups;
                this.DoSaveErrors  = configuration.DoSaveErrors;
                this._fileSystemWatcher.Created += DirectoryWatcher_OnCreated;

                #region Create subdirecories used for processing files

                this._errorsPath = System.IO.Path.Combine(configuration.WatcherDirectory, "errors");

                if (!Directory.Exists(this._errorsPath))
                {
                    Directory.CreateDirectory(this._errorsPath, Directory.GetAccessControl(configuration.WatcherDirectory));
                    EventLogManager.WriteInformation(String.Format("Errors directory created at {0}", this._errorsPath));
                }

                this._backupsPath = System.IO.Path.Combine(configuration.WatcherDirectory, "backups");

                if (!Directory.Exists(this._backupsPath))
                {
                    Directory.CreateDirectory(this._backupsPath, Directory.GetAccessControl(configuration.WatcherDirectory));
                    EventLogManager.WriteInformation(String.Format("Backups directory created at {0}", this._backupsPath));
                }

                this._workPath = System.IO.Path.Combine(configuration.WatcherDirectory, "work");

                if (!Directory.Exists(this._workPath))
                {
                    Directory.CreateDirectory(this._workPath, Directory.GetAccessControl(configuration.WatcherDirectory));
                    EventLogManager.WriteInformation(String.Format("Work directory created at {0}", this._workPath));
                }

                #endregion
            }
            else
            {
                EventLogManager.WriteError(new Exception(String.Format("Path {0} does not exist.", configuration.WatcherDirectory)));
            }
        }
Ejemplo n.º 5
0
        private void ProcessDirectory(string directoryPath)
        {
            // DON'T TOUCH THE BACKUPS, ERRORS AND WORK DIRECTORIES.  Just in case they were made or renamed after the fact for some reason
            if (directoryPath != this._errorsPath && directoryPath != this._backupsPath && directoryPath != this._workPath)
            {
                string pdfJsonPath = System.IO.Path.Combine(directoryPath, "pdf.json");

                if (File.Exists(pdfJsonPath))
                {
                    string workPath = System.IO.Path.Combine(this._workPath, System.IO.Path.GetFileName(directoryPath));

                    try
                    {
                        CopyToDirectory(directoryPath, workPath);

                        PdfMerge pdfMerge = null;

                        string jsonPath = System.IO.Path.Combine(workPath, "pdf.json");
                        using (StreamReader r = Helpers.GetStreamReader(jsonPath))
                        {
                            string json = r.ReadToEnd();
                            pdfMerge = JsonConvert.DeserializeObject <PdfMerge>(json);
                        }

                        FillFormFields(workPath, pdfMerge);
                        MergePdfs(workPath, pdfMerge);
                        //NumberPages(workPath, pdfMerge);
                        FinishPdf(workPath, pdfMerge);

                        // Move original to backups directory
                        if (DoSaveBackups)
                        {
                            string backupsPath = System.IO.Path.Combine(this._backupsPath, String.Format("{0}_{1}", System.IO.Path.GetFileName(directoryPath), DateTime.Now.ToString("yyyyMMddHHmmss")));
                            Directory.Move(directoryPath, backupsPath);
                        }
                        else
                        {
                            Directory.Delete(directoryPath, true);
                        }
                    }
                    catch (Exception ex)
                    {
                        EventLogManager.WriteError(ex);

                        if (DoSaveErrors)
                        {
                            // Move original to errors directory
                            string errorsPath = System.IO.Path.Combine(this._errorsPath, String.Format("{0}_{1}", System.IO.Path.GetFileName(directoryPath), DateTime.Now.ToString("yyyyMMddHHmmss")));
                            Directory.Move(directoryPath, errorsPath);
                        }
                        else
                        {
                            Directory.Delete(directoryPath, true);
                        }
                    }

                    // Delete work directory
                    Directory.Delete(workPath, true);
                }
                else
                {
                    EventLogManager.WriteInformation(String.Format("No pdf.json file.  {0} skipped.", directoryPath));
                }
            }
        }