Example #1
0
        public override void Saved(HttpRequest request, string actualPath, string vpath)
        {
            #region arguments

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            #endregion

            if (!SupportedImageTypes.IsSupported(vpath))
            {
                // image type is not supported. delete the file
                // and report failure.
                this.Success = false;
                this.DeleteUploaded = true;
            }
            else
            {

                logger.Log(LogLevel.Debug, "Supported file. Will add entry to media.");

                // call apps to work on the uploaded image
                AppEngine.ExecuteApps(AppEvent.ImageUploaded, actualPath);

                // add an entry to media
                Photo photo = new Photo();
                photo.Url = vpath;

                try
                {
                    // get the width and height of the photo
                    Image image = Image.FromFile(actualPath);

                    // get the width and height of the image
                    photo.Width = image.Width;
                    photo.Height = image.Height;
                    photo.Name = Path.GetFileName(actualPath);
                    photo.Owner = AuthenticationUtility.Instance.GetLoggedUser();

                    //save the media
                    BaseMedia media = MediaStore.Instance.Save(photo);

                    Dictionary<string, object> properties = new Dictionary<string, object>(1);
                    properties["Id"] = media.Id;
                    properties["name"] = media.Name;
                    properties["width"] = photo.Width;
                    properties["height"] = photo.Height;
                    this.Properties = properties;

                    this.Success = true;
                }
                catch(Exception exception)
                {
                    logger.Log(LogLevel.Error, "Unable to update the media information - {0}.", exception);

                    // failure mapping the image.
                    this.Success = false;
                    this.DeleteUploaded = true;
                }
            }
        }
Example #2
0
        public string Save(string url, string name, string tags, string mediaType)
        {
            // make sure the user has access
            RatnaUser user = base.ValidatedUser();
            if (!(IsAccessAllowed(user)))
            {
                return SendAccessDenied();
            }

            ServiceOutput output = new ServiceOutput();
            output.Success = false;

            if (!string.IsNullOrEmpty(url))
            {

                try
                {
                    BaseMedia media = null;

                    if (MediaStore.Instance.TryGetMedia(url, out media))
                    {
                        string mname = name ?? string.Empty;
                        if (!mname.Equals(media.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            media.Name = mname;
                        }

                        if (!string.IsNullOrEmpty(tags))
                        {
                            // update the tags
                            media.AddTags(tags);
                        }

                        media.Update();
                    }
                    else
                    {
                        // create a new media type
                        MediaType mType;

                        if (Enum.TryParse<MediaType>(mediaType, true, out mType))
                        {
                            switch (mType)
                            {
                                case MediaType.Photo:

                                    //try to get the width and height of the image
                                    Tuple<int, int> size = Jardalu.Ratna.Web.Utility.GetImageSize(url);

                                    Photo photo = new Photo();
                                    photo.Width = size.Item1;
                                    photo.Height = size.Item2;
                                    media = photo;

                                    output.AddOutput("width", photo.Width);
                                    output.AddOutput("height", photo.Height);
                                    break;
                                case MediaType.Video:
                                    media = new Video();
                                    break;
                                case MediaType.Document:
                                    media = new Document();
                                    break;
                            }
                        }

                        if (media != null)
                        {
                            media.Owner = user;
                            media.Url = url;
                            media.Name = name ?? string.Empty;
                            if (!string.IsNullOrEmpty(tags))
                            {
                                media.AddTags(tags);
                            }
                            media.Update();

                            output.AddOutput("location", url);
                        }
                    }

                    output.Success = true;

                }
                catch (MessageException me)
                {
                    // error saving the media
                    output.AddOutput(Constants.Json.Error, me.Message);
                }
                catch (Exception ex)
                {
                    // all other failures
                    logger.Log(LogLevel.Error, "Unable to save media, exception : {0}", ex);
                }
            }

            return output.GetJson();
        }