private void GenerateJsonObjectRecursive(JsonNode jsonNode, string filePath, NodeCollection nodeCollection, IEncryptionServices encryptionServices, string accumulatedPath, string pathSeparator) { foreach (KeyValuePair <string, Node> keyValuePair in nodeCollection) { if (keyValuePair.Value.Children.Count == 0) //is a file { string fullFilePath = $"{filePath}{accumulatedPath}{keyValuePair.Key}"; byte[] array = File.ReadAllBytes(fullFilePath); jsonNode.children.Add(new JsonNode( name: encryptionServices.EncryptToString(keyValuePair.Key), isFile: true, file: encryptionServices.EncryptToString(array))); } else // is a folder { var dirNode = new JsonNode(name: encryptionServices.EncryptToString(keyValuePair.Key)); jsonNode.children.Add(dirNode); GenerateJsonObjectRecursive(dirNode, filePath, keyValuePair.Value.Children, encryptionServices, $"{accumulatedPath}{keyValuePair.Key}{pathSeparator}", pathSeparator); } } }
public async Task <IActionResult> Post(IFormFile formFile, string user, string password) { if (formFile == null || string.IsNullOrEmpty(user) || string.IsNullOrEmpty(password)) { return(View(new PostModel { ErrorMessage = "Please provide a correct user, password and zip file", HasError = true })); } else { //ensure store directory _fileManagementServices.EnsureStoreDirectory(); //delete zip files directory and recreate _fileManagementServices.EnsureUnzipDirectory(); //zip storage FileManagementResult storedFile = await _fileManagementServices.StoreFilesAsync(formFile); //unzip file into zip directory _zipServices.UnzipFiles( storedFile, _fileManagementServices.GetUnzipPath(), _fileManagementServices.GetFileSeparator()); if (storedFile.IsStored) { NodeCollection nodes = _zipServices.GetFileAndFolderStructureAsync(storedFile.FileName); // this will generate the json object with all neccessary fields encrypted JsonNode root = nodes.GenerateJsonObject( _encryptionServices, _fileManagementServices.GetUnzipPath(), _fileManagementServices.GetFileSeparator()); // then generate the json string string jsonString = JsonConvert.SerializeObject(root); // call the DataManagement System API var response = await _dataManagementSystemCallerServices.PostAsync( _fileManagementServiceUrl, jsonString, _encryptionServices.EncryptToString(user), _encryptionServices.EncryptToString(password)); // visualized the sent json string if (!response) { return(View(new PostModel { ErrorMessage = "The Data Management system answer was negative", HasError = true })); } return(View(new PostModel { JsonString = jsonString, AnswerComment = "Json accepted by Data Management Service", HasError = false })); } else { return(View(new PostModel { ErrorMessage = "Error Storing zip file", HasError = true })); } } }