1

I'm wanting to clean-up the media library, I have a script that outputs unused media library IDs. I then want to grab the media item associated with these IDs and download them to a temporary folder (i.e. SitecoreTempFolder). I've tried Send-File and Out-Download but these both store items to the local download folder which is not viable.

I'm just after the actual media item/file and not the Sitecore item. I have also attempted to use the move-item/copy-item but these cannot transfer between Sitecore and local/temp folders.

Many thanks in advance for any advice!

1 Answer 1

1

You can extend a Download script (/sitecore/system/Modules/PowerShell/Script Library/SPE/Maintenance/Media Library Maintenance/Content Editor/Context Menu/Download) which is provided out of the box and store your file under SitecoreDataFolder (/App_Data).

So you can create a zip file for all the media items and store them in SitecoreDataFolder. Comment out the download and remove the zip code at the end of the script. So it will stay there in /App_Data folder.

function ZipItems( $zipArchive, $sourcedir )
{
  Set-Location $sourcedir
  [System.Reflection.Assembly]::Load("WindowsBase,Version=3.0.0.0, `
      Culture=neutral, PublicKeyToken=31bf3856ad364e35") > $null
  $ZipPackage=[System.IO.Packaging.ZipPackage]::Open($zipArchive, `
      [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite)
  $items = @(Get-Item $sourceDir) + (Get-ChildItem -recurse $sourceDir)
  [byte[]]$buff = new-object byte[] 40960
  $i = 0;
  ForEach ($item In $items) {
    $i++
    if([Sitecore.Resources.Media.MediaManager]::HasMediaContent($item)){
      $mediaItem = New-Object "Sitecore.Data.Items.MediaItem" $item;
      $mediaStream = $mediaItem.GetMediaStream();
      $fileName = Resolve-Path -Path $item.ProviderPath -Relative
      $fileName = "$fileName.$($item.Extension)".Replace("\","/").Replace("./","/");
      "Added: $fileName"
      Write-Progress -Activity "Zipping Files " -CurrentOperation "Adding $fileName" -Status "$i out of $($items.Length)" -PercentComplete ($i *100 / $items.Length)
      $partUri = New-Object System.Uri($fileName, [System.UriKind]::Relative)
      $partUri = [System.IO.Packaging.PackUriHelper]::CreatePartUri($partUri);
      $part=$ZipPackage.CreatePart($partUri, "application/zip", [System.IO.Packaging.CompressionOption]::Maximum)
      $stream=$part.GetStream();
      do {
        $count = $mediaStream.Read($buff, 0, $buff.Length)
        $stream.Write($buff, 0, $count)
      } while ($count -gt 0)
      $stream.Close()
      $mediaStream.Close()
    }
  }
  $ZipPackage.Close()
}

$location = get-location
$time = Get-Date -format "yyyy-MM-d_hhmmss"
$zipName = Split-Path -leaf $location | % { $_ -replace " ", ""}
$zipPath = "$($SitecoreDataFolder)\$zipName-$time.zip"

ZipItems $zipPath $location
Download-File -FullName $zipPath > $null
Remove-Item $zipPath
Close-Window

Refer this article that provide more context: https://www.nehemiahj.com/2022/05/download-multiple-media-files-from.html

Hope it helps!

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.