Skip to content

Commit

Permalink
Adaptation in GetIshDocumentObjData to work with big Xml objects
Browse files Browse the repository at this point in the history
Loading big xml into XmlDocument object(e.g. when downloading big image) could lead to OutOfMemory exception
  • Loading branch information
OlegTokar committed Feb 6, 2019
1 parent 1b2d0d1 commit 9b0cd1f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,52 @@ protected override void ProcessRecord()
// Get language ref
long lngRef = Convert.ToInt64(ishObject.ObjectRef[Enumerations.ReferenceType.Lng]);
long[] lngRefsArray = new long[1] { lngRef };
var dataObjectResponse = IshSession.DocumentObj25.RetrieveObjectsByIshLngRefs(lngRefsArray, productDefinitionFeatures.ToXml(), "");
XmlDocument xmlIshDataObject = new XmlDocument();
xmlIshDataObject.LoadXml(dataObjectResponse);
XmlElement ishDataObjectElement = (XmlElement)xmlIshDataObject.SelectSingleNode("ishobjects/ishobject/ishdata");
IshData ishData = new IshData(ishDataObjectElement);

if (FolderPath != null)
using (var stringReader = new StringReader(IshSession.DocumentObj25.RetrieveObjectsByIshLngRefs(lngRefsArray, productDefinitionFeatures.ToXml(), "")))
{
string tempLocation = Directory.CreateDirectory(FolderPath).FullName;
WriteDebug($"Writing lngRef[{lngRef}] to [{tempLocation}] {++current}/{ishObjects.Length}");
//Create the file.
string tempFilePath = FileNameHelper.GetDefaultObjectFileName(tempLocation, ishObject, ishData.FileExtension);
using (FileStream fs = File.Create(tempFilePath))
byte[] bytearray = null;
string edt = "";
string fileExtension = "";

using (XmlTextReader xmlTextReader = new XmlTextReader(stringReader))
{
while (xmlTextReader.Read())
{
if (xmlTextReader.NodeType == XmlNodeType.Element && xmlTextReader.Name == "ishdata")
{
edt = xmlTextReader.GetAttribute("edt");
fileExtension = xmlTextReader.GetAttribute("fileextension");
}

if (xmlTextReader.NodeType == XmlNodeType.CDATA)
{
bytearray = System.Convert.FromBase64String(xmlTextReader.Value);
}
}
}
IshData ishData = new IshData(edt, fileExtension, bytearray);

if (FolderPath != null)
{
string tempLocation = Directory.CreateDirectory(FolderPath).FullName;
WriteDebug($"Writing lngRef[{lngRef}] to [{tempLocation}] {++current}/{ishObjects.Length}");
//Create the file.
string tempFilePath = FileNameHelper.GetDefaultObjectFileName(tempLocation, ishObject, ishData.FileExtension);
using (FileStream fs = File.Create(tempFilePath))
{
fs.Write(ishData.ByteArray, 0, ishData.Size());
}
// Append file info list
fileInfo.Add(new FileInfo(tempFilePath));
WriteObject(fileInfo, true);
}
else
{
fs.Write(ishData.ByteArray, 0, ishData.Size());
WriteDebug($"Enriching ishObject[{ishObject.ObjectRef[Enumerations.ReferenceType.Lng]}] with IshData {++current}/{IshObject.Length}");
ishObject.IshData = ishData;
WriteObject(ishObject, true);
}
// Append file info list
fileInfo.Add(new FileInfo(tempFilePath));
WriteObject(fileInfo, true);
}
else
{
WriteDebug($"Enriching ishObject[{ishObject.ObjectRef[Enumerations.ReferenceType.Lng]}] with IshData {++current}/{IshObject.Length}");
ishObject.IshData = ishData;
WriteObject(ishObject, true);
}
}
WriteVerbose("returned file count[" + current + "]");
}
Expand Down
15 changes: 14 additions & 1 deletion Source/ISHRemote/Trisoft.ISHRemote/Objects/Public/IshData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,20 @@ public IshData(string edt, byte[] fileContent)
_byteArray = fileContent;
_fileExtension = "";
}


/// <summary>
/// Creates a new instance of the <see cref="IshData"/> class.
/// </summary>
/// <param name="edt">The EDT.</param>
/// <param name="fileExtension">File extension</param>
/// <param name="fileContent">Byte array with the file content.</param>
public IshData(string edt, string fileExtension, byte[] fileContent)
{
_edt = (edt == null) ? DefaultEDT : edt;
_byteArray = fileContent;
_fileExtension = fileExtension;
}

/// <summary>
/// Initializing by something that looks like <ishdata edt="EDTXML"><![CDATA[PFhNTEZJTEU+UHJvamVjdE1hbmFnZW1lbnQ/PC9YTUxGSUxFPg0K]]></ishdata>
/// </summary>
Expand Down

0 comments on commit 9b0cd1f

Please sign in to comment.