Esempio n. 1
0
        public IHttpActionResult Post()
        {
            return(Authorized(token =>
            {
                const String fileName = "profile.png";

                var contactId = _authenticationService.GetContactId(token);
                var files = _mpService.GetFileDescriptions("MyContact", contactId, token);
                var file = files.FirstOrDefault(f => f.IsDefaultImage);
                var base64String = Request.Content.ReadAsStringAsync().Result;

                if (base64String.Length == 0)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "Request did not specify a \"file\" for the profile image."));
                }

                var imageBytes = Convert.FromBase64String(base64String.Split(',')[1]);

                if (file != null)
                {
                    _mpService.UpdateFile(
                        file.FileId,
                        fileName,
                        "Profile Image",
                        true,
                        -1,
                        imageBytes,
                        token
                        );
                }
                else
                {
                    _mpService.CreateFile(
                        "MyContact",
                        contactId,
                        fileName,
                        "Profile Image",
                        true,
                        -1,
                        imageBytes,
                        token
                        );
                }
                return (Ok());
            }));
        }