public void GetPartialResourcesXmlTest() { resourceDocument = new XmlDocument(); resourceDocument.Load(@"c:\temp\resources_.20191122-182038.527\Resources.xml"); XmlDocument partialDocument = XmlSplitUtility.GetPartialResourcesXml(resourceDocument, fileIds); partialDocument.Save(@"c:\temp\resources_.20191122-182038.527\Resources_partial.xml"); }
internal IEnumerable <string> UploadResourceFilesToAzureFileStorage(List <Entity> resources, XDocument importXml, Configuration config, string folderDateTime, inRiverContext context) { Stopwatch saveFileStopWatch = new Stopwatch(); List <string> cloudFileNames = new List <string>(); try { // validate configuration settings if (string.IsNullOrEmpty(config.StorageAccountName) || string.IsNullOrEmpty(config.StorageAccountKey) || string.IsNullOrEmpty(config.StorageAccountShareReference) || string.IsNullOrEmpty(config.StorageAccountCatalogDirectoryReference) || string.IsNullOrEmpty(config.StorageAccountResourcesDirectoryReference)) { context.Log(LogLevel.Warning, $"Azure config settings are invalid: " + $"StorageAccountName: {config.StorageAccountName}, " + $"StorageAccountKey: {config.StorageAccountKey}, " + $"StorageAccountShareReference: {config.StorageAccountShareReference}, " + $"StorageAccountCatalogDirectoryReference: {config.StorageAccountCatalogDirectoryReference}, " + $"StorageAccountResourcesDirectoryReference: {config.StorageAccountResourcesDirectoryReference}"); return(cloudFileNames); } // validate resources argument if (resources == null) { context.Log(LogLevel.Error, "Resource is null!"); return(cloudFileNames); } // create varible files to hold filename and binary Dictionary <string, byte[]> files = new Dictionary <string, byte[]>(); // setup credentials and storage account StorageCredentials credentials = new StorageCredentials(config.StorageAccountName, config.StorageAccountKey); CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true); // setup file client and remoge share CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare share = fileClient.GetShareReference(config.StorageAccountShareReference); share.CreateIfNotExists(); // setup root directory and resource directory CloudFileDirectory root = share.GetRootDirectoryReference(); CloudFileDirectory directory = root.GetDirectoryReference(config.GetAzureStorageDirectoryName(XmlDocumentType.Resources)); directory.CreateIfNotExists(); saveFileStopWatch.Start(); // setup resource helper object Resources resourcesObj = new Resources(context); // setup the uncompressed file size total counter int totalFileSize = 0; // setup the azure file counter int fileCount = 0; // setup the file id list List <int> fileIdList = new List <int>(); foreach (Entity resource in resources) { // get the file id of the resource int resourceFileId = resourcesObj.GetResourceFileId(resource); // ensure the resource id has a proper id if (resourceFileId < 0) { context.Log(LogLevel.Information, $"Resource with id:{resource.Id} has no value for ResourceFileId"); continue; } // loop through each display configurations foreach (string displayConfiguration in resourcesObj.GetDisplayConfigurations(resource, config)) { // setup the fileName to use the output file extension from display configuration string fileName = resourcesObj.GetResourceFileName(resource, resourceFileId, displayConfiguration, config); // if the file for some reason already exists, continue to the next display configuration if (files.ContainsKey(fileName)) { context.Log(LogLevel.Debug, $"{fileName} already exists in the files collection and is skipped"); continue; } // get file bytes byte[] resourceData = context.ExtensionManager.UtilityService.GetFile(resourceFileId, displayConfiguration); // make sure we recieved the file from the utility service if (resourceData == null) { context.Log(LogLevel.Error, $"Resource with id:{resource.Id} and ResourceFileId: {resourceFileId} could not get file"); continue; } // add the current resource file id to the list fileIdList.Add(resource.Id); // log the resource file name context.Log(LogLevel.Debug, $"Adding resource {displayConfiguration}/{fileName}"); // add the file to the files collection files.Add($"{displayConfiguration}/{fileName}", resourceData); // add size to total file size counter totalFileSize += resourceData.Length; } if (totalFileSize > (config.TotalResourceSizeLimitMb * 1024 * 1024)) { try { // increase file counter fileCount++; // setup remote zip file CloudFile cloudFile = directory.GetFileReference(GetZipFileName(config, XmlDocumentType.Resources, folderDateTime, fileCount)); // setup reader using (XmlReader reader = importXml.CreateReader()) { // create a new file that only contains the elements specified in the file id list XmlDocument doc = XmlSplitUtility.GetPartialResourcesXml(reader, fileIdList); // log the resource file name context.Log(LogLevel.Debug, "Adding Resources.xml"); // setup memory a stream using (MemoryStream stream = new MemoryStream()) { // create a xml writer to format output using (XmlWriter writer = XmlWriter.Create(stream, new XmlWriterSettings { Indent = true })) { // save partial document to the xml writer doc.Save(writer); // add the partial document to the files collection files.Add("Resources.xml", stream.ToArray()); } } // send the zipped file to Azure and store the file name cloudFileNames.Add(ZipAndUploadToCloud(config, context, files, directory, cloudFile)); // clear the id list fileIdList.Clear(); // clear the file list files.Clear(); // reset total file size totalFileSize = 0; } } catch (Exception ex) { context.Log(LogLevel.Error, "An error occured while sending the resources to the cloud.", ex); } } } // make sure to send the final files if (files.Any()) { try { // increase file counter fileCount++; // setup remote zip file CloudFile cloudFile = directory.GetFileReference(GetZipFileName(config, XmlDocumentType.Resources, folderDateTime, fileCount)); // setup reader using (XmlReader reader = importXml.CreateReader()) { // create a new file that only contains the elements specified in the file id list XmlDocument doc = XmlSplitUtility.GetPartialResourcesXml(reader, fileIdList); // log the resource file name context.Log(LogLevel.Debug, "Adding Resources.xml"); // setup memory a stream using (MemoryStream stream = new MemoryStream()) { // create a xml writer to format output using (XmlWriter writer = XmlWriter.Create(stream, new XmlWriterSettings { Indent = true })) { // save partial document to the xml writer doc.Save(writer); // add the partial document to the files collection files.Add("Resources.xml", stream.ToArray()); } } // send the zipped file to Azure and store the file name cloudFileNames.Add(ZipAndUploadToCloud(config, context, files, directory, cloudFile)); } } catch (Exception ex) { context.Log(LogLevel.Error, "An error occured while sending the resources to the cloud.", ex); } } } catch (Exception ex) { context.Log(LogLevel.Error, "An error occured while sending the resources to the cloud.", ex); } return(cloudFileNames); }