public override void Get(HttpRequest Request, HttpResponse Response, params string[] PathParams)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetMaxAge(TimeSpan.Zero);

            Int64 AppUserId;

            if (IsAuthorizedRequest(Request, Response, true, out AppUserId))
            {
                Response.ContentType = @"application/json";

                using (StreamWriter streamWriter = new StreamWriter(Response.OutputStream))
                {
                    using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
                    {
                        AppUserAnimal appUserAnimal = AppUserAnimal.FetchByID(AppUserId);

                        jsonWriter.WriteStartObject();

                        jsonWriter.WritePropertyName(@"animal_type");
                        jsonWriter.WriteValue(appUserAnimal != null ? appUserAnimal.AnimalType : "");
                        jsonWriter.WritePropertyName(@"animal_age");
                        jsonWriter.WriteValue(appUserAnimal != null ? appUserAnimal.AnimagAge : "");
                        jsonWriter.WritePropertyName(@"animal_name");
                        jsonWriter.WriteValue(appUserAnimal != null ? appUserAnimal.AnimalName : "");
                        jsonWriter.WritePropertyName(@"animal_image");
                        jsonWriter.WriteValue(appUserAnimal != null ? appUserAnimal.AnimalImg : "");

                        jsonWriter.WriteEndObject();
                    }
                }
            }
        }
        public override void Post(HttpRequest Request, HttpResponse Response, params string[] PathParams)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetMaxAge(TimeSpan.Zero);


            Int64 AppUserId;

            if (IsAuthorizedRequest(Request, Response, true, out AppUserId))
            {
                Response.ContentType = @"application/json";

                string animal_type = null, animal_name = null, animal_age = null;

                if (Request.Form["animal_type"] != null)
                {
                    animal_type = (Request.Form["animal_type"]).ToString();
                }
                if (Request.Form["animal_name"] != null)
                {
                    animal_name = (Request.Form["animal_name"]).ToString();
                }
                if (Request.Form["animal_age"] != null)
                {
                    animal_age = (Request.Form["animal_age"]).ToString();
                }

                using (StreamWriter streamWriter = new StreamWriter(Response.OutputStream))
                {
                    using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
                    {
                        string fn = null;
                        if (Request.Files.Count == 1)
                        {
                            if (!IsAcceptedImageExtension(Request.Files[0].FileName))
                            {
                                RespondUnauthorized(Response);
                            }
                            fn = MediaUtility.SaveFile(Request.Files[0], @"Animal", AppUserId);
                            if (string.IsNullOrEmpty(fn))
                            {
                                RespondInternalServerError(Response);
                            }
                        }
                        Int32 w = Request.Form["w"] != null?Convert.ToInt32(Request.Form["w"]) : 0;

                        Int32 h = Request.Form["h"] != null?Convert.ToInt32(Request.Form["h"]) : 0;

                        AppUserAnimal appUserAnimal = AppUserAnimal.FetchByID(AppUserId);
                        if (appUserAnimal != null && appUserAnimal.AnimalImg != "" && fn != null)
                        {
                            MediaUtility.DeleteImageFilePath(@"Animal", appUserAnimal.AnimalImg, w, h, AppUserId);
                        }
                        if (appUserAnimal == null)
                        {
                            appUserAnimal = new AppUserAnimal();
                        }
                        if (animal_type != null)
                        {
                            appUserAnimal.AnimalType = animal_type;
                        }
                        if (animal_name != null)
                        {
                            appUserAnimal.AnimalName = animal_name;
                        }
                        if (animal_age != null)
                        {
                            appUserAnimal.AnimagAge = animal_age;
                        }
                        if (fn != null && fn != "")
                        {
                            appUserAnimal.AnimalImg = fn;
                        }
                        appUserAnimal.AppUserId = AppUserId;
                        appUserAnimal.Save();

                        jsonWriter.WriteStartObject();
                        jsonWriter.WriteEndObject();
                    }
                }
            }
        }
Beispiel #3
0
        public override void Post(HttpRequest Request, HttpResponse Response, params string[] PathParams)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Int64 AppUserId;

            if (IsAuthorizedRequest(Request, Response, true, out AppUserId))
            {
                Response.ContentType = @"text/plain"; // IE<=9 can't parse JSON when content type is any different than text/plain

                string sub_folder = Request.Form["sub_folder"] != null ? Request.Form["sub_folder"].ToString() : "";

                string fn = null;
                if (Request.Files.Count == 1)
                {
                    if (!IsAcceptedImageExtension(Request.Files[0].FileName))
                    {
                        RespondUnauthorized(Response);
                    }
                    fn = MediaUtility.SaveFile(Request.Files[0], sub_folder, (sub_folder == @"Animal" ? AppUserId : 0));
                    if (string.IsNullOrEmpty(fn))
                    {
                        RespondInternalServerError(Response);
                    }
                }
                Int64 ProductId = Request.Form["product_id"] != null?Convert.ToInt64(Request.Form["product_id"]) : 0;

                Int32 w = Request.Form["w"] != null?Convert.ToInt32(Request.Form["w"]) : 0;

                Int32 h = Request.Form["h"] != null?Convert.ToInt32(Request.Form["h"]) : 0;

                if (sub_folder == @"Animal")
                {
                    AppUserAnimal animal = AppUserAnimal.FetchByID(AppUserId);
                    MediaUtility.DeleteImageFilePath(sub_folder, animal.AnimalImg, w, h, AppUserId);
                    animal.AnimalImg = fn;
                    animal.Save();
                }
                else if (sub_folder == @"Product")
                {
                    Product p = Product.FetchByID(ProductId);
                    MediaUtility.DeleteImageFilePath(sub_folder, p.ProductImage, w, h, 0);
                    p.ProductImage = fn;
                    p.Save();
                }
                else if (sub_folder == @"ProductYad2")
                {
                    ProductYad2 p = ProductYad2.FetchByID(ProductId);
                    MediaUtility.DeleteImageFilePath(sub_folder, p.ProductImage, w, h, 0);
                    p.ProductImage = fn;
                    p.Save();
                }

                if (!string.IsNullOrEmpty(fn))
                {
                    using (StreamWriter streamWriter = new StreamWriter(Response.OutputStream))
                    {
                        using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
                        {
                            jsonWriter.WriteStartObject();

                            jsonWriter.WritePropertyName("file_name");
                            jsonWriter.WriteValue(fn);

                            //jsonWriter.WritePropertyName("preview");
                            //jsonWriter.WriteValue(MediaUtility.GetImageFilePath(AppUserId, @"Animal",HttpUtility.UrlEncode(fn),64,64));

                            //jsonWriter.WritePropertyName("url");
                            //jsonWriter.WriteValue(MediaUtility.GetOriginalFilePath(AppUserId, @"Animal",HttpUtility.UrlEncode(fn)));

                            jsonWriter.WriteEndObject();
                        }
                    }
                }
                else
                {
                    RespondInternalServerError(Response);
                }
            }
        }
        public override void Post(HttpRequest Request, HttpResponse Response, params string[] PathParams)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetMaxAge(TimeSpan.Zero);

            JObject inputData = null;

            try
            {
                using (StreamReader reader = new StreamReader(Request.InputStream))
                {
                    using (JsonTextReader jsonReader = new JsonTextReader(reader))
                    {
                        inputData = JObject.Load(jsonReader);
                    }
                }
            }
            catch
            {
                RespondBadRequest(Response);
            }

            Int64 AppUserId;

            if (IsAuthorizedRequest(Request, Response, true, out AppUserId))
            {
                Response.ContentType = @"application/json";

                JToken jt;
                string animal_type = null, animal_name = null, animal_age = null;

                if (inputData.TryGetValue(@"animal_type", out jt))
                {
                    animal_type = jt.Value <string>();
                }
                if (inputData.TryGetValue(@"animal_name", out jt))
                {
                    animal_name = jt.Value <string>();
                }
                if (inputData.TryGetValue(@"animal_age", out jt))
                {
                    animal_age = jt.Value <string>();
                }

                using (StreamWriter streamWriter = new StreamWriter(Response.OutputStream))
                {
                    using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
                    {
                        AppUserAnimal appUserAnimal = AppUserAnimal.FetchByID(AppUserId);
                        if (appUserAnimal == null)
                        {
                            appUserAnimal = new AppUserAnimal();
                        }
                        if (animal_type != null)
                        {
                            appUserAnimal.AnimalType = animal_type;
                        }
                        if (animal_name != null)
                        {
                            appUserAnimal.AnimalName = animal_name;
                        }
                        if (animal_age != null)
                        {
                            appUserAnimal.AnimagAge = animal_age;
                        }
                        appUserAnimal.AppUserId = AppUserId;
                        appUserAnimal.Save();

                        jsonWriter.WriteStartObject();
                        jsonWriter.WriteEndObject();
                    }
                }
            }
        }