void OnAppDataChanged(object sender, FSEventStreamEventsArgs e) { foreach (var evnt in e.Events) { numDataChanged++; String message = numDataChanged + " " + evnt; System.Diagnostics.Debug.WriteLine(evnt); FileSystemWatcherObject o = new FileSystemWatcherObject(evnt.Path.Replace(path + "/", ""), evnt.Path, (evnt.Flags.HasFlag(FSEventStreamEventFlags.ItemIsDir) ? TargetType.Folder : TargetType.File)); if (evnt.Flags.HasFlag(FSEventStreamEventFlags.ItemRenamed)) { Event?.Invoke(FileSystemWatcherEventArgs.CreateRenamedEvent(o)); } else if (evnt.Flags.HasFlag(FSEventStreamEventFlags.ItemCreated)) { Event?.Invoke(FileSystemWatcherEventArgs.CreateCreatedEvent(o)); } else if (evnt.Flags.HasFlag(FSEventStreamEventFlags.ItemRemoved)) { Event?.Invoke(FileSystemWatcherEventArgs.CreateDeletedEvent(o)); } else if (evnt.Flags.HasFlag(FSEventStreamEventFlags.ItemModified)) { Event?.Invoke(FileSystemWatcherEventArgs.CreateChangedEvent(o)); } } }
private void OnAppDataRenamed(object source, RenamedEventArgs e) { FileSystemWatcherObject newObject = new FileSystemWatcherObject(e.Name, e.FullPath, (Directory.Exists(e.FullPath)) ? TargetType.Folder : TargetType.File); FileSystemWatcherObject oldObject = new FileSystemWatcherObject(e.OldName, e.OldFullPath, (Directory.Exists(e.FullPath)) ? TargetType.Folder : TargetType.File); Event?.Invoke(FileSystemWatcherEventArgs.CreateRenamedEvent(newObject, oldObject)); }
// https://docs.microsoft.com/en-us/uwp/api/windows.storage.search.istoragequeryresultbase // https://docs.microsoft.com/en-us/uwp/api/windows.storage.search.storageitemqueryresult // https://docs.microsoft.com/en-us/uwp/api/windows.storage.istorageitem private async void OnAppDataChanged(Windows.Storage.Search.IStorageQueryResultBase sender, object args) { FileSystemWatcherObject o = new FileSystemWatcherObject(storageFolder.DisplayName, GetPath(), TargetType.Folder); Event?.Invoke(FileSystemWatcherEventArgs.CreateContentChangedEvent(o)); var newFileList = await query.GetItemsAsync(); DiffSet diffSet = await Diff(oldFileList, newFileList); oldFileList = newFileList; foreach (IStorageItem file in diffSet.Added) { o = new FileSystemWatcherObject(file.Name, file.Path, (file.IsOfType(StorageItemTypes.Folder) ? TargetType.Folder : TargetType.File)); Event?.Invoke(FileSystemWatcherEventArgs.CreateCreatedEvent(o)); } foreach (IStorageItem file in diffSet.Deleted) { o = new FileSystemWatcherObject(file.Name, file.Path, (file.IsOfType(StorageItemTypes.Folder) ? TargetType.Folder : TargetType.File)); Event?.Invoke(FileSystemWatcherEventArgs.CreateDeletedEvent(o)); } foreach (IStorageItem file in diffSet.Changed) { o = new FileSystemWatcherObject(file.Name, file.Path, (file.IsOfType(StorageItemTypes.Folder) ? TargetType.Folder : TargetType.File)); Event?.Invoke(FileSystemWatcherEventArgs.CreateChangedEvent(o)); } }
private bool DataSourceIsDisabled(int dataSourceId, FileSystemWatcherEventArgs e) { DataSource dataSource = new DataSource(dataSourceId, string.Empty); if (dataSource.IsActive == false) { #region Getting Ignored Folder string ignoredFolder = Path.Combine(SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryIgnored, dataSourceId.ToString()); ignoredFolder = Path.Combine(ignoredFolder, DateTime.Now.ToDBDateFormat()); if (!Directory.Exists(ignoredFolder)) { Directory.CreateDirectory(ignoredFolder); } #endregion Getting Ignored Folder #region Getting Ignored File string ignoredFile = Path.Combine(ignoredFolder, Path.GetFileName(e.FileName)); if (File.Exists(ignoredFile)) { string ignoredFileBUName = Path.Combine(ignoredFolder, string.Format("{0}_{1}", ShortGuid.NewGuid(), Path.GetFileName(e.FileName))); FileCopy(ignoredFile, ignoredFileBUName, true); //backup existing } #endregion Getting Ignored File new FileUtility().FileCopy(e.FileName, ignoredFile, true); Trace.TraceInformation("A file from {0} was ignored as the datasource is disabled. The file can be found at {1}", dataSource.Name, ignoredFile); #region Sending Email string emailBody = string.Format("A file from <b>{0}</b> was ignored as the datasource is disabled. The file can be found at <b>{1}</b>", dataSource.Name, ignoredFile); emailBody = PostMan.__warningStartTag + emailBody + PostMan.__warningEndTag; new PostMan(dataSource, false).Send(emailBody, "File Ignored"); #endregion Sending Email return(true); } return(false); }
internal void FileDownloaded(FileSystemWatcherEventArgs e) { #region Getting all required information int dataSourceId = 0; int.TryParse(e.ApplicationParameters["DataSourceId"].ToString(), out dataSourceId); if (dataSourceId == 0) { return; } string processingBy = "u175675"; //todo string onlyFileName = Path.GetFileNameWithoutExtension(e.FileName); string fileExt = Path.GetExtension(e.FileName); List <SreKey> keys = Cache.Instance.Bag[dataSourceId + ".keys"] as List <SreKey>; if (keys == null) { if (_Manager == null) { _Manager = new Manager(); } keys = _Manager.GetApplicationKeys(dataSourceId, true); Cache.Instance.Bag.Add(dataSourceId + ".keys", keys); } string OutputFolder = DataSource.GetOutputFolder(dataSourceId, keys); string actualOutputFolder = OutputFolder; string outputExtension = keys.GetKeyValue(SreKeyTypes.OutputFileExtension); if (string.IsNullOrEmpty(outputExtension)) { outputExtension = ".xml"; } string outputFileName = string.Empty; string outputFileNameKey = keys.GetKeyValue(SreKeyTypes.OutputFileName); if (string.IsNullOrEmpty(outputFileNameKey)) { outputFileName = Path.Combine(OutputFolder, onlyFileName + outputExtension); } else { outputFileName = FormatOutputFileName(outputFileNameKey, onlyFileName); outputFileName = Path.Combine(OutputFolder, outputFileName + outputExtension); } Directory.CreateDirectory(Path.GetDirectoryName(outputFileName)); string withError = string.Empty; string withWarning = string.Empty; string appWatchFilter = keys.GetKeyValue(SreKeyTypes.WatchFilter); string zipInterfaceName = keys.GetKeyValue(SreKeyTypes.ZipInterfaceName); if ((fileExt.ToLower() == ".zip") || (fileExt.ToLower() == ".rar") || (fileExt.ToLower() == ".tar")) { OutputFolder = Path.Combine(SymplusCoreConfigurationSection.CurrentConfig.TempDirectory, Constants.SREBaseFolderName); OutputFolder = Path.Combine(OutputFolder, "RedirectedOutput"); OutputFolder = Path.Combine(OutputFolder, DateTime.Now.ToDBDateFormat()); OutputFolder = Path.Combine(OutputFolder, dataSourceId.ToString()); } if ((!string.IsNullOrEmpty(appWatchFilter)) && (appWatchFilter != FileExtensionSupportAll)) { List <string> filters = new List <string>(); if (appWatchFilter.Contains("|")) { filters.AddRange(appWatchFilter.ToLower().Split("|".ToCharArray())); } else { filters.Add(appWatchFilter.ToLower()); } var filterOrNot = (from f in filters where f == e.FileExtension.ToLower() select f).SingleOrDefault(); if (filterOrNot == null) { if (!onlyFileName.StartsWith(Constants.UnzippedFilePrefix)) { SreMessage warn = new SreMessage(SreMessageCodes.SRE_FILE_TYPE_NOT_SUPPORTED); DataSource dataSource = new DataSource(dataSourceId, string.Empty); withWarning = string.Format(warn.Message, dataSource.Name, appWatchFilter, Path.GetFileName(e.FileName)); Trace.TraceInformation(withWarning); new PostMan(dataSource).Send(PostMan.__warningStartTag + withWarning + PostMan.__warningEndTag, "File Ignored"); return; } } } string zipUniuqeId = string.Empty; bool isRequestFromWCF = false; string jobId = string.Empty; if (onlyFileName.StartsWith(Constants.WCFFilePrefix)) { isRequestFromWCF = true; jobId = onlyFileName.Replace(Constants.WCFFilePrefix, ""); jobId = jobId.Replace(fileExt, ""); } else if (onlyFileName.StartsWith(Constants.UnzippedFilePrefix)) { zipUniuqeId = ZipFileWatcher.ExtractUniqueId(onlyFileName); OutputFolder = Path.Combine(OutputFolder, zipUniuqeId); if (!Directory.Exists(OutputFolder)) { Directory.CreateDirectory(OutputFolder); } outputFileName = Path.Combine(OutputFolder, onlyFileName + outputExtension); outputFileName = ZipFileWatcher.ExtractActualFileName(outputFileName); } #endregion Getting all required information if (!isRequestFromWCF) { Trace.TraceInformation("Got a new file for {0} - '{1}'", dataSourceId, e.FileName); } else { Trace.TraceInformation("Got a new WCF request for {0}, JobId = '{1}'", dataSourceId, jobId); } if (DataSourceIsDisabled(dataSourceId, e)) { return; } StringBuilder result = new StringBuilder(); JobProcessor jobProcessor = new JobProcessor(); if (isRequestFromWCF) { jobProcessor.IsWCFRequest = true; result = jobProcessor.ProcessJob(dataSourceId, string.Empty, processingBy, onlyFileName, jobId, withError, withWarning); } else if ((fileExt.ToLower() == ".zip") || (fileExt.ToLower() == ".rar") || (fileExt.ToLower() == ".tar")) { zipUniuqeId = new ZipFileWatcher(e.FileName, dataSourceId, zipInterfaceName, processingBy, OutputFolder, e.RenamedToIdentifier).Handle(); OutputFolder = Path.Combine(DataSource.GetOutputFolder(dataSourceId, keys), zipUniuqeId); if (!Directory.Exists(OutputFolder)) { Directory.CreateDirectory(OutputFolder); } outputFileName = Path.Combine(OutputFolder, onlyFileName + fileExt); result.AppendLine(zipUniuqeId); } else if ((fileExt.ToLower() == ".xls") || (fileExt.ToLower() == ".xlsx")) { result = jobProcessor.ProcessSpreadSheet(dataSourceId, string.Empty, processingBy, e.FileName); } else if (fileExt.ToLower() == ".edi") { new EDIX12FileWatcher(dataSourceId, e.FileName).Process(); Trace.TraceInformation("{0} successfully processed. Output file was {1}", e.FileName, outputFileName); if (File.Exists(outputFileName)) { InvokeFileProcessed(dataSourceId, jobProcessor.JobId, keys, outputFileName, actualOutputFolder, zipUniuqeId); } jobProcessor.Dispose(); return; } else { result = jobProcessor.ProcessJob(dataSourceId, string.Empty, processingBy, e.FileName, string.Empty, withError, withWarning); } if (File.Exists(outputFileName)) { string buName = Path.Combine(OutputFolder, string.Format("{0}_{1}", e.RenamedToIdentifier, Path.GetFileName(outputFileName))); FileCopy(outputFileName, buName, true); //backup existing } if (((fileExt.ToLower() == ".zip") || (fileExt.ToLower() == ".rar") || (fileExt.ToLower() == ".tar")) && (keys.GetKeyValue(SreKeyTypes.ZipDoNotCreateAcknoledgementInOutputFolder).ParseBool())) { Trace.TraceInformation("The data source '{0}' has been configured as not to copy zip acknoledgement file. File will not be created!", dataSourceId); return; } if (result.Length > 0) { using (StreamWriter tw = new StreamWriter(outputFileName)) { tw.Write(result); tw.Close(); } Trace.TraceInformation("{0} successfully processed. Output file was {1}", e.FileName, outputFileName); InvokeFileProcessed(dataSourceId, jobProcessor.JobId, keys, outputFileName, actualOutputFolder, zipUniuqeId); } else { Trace.TraceInformation("Failed to process '{0}', empty data came from output writer! Check log for more details.", e.FileName); } jobProcessor.Dispose(); }
private void OnAppDataDeleted(object source, FileSystemEventArgs e) { FileSystemWatcherObject o = new FileSystemWatcherObject(e.Name, e.FullPath, (Directory.Exists(e.FullPath)) ? TargetType.Folder : TargetType.File); Event?.Invoke(FileSystemWatcherEventArgs.CreateDeletedEvent(o)); }