Updating Media Library Files

Jon Tetzlaff asked on June 9, 2016 19:29

I need to update an existing media library file after I run it through an image optimizer. I can't find any API methods that let me update the existing file, without just creating a new one. I can't just write an image to the disk because I'm using azure blog storage, so the files aren't stored locally. Does anyone know any methods to update a MediaFileInfo file using the Kentico API?

EDIT: To be clear I am looking for how to update the associated file of a MediaFileInfo object.

Thanks,
Jon

Recent Answers


Jan Hermann answered on June 10, 2016 07:48

// Gets the media library
MediaLibraryInfo library = MediaLibraryInfoProvider.GetMediaLibraryInfo("NewLibrary", SiteContext.CurrentSiteName);

if (library != null)
{
    // Gets the media file
    MediaFileInfo updateFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, "NewFolder/Image.png");

    if (updateFile != null)
    {
        // Updates the media library file properties
        updateFile.FileDescription = updateFile.FileDescription.ToLower();

        // Saves the media library file
        MediaFileInfoProvider.SetMediaFileInfo(updateFile);
    }
}
0 votesVote for this answer Mark as a Correct answer

Jon Tetzlaff answered on June 10, 2016 14:20

@Jan if you look at my question I'm not looking at how to update the FileDescription of a MediaFileInfo object, but how to update the associated file of a MediaFileInfo object

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on June 10, 2016 15:45

It's simply similiar to creating new one:

// Prepares a path to a local file
string filePath = @"C:\Files\images\Image.png";

// Prepares a CMS.IO.FileInfo object representing the local file
CMS.IO.FileInfo file = CMS.IO.FileInfo.New(filePath);

if (file != null)
{
    // Creates a new media library file object
    MediaFileInfo mediaFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, "NewFolder/Image.png");  

    // Sets the media library file properties
    mediaFile.FileName = "Image";
    mediaFile.FileTitle = "File title";
    mediaFile.FileDescription = "This file was added through the API.";
    mediaFile.FilePath = "NewFolder/Image"; // Sets the path within the media library's folder structure
    mediaFile.FileExtension = file.Extension;
    mediaFile.FileMimeType = MimeTypeHelper.GetMimetype(file.Extension);
    mediaFile.FileSiteID = SiteContext.CurrentSiteID;
    mediaFile.FileLibraryID = library.LibraryID;
    mediaFile.FileSize = file.Length;

    // Saves the media library file
    MediaFileInfoProvider.SetMediaFileInfo(mediaFile);
}

You got more examples in the documentation https://docs.kentico.com/display/API9/Media+libraries#Medialibraries-Updatingamedialibraryfile

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.