Esempio n. 1
0
        public async Task <IEnumerable <AnalogyLogMessage> > Process(IAnalogyOfflineDataProvider fileDataProvider,
                                                                     string filename, CancellationToken token, bool isReload = false)
        {
            //TODO in case of zip recursive call on all extracted files


            FileName = filename;
            if (string.IsNullOrEmpty(FileName))
            {
                return(new List <AnalogyLogMessage>());
            }
            if (!isReload && !DataWindow.ForceNoFileCaching &&
                FileProcessingManager.Instance.AlreadyProcessed(FileName) &&
                Settings.EnableFileCaching) //get it from the cache
            {
                var cachedMessages = FileProcessingManager.Instance.GetMessages(FileName);
                DataWindow.AppendMessages(cachedMessages, Utils.GetFileNameAsDataSource(FileName));
                if (LogWindow != null)
                {
                    Interlocked.Decrement(ref LogWindow.fileLoadingCount);
                }
                return(cachedMessages);
            }

            if (FileProcessingManager.Instance.IsFileCurrentlyBeingProcessed(FileName))
            {
                while (FileProcessingManager.Instance.IsFileCurrentlyBeingProcessed(FileName))
                {
                    await Task.Delay(1000);
                }

                var cachedMessages = FileProcessingManager.Instance.GetMessages(FileName);
                DataWindow.AppendMessages(cachedMessages, Utils.GetFileNameAsDataSource(FileName));
                if (LogWindow != null)
                {
                    Interlocked.Decrement(ref LogWindow.fileLoadingCount);
                }
                return(cachedMessages);
            }

            //otherwise read file:
            try
            {
                if (fileDataProvider.CanOpenFile(filename)) //if can open natively: add to processing and process
                {
                    FileProcessingManager.Instance.AddProcessingFile(FileName);

                    if (!DataWindow.DoNotAddToRecentHistory)
                    {
                        Settings.AddToRecentFiles(fileDataProvider.ID, FileName);
                    }

                    var messages = (await fileDataProvider.Process(filename, token, DataWindow).ConfigureAwait(false))
                                   .ToList();
                    FileProcessingManager.Instance.DoneProcessingFile(messages.ToList(), FileName);
                    if (messages.Any())
                    {
                        lastNewestMessage = messages.Select(m => m.Date).Max();
                    }
                    OnFileReadingFinished?.Invoke(this, EventArgs.Empty);
                    if (LogWindow != null)
                    {
                        Interlocked.Decrement(ref LogWindow.fileLoadingCount);
                    }
                    return(messages);
                }
                else //cannot open natively. is it compressed file?
                {
                    if (Utils.IsCompressedArchive(filename))
                    {
                        var    compressedMessages = new List <AnalogyLogMessage>();
                        string extractedPath      = UnzipFilesIntoTempFolder(filename, fileDataProvider);
                        if (string.IsNullOrEmpty(extractedPath))
                        {
                            string msg = $"File is not supported by {fileDataProvider}. File: {filename}";
                            AnalogyLogger.Instance.LogCritical("Analogy", msg);
                            AnalogyLogMessage error = new AnalogyErrorMessage(msg, "Analogy");
                            error.Source = nameof(FileProcessor);
                            error.Module = "Analogy";
                            DataWindow.AppendMessage(error, fileDataProvider.GetType().FullName);
                            return(new List <AnalogyLogMessage> {
                                error
                            });
                        }
                        CleanupManager.Instance.AddFolder(extractedPath);
                        var files = Directory.GetFiles(extractedPath);
                        files.ForEach(async file =>
                        {
                            var messages = await Process(fileDataProvider, file, token, isReload);
                            compressedMessages.AddRange(messages);
                        });
                        return(compressedMessages);
                    }
                    else
                    {
                        string msg = $"File is not supported by {fileDataProvider}. File: {filename}";
                        AnalogyLogger.Instance.LogCritical("Analogy", msg);
                        AnalogyLogMessage error = new AnalogyErrorMessage(msg, "Analogy");
                        error.Source = nameof(FileProcessor);
                        error.Module = "Analogy";
                        DataWindow.AppendMessage(error, fileDataProvider.GetType().FullName);
                        return(new List <AnalogyLogMessage> {
                            error
                        });
                    }
                }
            }
            catch (Exception e)
            {
                AnalogyLogger.Instance.LogCritical("Analogy", $"Error parsing file: {e}");
                var error = new AnalogyErrorMessage($"Error reading file {filename}: Error: {e.Message}", "Analogy");
                error.Source = nameof(FileProcessor);
                error.Module = "Analogy";
                DataWindow.AppendMessage(error, fileDataProvider.GetType().FullName);
                return(new List <AnalogyLogMessage> {
                    error
                });
            }
        }
Esempio n. 2
0
        private void UnzipZipFileIntoTempFolder(string zipPath, string extractPath, IAnalogyOfflineDataProvider fileDataProvider)
        {
            using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
                {
                    //build a list of files to be extracted
                    var entries = archive.Entries.Where(entry => !entry.FullName.EndsWith("/") && fileDataProvider.CanOpenFile(entry.FullName));
                    foreach (ZipArchiveEntry entry in entries)
                    {
                        entry.ExtractToFile(Path.Combine(extractPath, entry.Name));
                    }

                    if (!Directory.GetFiles(extractPath).Any())
                    {
                        AnalogyLogger.Instance.LogError(nameof(UnzipFilesIntoTempFolder),
                                                        "Zip file does not contain any supported files");
                    }
                }
            }
        }