Ejemplo n.º 1
0
        public async Task <IHttpActionResult> UpdateAvatar()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            try
            {
                ClaimsPrincipal oPrincipal  = (ClaimsPrincipal)Request.GetRequestContext().Principal;
                var             userId      = oPrincipal.Identity.GetUserId();
                var             userDetails = new UserDetails(userId);
                userDetails.ValidOrBreak();
                var root     = HttpContext.Current.Server.MapPath("~/App_Data");
                var provider = new MultipartFormDataStreamProvider(root);
                await Request.Content.ReadAsMultipartAsync(provider);

                var imageFile = ImageFactory.CreateFullImageModel("users", userDetails.PictureName, provider.FileData);
                ImageContainer.UploadImage(imageFile);

                return(Ok());
            }
            catch (InvalidModelException e)
            {
                return(BadRequest(e.Message));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> CreateClassified()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            try
            {
                ClaimsPrincipal principal = (ClaimsPrincipal)Request.GetRequestContext().Principal;
                var             root      = HttpContext.Current.Server.MapPath("~/App_Data");

                var provider = new MultipartFormDataStreamProvider(root);
                await Request.Content.ReadAsMultipartAsync(provider);

                Directory.CreateDirectory(root);
                var classifiedForm = new ClassifiedForm(provider.FormData)
                {
                    CreatedBy = principal.Identity.GetUserId()
                };

                ModelState.Clear();
                this.Validate <ClassifiedForm>(classifiedForm);

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.WithoutFormName()));
                }

                var classified = new Classified(classifiedForm);
                classified.ValidOrBreak();
                classified.Save();

                var imageFile = ImageFactory.CreateFullImageModel("classifieds", classified.Id, provider.FileData);
                ImageContainer.UploadImage(imageFile);

                return(Ok());
            }
            catch (InvalidModelException e)
            {
                return(BadRequest(e.Message));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }