public void Add(ImportStats.FileStats value, bool sync = true) { Guard.NotNull(value, nameof(value)); if (value.Name.IsEmpty()) { return; } try { var oldStats = File.Exists(_path) ? XmlHelper.Deserialize <ImportStats>(File.ReadAllText(_path)) : new ImportStats(); var existingStats = oldStats.Files.FirstOrDefault(x => x.Name == value.Name); if (existingStats != null) { existingStats.Name = value.Name; existingStats.CategoryCount = value.CategoryCount; existingStats.ProductCount = value.ProductCount; } else { oldStats.Files.Add(value); } if (sync) { try { var newStats = new ImportStats(); var existingFiles = new ShopConnectorFileSystem(_dirName).GetAllFiles(); foreach (var stats in oldStats.Files) { if (existingFiles.Contains(stats.Name)) { newStats.Files.Add(stats); } } File.WriteAllText(_path, XmlHelper.Serialize(newStats)); } catch { File.WriteAllText(_path, XmlHelper.Serialize(oldStats)); } } else { File.WriteAllText(_path, XmlHelper.Serialize(oldStats)); } } catch (Exception ex) { ex.Dump(); } }
public ImportStats.FileStats Get(string fileName, bool estimateIfMissing = false) { ImportStats.FileStats stats = null; try { if (fileName.HasValue() && File.Exists(_path)) { var deserializedStats = XmlHelper.Deserialize <ImportStats>(File.ReadAllText(_path)); stats = deserializedStats.Files.FirstOrDefault(x => x.Name == fileName); } if (stats == null) { stats = new ImportStats.FileStats { Name = fileName }; } if (estimateIfMissing && (stats.CategoryCount == 0 || stats.ProductCount == 0)) { var importPath = new ShopConnectorFileSystem(_dirName).GetFullFilePath(fileName); if (File.Exists(importPath)) { if (stats.CategoryCount == 0) { stats.CategoryCount = 1000; } if (stats.ProductCount == 0) { stats.ProductCount = (int)((double)ShopConnectorFileSystem.GetFileSize(importPath) / (double)10000); } } } } catch (Exception ex) { ex.Dump(); } return(stats); }