Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SPE commands to create and update media items #1064

Closed
michaellwest opened this issue Oct 25, 2018 · 3 comments
Closed

Add SPE commands to create and update media items #1064

michaellwest opened this issue Oct 25, 2018 · 3 comments
Labels
stale The issue has become stale due to lack of activity and was automatically or manually closed.

Comments

@michaellwest
Copy link
Member

There are a number of solutions developers have come up with to handle this. Would be nice to include commands out of the box.

Reference

function New-MediaItem{
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$filePath,

        [Parameter(Position=1, Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$mediaPath
    )

    $mco = New-Object Sitecore.Resources.Media.MediaCreatorOptions
    $mco.Database = [Sitecore.Configuration.Factory]::GetDatabase("master");
    $mco.Language = [Sitecore.Globalization.Language]::Parse("en");
    $mco.Versioned = [Sitecore.Configuration.Settings+Media]::UploadAsVersionableByDefault;
    $mco.Destination = "$($mediaPath)/$([System.IO.Path]::GetFileNameWithoutExtension($filePath))";

    $mc = New-Object Sitecore.Resources.Media.MediaCreator
    $mc.CreateFromFile($filepath, $mco);
}

# Usage example
New-MediaItem "C:\Users\Adam\Desktop\Accelerators.jpg" "$([Sitecore.Constants]::MediaLibraryPath)/Images"
function Add-MediaItem {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,
        
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Extension,
        
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [byte[]]$Content
    )
    
    $mco = New-Object Sitecore.Resources.Media.MediaCreatorOptions
    $mco.Database = Get-Database -Name "master"
    $mco.KeepExisting = $false
    $mco.Versioned = [Sitecore.Configuration.Settings+Media]::UploadAsVersionableByDefault
    $mco.Destination = $Path

    $mc = New-Object Sitecore.Resources.Media.MediaCreator
    New-UsingBlock ($stream = New-Object System.IO.MemoryStream) {
        $stream.Write($Content, 0, $Content.Length)
        $mc.CreateFromStream($stream, ($Path + "." + $Extension), $mco)
    }
}
function Update-MediaItem{
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$filePath,

        [Parameter(Position=1, Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$mediaPath)

    [Sitecore.Data.Items.MediaItem]$item = gi $mediaPath
    [Sitecore.Resources.Media.Media] $media = [Sitecore.Resources.Media.MediaManager]::GetMedia($item);
    $extension = [System.IO.Path]::GetExtension($filePath);
    $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $filePath, "Open", "Read"
    $media.SetStream($stream, $extension);
    $stream.Close();
}

# Usage example - overwrite the image created with previous cmdlet with new image
Update-MediaItem "$SitecoreDataFolder\Tchotchkeys.jpg" "$([Sitecore.Constants]::MediaLibraryPath)/Images/Accelerators"
@michaellwest michaellwest changed the title Add native SPE commands to create and update media items Add SPE commands to create and update media items Oct 25, 2018
@michaellwest
Copy link
Member Author

Should probably keep these as functions to avoid breaking functionality for others.

Copy link

github-actions bot commented Jun 1, 2024

This issue is stale because it has been open for 365 days with no activity.

@github-actions github-actions bot added the stale The issue has become stale due to lack of activity and was automatically or manually closed. label Jun 1, 2024
Copy link

github-actions bot commented Jul 1, 2024

This issue was closed because it has been inactive for 30 days since being marked as stale.

@github-actions github-actions bot closed this as completed Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale The issue has become stale due to lack of activity and was automatically or manually closed.
Projects
None yet
Development

No branches or pull requests

1 participant