Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • James Jackson-South 489 posts 1743 karma points c-trib
    Jun 06, 2015 @ 13:46
    James Jackson-South
    0

    Updating media properties on upload.

    I'm intercepting image uploads to resize images to a maximum size. Easy-peasy except...

    I can't seem to get the IMedia properties to update.

    // Tap into the Saving event
    MediaService.Saving += (sender, args) =>
    {
        // Resize code removed for brevity. It works.
    
        // Set the correct meta values for the image.
        media.SetValue(Constants.Conventions.Media.Width, newWidth);
        media.SetValue(Constants.Conventions.Media.Height, newHeight);
        media.SetValue(Constants.Conventions.Media.Bytes, bytes);
        sender.Save(media, 0, false);
    }
    

    Am I saving those values at the wrong time?

  • Sebastiaan Janssen 4899 posts 14655 karma points MVP admin hq
    Jun 06, 2015 @ 16:01
    Sebastiaan Janssen
    0

    I hope your not doing GetById to get the media item else your writing to it before it's saving so it will then not save anything.

    In other words: show your var media = ... something ? :)

  • James Jackson-South 489 posts 1743 karma points c-trib
    Jun 06, 2015 @ 16:16
    James Jackson-South
    0

    Nah... I'm looping through the args.SavedEntities enumerable. Basically an enhanced version of the preprocessing example here. http://24days.in/umbraco/2014/all-your-images-are-belong-to-umbraco/

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jun 06, 2015 @ 23:11
    Morten Bock
    0

    Could it be that your "saving" event happens before the core code sets the same properties?

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jun 06, 2015 @ 23:34
    Morten Bock
    100

    EDIT: My sample was a false positive! Refreshing the media revealed that it was in fact not saved, just modified for display.

    Fixed sample:

    MediaService.Saved += (sender, e) =>
    {
        foreach (IMedia entity in e.SavedEntities)
        {
            entity.SetValue(Constants.Conventions.Media.Width, 8878);
            sender.Save(entity, 0, false);
        }
    };
    

    The above seems to work.

  • James Jackson-South 489 posts 1743 karma points c-trib
    Jun 07, 2015 @ 15:29
    James Jackson-South
    0

    Yeah that get's it Morten thanks. I thought it was probably a timing issue.

    Morten Christensen reckons this is the probable cause of my issue.

    https://github.com/umbraco/Umbraco-CMS/blob/07e9a3a4ea27e167816e2cce8cea1b89258b01a8/src/Umbraco.Web/Strategies/DataTypes/LegacyUploadFieldWorkaround.cs

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies