Ever wondered how can we read contents of files uploaded to media library without rendering them? Recently, we needed a PowerShell script to read the content of our SVGs and create a list of SVGs which contained a pattern in them. After a little research, I came up with this script -
$item =Get-Item -Path "/sitecore/media library/<filepath here>"
$mediaItem = New-Object 'Sitecore.Data.Items.MediaItem' $item;
$blobField = $mediaItem.InnerItem.Fields["blob"]
if($blobField){
$blobStream = $blobField.GetBlobStream()
if($blobStream){
try
{
$contents = New-Object byte[] $blobStream.Length
$blobStream.Read($contents, 0, $blobStream.Length) | Out-Null
}
finally
{
$blobStream.Close()
}
$fileText = ([System.Text.Encoding]::Default.GetString($contents))
}
}
In the above script, $fileText will contain the contents of the file.
Note: If a file's content is correctly rendered in notepad++ as in snapshot below, then you will be able to read and process the contents using this script.
And if a file's content is not rendered correctly in notepad++, then you may need additional/alternate logics to convert the byte[] read from SqlServerStream returned by blob field that contains media as attachment into a readable format. This script shall be a good starting point in such cases to fetch the SqlServerStream from the blobfield.
Hope it helps you! Thanks for reading!!
Comments
Post a Comment