public void GenerateMetadata(string filePath, bool addNewFiles, bool removeMissingFiles) { // Deserialize existing metadata var listMetadataFiles = Directory.GetFiles(filePath, $"*.{ListMetaDataExtension}"); var listsMetadata = listMetadataFiles.Select(ListMetadata.DeserializeFromFile).ToList(); // Get all files in the folder var files = Directory.GetFiles(filePath, $"*.{ListExtension}"); if (addNewFiles || listsMetadata.Count == 0) { // Get list file names without a matching metadata file var newFiles = files.Where( file => listsMetadata.All( listInfo => Path.GetFileName(listInfo.Location.ToLower()) != Path.GetFileName(file.ToLower()))) .ToList(); // Generate any new files foreach (var newFile in newFiles) { var listMetadata = new ListMetadata { ListId = Guid.NewGuid().ToString(), Name = Path.GetFileNameWithoutExtension(newFile), Description = Path.GetFileNameWithoutExtension(newFile), Location = Path.GetFileName(newFile), Type = "rule", Separator = "|", UpdatedBy = "MaritzCX", LastUpdated = DateTime.UtcNow }; listMetadata.SerializeToFile(Path.ChangeExtension(newFile, ListMetaDataExtension)); } } if (!removeMissingFiles) { return; } // Get metadata files without a matching list file var missingFiles = listsMetadata.Where( listMetadata => files.Any(file => Path.GetFileName(file.ToLower()) != listMetadata.Location.ToLower())) .ToList(); // Remove missing files foreach (var missingFile in missingFiles) { File.Delete(Path.Combine(filePath, Path.ChangeExtension(missingFile.Location, ListMetaDataExtension))); } }
public override void SaveList(List list) { var existingListMetadata = new ListMetadata(); var existingMetadataFile = string.Empty; var tenantFolder = Directory.EnumerateDirectories(ListFolder).FirstOrDefault(path => path.EndsWith(list.TenantId)); if (tenantFolder == null) { tenantFolder = Path.Combine(ListFolder, list.TenantId); Directory.CreateDirectory(tenantFolder); } else { var listMetadataFiles = Directory.GetFiles(tenantFolder, $"*.{ListMetaDataExtension}"); foreach (var file in listMetadataFiles) { var metadata = ListMetadata.DeserializeFromFile(file); if (metadata.ListId != list.ListId) { continue; } existingListMetadata = metadata; existingMetadataFile = file; break; } } var invalidFilenameCharacters = new Regex("[\\\\/:*?\"<>|]"); var newListLoaded = list.FileData.Length > 0; var existingMetadata = existingListMetadata.ListId != null; var newMetaData = existingListMetadata.Name != list.Name || existingListMetadata.Description != list.Description || existingListMetadata.Separator != list.Separator || existingListMetadata.Location != $"{invalidFilenameCharacters.Replace(list.Name, "")}_{list.ListId}.{ListExtension}"; var listMetadata = new ListMetadata { ListId = list.ListId, Name = list.Name, Description = list.Description, Location = newListLoaded ? $"{invalidFilenameCharacters.Replace(list.Name, "")}_{list.ListId}.{ListExtension}" : existingListMetadata.Location, Type = TranslateMetaType(list.Function), Separator = list.Separator, UpdatedBy = "MaritzCX", LastUpdated = DateTime.UtcNow }; if (newListLoaded) { if (existingMetadata) { File.Delete(Path.Combine(tenantFolder, existingListMetadata.Location)); } File.WriteAllBytes(Path.Combine(tenantFolder, listMetadata.Location ?? $"missing_{listMetadata.ListId}"), list.FileData); } if (newMetaData) { if (existingMetadata) { File.Delete(existingMetadataFile); } listMetadata.SerializeToFile(Path.Combine(tenantFolder, $"{invalidFilenameCharacters.Replace(list.Name, "")}_{list.ListId}.{ListMetaDataExtension}")); } }