Exemplo n.º 1
0
        public IHttpActionResult Post([FromUri] string name, [FromUri] string description, [FromUri] string color, FormDataCollection formData)
        {
            string imagePath = "";

            if (formData["banner"] != null && formData["banner"].Length > 0)
            {
                try
                {
                    var bitmap = ImageDecoder.DecodeBase64String(formData["banner"]);

                    string extension = string.Empty;
                    if (bitmap.RawFormat.Equals(ImageFormat.Jpeg))
                    {
                        extension = ".jpg";
                    }
                    if (bitmap.RawFormat.Equals(ImageFormat.Png))
                    {
                        extension = ".png";
                    }
                    if (bitmap.RawFormat.Equals(ImageFormat.Bmp))
                    {
                        extension = ".bmp";
                    }
                    if (bitmap.RawFormat.Equals(ImageFormat.Gif))
                    {
                        extension = ".gif";
                    }

                    if (string.IsNullOrEmpty(extension))
                    {
                        return(Content(HttpStatusCode.InternalServerError, "The supplied image is not a valid image"));
                    }

                    imagePath = FileHelper.NextAvailableFilename(Path.Combine(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["OrganisationsImgPath"]), "organisation" + extension));
                    bitmap.Save(imagePath);

                    imagePath = Path.GetFileName(imagePath);
                }
                catch (Exception ex)
                {
                    Content(HttpStatusCode.InternalServerError, ex);
                }
            }

            var userIdentity = RequestContext.Principal.Identity as ClaimsIdentity;

            if (userIdentity == null)
            {
                return(InternalServerError());
            }

            var email = userIdentity.Claims.First(c => c.Type == "sub").Value;

            if (email == null)
            {
                return(InternalServerError());
            }

            var user = userManager.ReadUser(email);

            if (user == null)
            {
                return(InternalServerError());
            }

            if (organisationManager.ReadOrganisation(name) != null)
            {
                return(InternalServerError(new Exception("Organisation name already exists")));
            }

            Organisation organisation = organisationManager.CreateOrganisation(name, imagePath, user);

            return(Ok(organisation));
        }
        public IHttpActionResult createPlaylist(FormDataCollection formData)
        {
            var userIdentity = RequestContext.Principal.Identity as ClaimsIdentity;

            if (userIdentity == null)
            {
                return(NotFound());
            }

            var email = userIdentity.Claims.First(c => c.Type == "sub").Value;

            if (email == null)
            {
                return(NotFound());
            }

            var user = userManager.ReadUser(email);

            if (user == null)
            {
                return(NotFound());
            }

            string imagePath = "";

            if (formData["image"] != null && formData["image"].Length > 0)
            {
                try
                {
                    var bitmap = ImageDecoder.DecodeBase64String(formData["image"]);

                    string extension = string.Empty;
                    if (bitmap.RawFormat.Equals(ImageFormat.Jpeg))
                    {
                        extension = ".jpg";
                    }
                    if (bitmap.RawFormat.Equals(ImageFormat.Png))
                    {
                        extension = ".png";
                    }
                    if (bitmap.RawFormat.Equals(ImageFormat.Bmp))
                    {
                        extension = ".bmp";
                    }
                    if (bitmap.RawFormat.Equals(ImageFormat.Gif))
                    {
                        extension = ".gif";
                    }

                    if (string.IsNullOrEmpty(extension))
                    {
                        return(BadRequest("The supplied image is not a valid image"));
                    }

                    imagePath = FileHelper.NextAvailableFilename(Path.Combine(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PlaylistImgPath"]), "playlist" + extension));
                    bitmap.Save(imagePath);

                    imagePath = Path.GetFileName(imagePath);
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }

            if (formData["organisationId"] != null)
            {
                var playlist = playlistManager.CreatePlaylistForOrganisation(formData["name"], formData["description"], formData["key"], 1, false, imagePath, user, long.Parse(formData["organisationId"]));
                if (playlist != null)
                {
                    return(Ok(playlist));
                }
            }
            else
            {
                var playlist = playlistManager.CreatePlaylistForUser(formData["name"], formData["description"], formData["key"], 1, false, imagePath, user);
                if (playlist != null)
                {
                    return(Ok(playlist));
                }
            }

            return(BadRequest());
        }