Beispiel #1
0
        private JObject createMedia(NetmeraContent content)
        {
            JObject json = null;

            try
            {
                JObject mediaObject      = new JObject();
                bool    isMediaFileAdded = false;

                IEnumerable <JProperty> jlist = mediaData.Properties();

                foreach (JProperty token in jlist)
                {
                    String key = token.Name;
                    String tmpMediaDataJson = mediaData.Value <String>(key);

                    byte[] tmpMediaData = JsonConvert.DeserializeObject <byte[]>(tmpMediaDataJson);

                    NetmeraMedia file = new NetmeraMedia(tmpMediaData);

                    isMediaFileAdded = true;

                    String appId       = content.getContent().Value <string>(NetmeraConstants.Site);
                    String apiKey      = NetmeraClient.getSecurityToken();
                    String contentPath = content.getPath();
                    String viewerId    = content.getOwner().Value <String>(NetmeraConstants.Netmera_Owner_Node_Name);
                    String mediaUrl    = file.save(appId, apiKey, contentPath, viewerId);

                    mediaObject.Add(key, mediaUrl);
                }
                if (isMediaFileAdded)
                {
                    json = NetmeraHttpUtils.updateContent(mediaObject, content.getPath());
                }
            }
            catch (JsonException)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of creating media file is invalid");
            }
            catch (WebException)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Request exception occurred while saving media data");
            }
            catch (IOException)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred while saving media data");
            }
            return(json);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the <see cref="NetmeraMedia"/> object with the specified key.
        /// </summary>
        /// <param name="key">Key to get value</param>
        /// <returns><see cref="NetmeraMedia"/> object with the specified key.If the object type is not an  <see cref="NetmeraMedia"/> or key does not exists then it returns null.</returns>
        public NetmeraMedia getNetmeraMedia(String key)
        {
            if (string.IsNullOrEmpty(data.Value <String>(key)))
            {
                return(null);
            }

            Object obj;

            try
            {
                obj = data.Value <String>(key);
            }
            catch (JsonException)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_KEY, "Json with key [" + key + "] is invalid.");
            }
            if (obj.GetType() != typeof(String))
            {
                return(null);
            }

            if (!Regex.IsMatch((String)obj, NetmeraConstants.Url_Pattern))
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_URL, key);
            }

            String url = (String)obj;

            byte[] imageBytes = null;
            try
            {
                imageBytes = HttpUtils.toByteArray(new Uri(url));
            }
            catch (UriFormatException e)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_DATA_TYPE, e.Message);
            }
            catch (IOException e)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, e.Message);
            }

            NetmeraMedia nm = new NetmeraMedia(imageBytes);

            nm.setUrl(url);

            return(nm);
        }
        /// <summary>
        ///  Gets the  <see cref="NetmeraMedia"/> object with the specified key.
        /// </summary>
        /// <param name="key">Key to get value</param>
        /// <param name="callback">Method called when nmedia get operation finishes</param>
        public void getNetmeraMedia(String key, Action <NetmeraMedia, Exception> callback)
        {
            if (string.IsNullOrEmpty(data.Value <String>(key)))
            {
                if (callback != null)
                {
                    callback(null, null);
                }
                //return null;
            }

            Object obj = null;

            try
            {
                obj = data.Value <String>(key);
            }
            catch (JsonException)
            {
                if (callback != null)
                {
                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_KEY, "Json with key [" + key + "] is invalid."));
                }
            }
            if (obj.GetType() != typeof(String))
            {
                if (callback != null)
                {
                    callback(null, null);
                }
                //return null;
            }

            if (!Regex.IsMatch((String)obj, NetmeraConstants.Url_Pattern))
            {
                if (callback != null)
                {
                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_URL, key));
                }
            }

            String url = (String)obj;

            try
            {
                HttpUtils.toByteArray(new Uri(url), (imageBytes, ex) =>
                {
                    if (ex != null || imageBytes == null)
                    {
                        if (callback != null)
                        {
                            callback(null, ex);
                        }
                    }
                    else
                    {
                        NetmeraMedia nm = new NetmeraMedia(imageBytes);
                        nm.setUrl(url);
                        if (callback != null)
                        {
                            callback(nm, null);
                        }
                    }
                });
            }
            catch (UriFormatException e)
            {
                if (callback != null)
                {
                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_DATA_TYPE, e.Message));
                }
            }
            catch (IOException e)
            {
                if (callback != null)
                {
                    callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, e.Message));
                }
            }
        }
        /// <summary>
        /// Adds key,value pairs into the object. If the object contains key, the old value is replaced.
        /// </summary>
        /// <param name="key">Key to identify specified value</param>
        /// <param name="value">value associates with the specified key</param>
        /// <exception cref="NetmeraException">Throws exception if key is null, if value is null</exception>
        public void add(String key, Object value)
        {
            if (key == null)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, NetmeraConstants.ContentKey);
            }

            if (value == null)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, NetmeraConstants.ContentValue);
            }

            if ((value.GetType() == typeof(JObject)) == false && (value.GetType() == typeof(JArray)) == false && (value.GetType() == typeof(String)) == false &&
                (value.GetType() == typeof(Boolean)) == false && (value.GetType() == typeof(DateTime)) == false && (value.GetType() == typeof(byte[])) == false &&
                (value.GetType() == typeof(double)) == false && (value.GetType() == typeof(Double)) == false && (value.GetType() == typeof(float)) == false &&
                (value.GetType() == typeof(long)) == false && (value.GetType() == typeof(int) == false) && (value.GetType() == typeof(Int16)) == false &&
                (value.GetType() == typeof(Int32)) == false && (value.GetType() == typeof(Int64)) == false && (value.GetType() == typeof(NetmeraMedia)) == false && (value.GetType() == typeof(NetmeraGeoLocation)) == false && (value.GetType() == typeof(NetmeraUser)) == false)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_DATA_TYPE, value.GetType().ToString());
            }

            try
            {
                if (value.GetType() == typeof(NetmeraGeoLocation))
                {
                    double lat = ((NetmeraGeoLocation)value).getLatitude();
                    double lng = ((NetmeraGeoLocation)value).getLongitude();

                    //Replacements are done because ToString() method convert points in a decimal number to comma.
                    String location = lat.ToString().Replace(',', '.') + "," + lng.ToString().Replace(',', '.');

                    JProperty prLocationField = new JProperty(key + NetmeraConstants.LocationField_Suffix, location);

                    //if data already contains a node with name "key + NetmeraConstants.LocationField_Suffix"
                    data.Remove(key + NetmeraConstants.LocationField_Suffix);

                    data.Add(prLocationField);

                    JProperty prLocationLatitudeField = new JProperty(key + NetmeraConstants.LocationLatitude_Suffix, lat);

                    //if data already contains a node with name "key + NetmeraConstants.LocationLatitude_Suffix"
                    data.Remove(key + NetmeraConstants.LocationLatitude_Suffix);

                    data.Add(prLocationLatitudeField);

                    JProperty prLocationLongitudeField = new JProperty(key + NetmeraConstants.LocationLongitude_Suffix, lng);

                    //if data already contains a node with name "key + NetmeraConstants.LocationLongitude_Suffix"
                    data.Remove(key + NetmeraConstants.LocationLongitude_Suffix);

                    data.Add(prLocationLongitudeField);
                }
                else if (value.GetType() == typeof(NetmeraUser))
                {
                    NetmeraClient.setLoggedUserSecurityToken(NetmeraUser.securityToken);
                }
                else if (value.GetType() == typeof(NetmeraMedia))
                {
                    //mediaData.Add(key, value);

                    NetmeraMedia tmpMedia     = (NetmeraMedia)value;
                    byte[]       tmpMediaData = tmpMedia.getData(); //url is already null, so no need to add to json

                    JProperty prop = new JProperty(key, JsonConvert.SerializeObject(tmpMediaData));

                    //if mediaData already contains a node with name "key"
                    mediaData.Remove(key);

                    mediaData.Add(prop);
                }
                else
                {
                    if (value is string)
                    {
                        var allow = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890&-()%'!@[]{}#^*;:/?<>,.|~^$";

                        var allowedChars = allow.ToCharArray();
                        var valueArray   = value.ToString().ToCharArray();
                        var newValue     = new List <char>();
                        for (int i = 0; i < valueArray.Length; i++)
                        {
                            if (allowedChars.Contains(valueArray[i]))
                            {
                                newValue.Add(valueArray[i]);
                            }
                        }

                        var s = new string(newValue.ToArray()).Trim();

                        value = HttpUtility.UrlEncode(s);
                    }

                    JProperty prop = new JProperty(key, value);

                    //if data already contains a node with name "key"
                    data.Remove(key);

                    data.Add(prop);
                }
            }
            catch (JsonException e)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json with key : [" + key + "], value : [" + value + "] pairs is invalid.", e.Message);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Adds key,value pairs into the object. If the object contains key, the old value is replaced.
        /// </summary>
        /// <param name="key">Key to identify specified value</param>
        /// <param name="value">Value associates with the specified key</param>
        /// <exception cref="NetmeraException">Throws exception if key is null, if value is null</exception>
        public void add(String key, Object value)
        {
            if (key == null)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, NetmeraConstants.ContentKey);
            }

            if (value == null)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, NetmeraConstants.ContentValue);
            }

            if ((value.GetType() == typeof(JObject)) == false && (value.GetType() == typeof(JArray)) == false && (value.GetType() == typeof(String)) == false &&
                (value.GetType() == typeof(Boolean)) == false && (value.GetType() == typeof(DateTime)) == false && (value.GetType() == typeof(byte[])) == false &&
                (value.GetType() == typeof(double)) == false && (value.GetType() == typeof(Double)) == false && (value.GetType() == typeof(float)) == false &&
                (value.GetType() == typeof(long)) == false && (value.GetType() == typeof(int) == false) && (value.GetType() == typeof(Int16)) == false &&
                (value.GetType() == typeof(Int32)) == false && (value.GetType() == typeof(Int64)) == false && (value.GetType() == typeof(NetmeraMedia)) == false && (value.GetType() == typeof(NetmeraGeoLocation)) == false && (value.GetType() == typeof(NetmeraUser)) == false)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_DATA_TYPE, value.GetType().ToString());
            }

            try
            {
                if (value.GetType() == typeof(NetmeraGeoLocation))
                {
                    double lat = ((NetmeraGeoLocation)value).getLatitude();
                    double lng = ((NetmeraGeoLocation)value).getLongitude();

                    //Replacements are done because ToString() method convert points in a decimal number to comma.
                    String location = lat.ToString().Replace(',', '.') + "," + lng.ToString().Replace(',', '.');

                    JProperty prLocationField = new JProperty(key + NetmeraConstants.LocationField_Suffix, location);

                    //if data already contains a node with name "key + NetmeraConstants.LocationField_Suffix"
                    data.Remove(key + NetmeraConstants.LocationField_Suffix);

                    data.Add(prLocationField);

                    JProperty prLocationLatitudeField = new JProperty(key + NetmeraConstants.LocationLatitude_Suffix, lat);

                    //if data already contains a node with name "key + NetmeraConstants.LocationLatitude_Suffix"
                    data.Remove(key + NetmeraConstants.LocationLatitude_Suffix);

                    data.Add(prLocationLatitudeField);

                    JProperty prLocationLongitudeField = new JProperty(key + NetmeraConstants.LocationLongitude_Suffix, lng);

                    //if data already contains a node with name "key + NetmeraConstants.LocationLongitude_Suffix"
                    data.Remove(key + NetmeraConstants.LocationLongitude_Suffix);

                    data.Add(prLocationLongitudeField);
                }
                else if (value.GetType() == typeof(NetmeraUser))
                {
                    NetmeraClient.setLoggedUserSecurityToken(NetmeraUser.securityToken);
                }
                else if (value.GetType() == typeof(NetmeraMedia))
                {
                    //mediaData.Add(key, value);

                    NetmeraMedia tmpMedia     = (NetmeraMedia)value;
                    byte[]       tmpMediaData = tmpMedia.getData(); //url is already null, so no need to add to json

                    JProperty prop = new JProperty(key, JsonConvert.SerializeObject(tmpMediaData));

                    //if mediaData already contains a node with name "key"
                    mediaData.Remove(key);

                    mediaData.Add(prop);
                }
                else
                {
                    JProperty prop = new JProperty(key, value);

                    //if data already contains a node with name "key"
                    data.Remove(key);

                    data.Add(prop);
                }
            }
            catch (JsonException e)
            {
                throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json with key : [" + key + "], value : [" + value + "] pairs is invalid.", e.Message);
            }
        }