Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        /// <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));
                }
            }
        }