Creating Media through usercontrol Options
Ethan
Posted: Monday, August 18, 2008 5:51:37 PM
Rank: Newbie

Joined: 8/18/2008
Posts: 10
Location: Iowa
The idea is pretty simple:

I want my editors to be able to log into umbraco and create new calendar event items which they can then hyperlink to later.

This Calendar Event is just a form that I want my editors to fill out. When they save the form, it will create the icalendar file as media.

This needs to be done in umbraco's back end.

Using this tutorial I have managed to create a usercontrol with the form that I want and I have been able to create the icalendar file just fine. I just don't know of any way that I can store the URL to this file that I've created so that I can reference it later.

Is there any more comprehensive tutorial or document regarding user controls and creating custom datatypes? I've been unsuccessful in finding anything useful. The existing forum topics are nice but they're just snippets of an implementation. That's like showing me how to put ketchup on the burger instead of how to fully cook the burger.

I've downloaded the umbraco source and I could easily duplicate the core datatypes, but for some annoying reason (no offense) the user controls look like they have to implement different interfaces. I'd rather not hack this in. Cleanliness is important.

Any help would be appreciated.

Thanks.

psterling@homax
Posted: Monday, August 18, 2008 6:36:38 PM

Rank: Fanatic

Joined: 10/30/2007
Posts: 215
Location: Bellingham
Ethan -

Have a look at this thread - there's a full C# snippet of creating a media node, thumbnail (if needed) and setting properties.

-Paul

motusconnect.com :: level-2 certified :: MVP 2008/2009
Ethan
Posted: Monday, August 18, 2008 6:57:00 PM
Rank: Newbie

Joined: 8/18/2008
Posts: 10
Location: Iowa
I looked at that article earlier actually.

My question about that code is that somewhere this media object "m" is created or passed in.

"m.Id.ToString() "

Where is that defined?

-Ethan

psterling@homax
Posted: Monday, August 18, 2008 11:26:15 PM

Rank: Fanatic

Joined: 10/30/2007
Posts: 215
Location: Bellingham
Ethan -

Sorry, thought you were past that already...it's created like this:

Code:

// creates an item in Media Root for the System User
// filename is the name of the file you're creating

umbraco.cms.businesslogic.media.Media m =
  umbraco.cms.businesslogic.media.Media.MakeNew(
  filename, umbraco.cms.businesslogic.media.MediaType.GetByAlias("image"),
  User.GetUser(0), -1);


There's a full API reference available here for future reference.

-Paul

motusconnect.com :: level-2 certified :: MVP 2008/2009
psterling@homax
Posted: Monday, August 18, 2008 11:34:05 PM

Rank: Fanatic

Joined: 10/30/2007
Posts: 215
Location: Bellingham
Ethan -

Oh heck...seems like you're looking for an "all in one" sample. So here you go. The following creates an Umbraco Media Node (an image) in the Media Root along with the thumbnail.

Hope this helps!

Code:
        protected string UmbracoSave(FileUpload uploadControl)
        {
            string mediaPath = "";

            if (uploadControl.PostedFile != null)
            {
                if (uploadControl.PostedFile.FileName != "")
                {
                    // Find filename
                    _text = uploadControl.PostedFile.FileName;
                    string filename;
                    string _fullFilePath;

                    filename = _text.Substring(_text.LastIndexOf("\\") + 1, _text.Length - _text.LastIndexOf("\\") - 1).ToLower();

                    // create the Media Node
                    // TODO:  get parent id for current category - as selected by user (see below)
                    // - for now just stick these in the media root :: node = -1
                    umbraco.cms.businesslogic.media.Media m = umbraco.cms.businesslogic.media.Media.MakeNew(
                        filename, umbraco.cms.businesslogic.media.MediaType.GetByAlias("image"), User.GetUser(0), -1);

                    // Create a new folder in the /media folder with the name /media/propertyid
                    System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + m.Id.ToString()));
                    _fullFilePath = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + m.Id.ToString() + "/" + filename);
                    uploadControl.PostedFile.SaveAs(_fullFilePath);

                    // Save extension
                    string orgExt = ((string)_text.Substring(_text.LastIndexOf(".") + 1, _text.Length - _text.LastIndexOf(".") - 1));
                    orgExt = orgExt.ToLower();
                    string ext = orgExt.ToLower();
                    try
                    {
                        m.getProperty("umbracoExtension").Value = ext;
                    }
                    catch { }

                    // Save file size
                    try
                    {
                        System.IO.FileInfo fi = new FileInfo(_fullFilePath);
                        m.getProperty("umbracoBytes").Value = fi.Length.ToString();
                    }
                    catch { }

                    // Check if image and then get sizes, make thumb and update database
                    if (",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + ext + ",") > 0)
                    {
                        int fileWidth;
                        int fileHeight;

                        FileStream fs = new FileStream(_fullFilePath,
                            FileMode.Open, FileAccess.Read, FileShare.Read);

                        System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
                        fileWidth = image.Width;
                        fileHeight = image.Height;
                        fs.Close();
                        try
                        {
                            m.getProperty("umbracoWidth").Value = fileWidth.ToString();
                            m.getProperty("umbracoHeight").Value = fileHeight.ToString();
                        }
                        catch { }

                        // Generate thumbnails
                        string fileNameThumb = _fullFilePath.Replace("." + orgExt, "_thumb");
                        generateThumbnail(image, 100, fileWidth, fileHeight, _fullFilePath, ext, fileNameThumb + ".jpg");

                        image.Dispose();
                    }
                    mediaPath = "/media/" + m.Id.ToString() + "/" + filename;

                    m.getProperty("umbracoFile").Value = mediaPath;
                    m.XmlGenerate(new XmlDocument());

                    string commerceFileName = mediaPath;
                    CommerceSave(commerceFileName);
                }
            }
            return mediaPath;
        }

        protected void generateThumbnail(System.Drawing.Image image, int maxWidthHeight, int fileWidth, int fileHeight, string fullFilePath, string ext, string thumbnailFileName)
        {
            // Generate thumbnail
            float fx = (float)fileWidth / (float)maxWidthHeight;
            float fy = (float)fileHeight / (float)maxWidthHeight;
            // must fit in thumbnail size
            float f = Math.Max(fx, fy); //if (f < 1) f = 1;
            int widthTh = (int)Math.Round((float)fileWidth / f); int heightTh = (int)Math.Round((float)fileHeight / f);

            // fixes for empty width or height
            if (widthTh == 0)
                widthTh = 1;
            if (heightTh == 0)
                heightTh = 1;

            // Create new image with best quality settings
            Bitmap bp = new Bitmap(widthTh, heightTh);
            Graphics g = Graphics.FromImage(bp);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;

            // Copy the old image to the new and resized
            Rectangle rect = new Rectangle(0, 0, widthTh, heightTh);
            g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

            // Copy metadata
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo codec = null;
            for (int i = 0; i < codecs.Length; i++)
            {
                if (codecs[i].MimeType.Equals("image/jpeg"))
                    codec = codecs[i];
            }

            // Set compresion ratio to 90%
            EncoderParameters ep = new EncoderParameters();
            ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);

            // Save the new image
            bp.Save(thumbnailFileName, codec, ep);
            bp.Dispose();
            g.Dispose();

        }


motusconnect.com :: level-2 certified :: MVP 2008/2009
Ethan
Posted: Monday, August 18, 2008 11:36:13 PM
Rank: Newbie

Joined: 8/18/2008
Posts: 10
Location: Iowa
Ah so I have to create the new media. Hmm.. my original idea was the user to create the media, and then the media creates its own data. Just to do that I would have to get my own media id. Is there a way to get my own media id?

Thanks for the ref to the api. I seem to have terrible luck opening it. My browser keeps crashing lol. I'm sure I'll get to it eventually.
Ethan
Posted: Tuesday, August 19, 2008 11:22:19 PM
Rank: Newbie

Joined: 8/18/2008
Posts: 10
Location: Iowa
To answer my own question for future reference, here's how to get the ID of the content node that your custom user control is currently in:

int.Parse(Request.QueryString["id"])
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.