Copied to clipboard

Flag this post as spam?

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


  • Remi Martel 32 posts 104 karma points
    Mar 15, 2018 @ 20:00
    Remi Martel
    0

    Media Event Saving is not before the actual save

    I made a mediaservice.Saving event that will check on already existing media item if the image's width and the new image's width are the same. If not, the save will be rejected and the original image will be kept. The problem is that as soon as the saving event is reached, my old image get replaced in the media folder and I can't go back. This is my code:

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            //when saving something in MEDIA section - before save
            MediaService.Saving += MediaServiceSaving;
        }
    
        //When saving an image that already existed, this event will check if the image has the same aspect ratio as the previous image and allow replacing the image
        void MediaServiceSaving(IMediaService sender, SaveEventArgs<IMedia> e)
        {
            foreach (var page in e.SavedEntities)
            {
                //getting the image id
                string id = page.Id + "";
                //Checking media item is not new
                if (id != "0")
                {
                    //Querying for image location
                    string str = "";
                    var db = ApplicationContext.Current.DatabaseContext.Database;
                    var result = db.Query<string>("SELECT mediaPath FROM cmsMedia WHERE nodeId = " + id + ";");
                    foreach (var row in result)
                        str = row;
                    //extracting the id from the path
                    id = str.Substring(7, 4);
                    //getting the name of the images + extension
                    string name = str.Substring(str.LastIndexOf('/') + 1);
                    //Path to folder containing original image
                    string path = HttpContext.Current.Server.MapPath("~/Media");
                    string complete = path + "/" + id + "/" + name;
    
                    //check if image needs to be resized by checking if size folders exists
                    if (Directory.Exists(path + "/" + id + "/tile/") || Directory.Exists(path + "/" + id + "/carousel/") || Directory.Exists(path + "/" + id + "/product/") ||
                        Directory.Exists(path + "/" + id + "/mobileTile/") || Directory.Exists(path + "/" + id + "/mobileProduct/"))
                    {
                        //Check if new image has acceptable aspect ratio
                        //getting previous image's aspect ratio
                        Bitmap bitmap1 = new Bitmap(complete);
                        double initWidth = bitmap1.Width;
                        double initHeight = bitmap1.Height;
                        double initRatio = (initWidth / initHeight);
    
                        //getting new image's aspect ratio
                        double height = 0;
                        double width = 0;
                        foreach (var prop in page.Properties)
                        {
                            if (prop.Alias == "umbracoHeight")
                                height = Double.Parse(prop.Value.ToString());
                            if (prop.Alias == "umbracoWidth")
                                width = Double.Parse(prop.Value.ToString());
                        }
                        double ratio = width / height;
    
                        if (initRatio != ratio)
                        {
                            e.CancelOperation(new EventMessage("Error","Aspect ratio not respected. Choose another image", EventMessageType.Error));
                        }
                        else
                            e.Messages.Add(new EventMessage("Success", "Your new image has the same aspect ratio as the previous", EventMessageType.Success));
                    }
                }
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft