Example #1
0
 private static Stream CreateImageStream(IconModel model, Profile profile)
 {
     if (model.SvgFile != null)
     {
         return(RenderSvgToStream(model.SvgFile, profile.Width, profile.Height, profile.Format, model.Padding, model.Background));
     }
     else
     {
         return(ResizeImage(model.InputImage, profile.Width, profile.Height, profile.Format, model.Padding, model.Background));
     }
 }
        // POST api/image
        public async Task<HttpResponseMessage> Post()
        {
            string root = HttpContext.Current.Server.MapPath("~/App_Data");
            var provider = new MultipartFormDataStreamProvider(root);
            Guid zipId = Guid.NewGuid();

            var model = new IconModel();

            try
            {
                // Read the form data.
                await Request.Content.ReadAsMultipartAsync(provider);

                MultipartFileData multipartFileData = provider.FileData.First();

                var ct = multipartFileData.Headers.ContentType.MediaType;
                if (ct != null && ct.Contains("svg"))
                {
                    model.SvgFile = multipartFileData.LocalFileName;
                }
                else
                {
                    model.InputImage = Image.FromFile(multipartFileData.LocalFileName);
                }
                model.Padding = Convert.ToDouble(provider.FormData.GetValues("padding")[0]);
                if (model.Padding < 0 || model.Padding > 1.0)
                {
                    // Throw out as user has supplied invalid hex string..
                    HttpResponseMessage httpResponseMessage =
                        Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Padding value invalid. Please input a number between 0 and 1");
                    return httpResponseMessage;
                }

                var colorStr = provider.FormData.GetValues("color") != null ? provider.FormData.GetValues("color")[0] : null;
                if (!string.IsNullOrEmpty(colorStr))
                {
                    try
                    {
                        var colorConverter = new ColorConverter();
                        model.Background = (Color)colorConverter.ConvertFromString(colorStr);
                    }
                    catch (Exception ex)
                    {
                        // Throw out as user has supplied invalid hex string..
                        HttpResponseMessage httpResponseMessage = 
                            Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Background Color value invalid. Please input a valid hex color.", ex);
                        return httpResponseMessage;
                    }
                }

                var platform = provider.FormData.GetValues("platform") != null ? provider.FormData.GetValues("platform")[0] : "ManifoldJS";
                if (!string.IsNullOrEmpty(platform))
                {
                    model.Platform = platform;
                }


                //get the platform and profiles
                IEnumerable<string> config = GetConfig(model.Platform);
                if (config.Count() < 1)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }
                List<Profile> profiles = null;
                foreach (var cfg in config)
                {
                    if (profiles == null)
                        profiles = JsonConvert.DeserializeObject<List<Profile>>(cfg);
                    else
                        profiles.AddRange(JsonConvert.DeserializeObject<List<Profile>>(cfg));
                }



                using (var zip = new ZipFile())
                {
                    var iconObject = new IconRootObject();
                    foreach (var profile in profiles)
                    {

                        var stream = CreateImageStream(model, profile);

                        //var stream = ResizeImage(model.InputImage, profile.Width, profile.Height, profile.Format, model.Padding, model.Background);
                        string fmt = string.IsNullOrEmpty(profile.Format) ? "png" : profile.Format;
                        zip.AddEntry(profile.Folder + profile.Name + "." + fmt, stream);
                        stream.Flush();

                        iconObject.icons.Add(new IconObject(profile.Name + "." + fmt, profile.Width + "x" + profile.Height));
                    }

                    var iconStr = JsonConvert.SerializeObject(iconObject, Formatting.Indented);

                    zip.AddEntry("icons.json", iconStr);

                    string zipFilePath = CreateFilePathFromId(zipId);
                    zip.Save(zipFilePath);
                }
            }
            catch (OutOfMemoryException ex)
            {
                HttpResponseMessage httpResponseMessage = Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, ex);
                return httpResponseMessage;
            }
            catch (Exception ex)
            {
                HttpResponseMessage httpResponseMessage = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
                return httpResponseMessage;
            }
            string url = Url.Route("DefaultApi", new { controller = "image", id = zipId.ToString() });

            var uri = new Uri(url, UriKind.Relative);
            var responseMessage = Request.CreateResponse(HttpStatusCode.Created,
                new ImageResponse { Uri = uri });

            responseMessage.Headers.Location = uri;

            return responseMessage;
        }
Example #3
0
        // POST api/image
        public async Task <HttpResponseMessage> Post()
        {
            string root     = HttpContext.Current.Server.MapPath("~/App_Data");
            var    provider = new MultipartFormDataStreamProvider(root);
            Guid   zipId    = Guid.NewGuid();

            try
            {
                // Read the form data.
                await Request.Content.ReadAsMultipartAsync(provider);

                MultipartFileData multipartFileData = provider.FileData.First();

                using (var model = new IconModel())
                {
                    var ct = multipartFileData.Headers.ContentType.MediaType;
                    if (ct != null && ct.Contains("svg"))
                    {
                        model.SvgFile = multipartFileData.LocalFileName;
                    }
                    else
                    {
                        model.InputImage = Image.FromFile(multipartFileData.LocalFileName);
                    }
                    model.Padding = Convert.ToDouble(provider.FormData.GetValues("padding")[0]);
                    if (model.Padding < 0 || model.Padding > 1.0)
                    {
                        // Throw out as user has supplied invalid hex string..
                        HttpResponseMessage httpResponseMessage =
                            Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Padding value invalid. Please input a number between 0 and 1");
                        return(httpResponseMessage);
                    }

                    var colorStr     = provider.FormData.GetValues("color")?[0];
                    var colorChanged = provider.FormData.GetValues("colorChanged")?[0] == "1";

                    if (!string.IsNullOrEmpty(colorStr) && colorChanged)
                    {
                        try
                        {
                            var colorConverter = new ColorConverter();
                            model.Background = (Color)colorConverter.ConvertFromString(colorStr);
                        }
                        catch (Exception ex)
                        {
                            // Throw out as user has supplied invalid hex string..
                            HttpResponseMessage httpResponseMessage =
                                Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Background Color value invalid. Please input a valid hex color.", ex);
                            return(httpResponseMessage);
                        }
                    }

                    var platforms = provider.FormData.GetValues("platform");

                    if (platforms == null)
                    {
                        // Throw out as user has supplied no platforms..
                        HttpResponseMessage httpResponseMessage =
                            Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No platform has been specified.");
                        return(httpResponseMessage);
                    }

                    model.Platforms = platforms;

                    List <Profile> profiles = null;

                    foreach (var platform in model.Platforms)
                    {
                        // Get the platform and profiles
                        IEnumerable <string> config = GetConfig(platform);
                        if (config.Count() < 1)
                        {
                            throw new HttpResponseException(HttpStatusCode.BadRequest);
                        }

                        foreach (var cfg in config)
                        {
                            if (profiles == null)
                            {
                                profiles = JsonConvert.DeserializeObject <List <Profile> >(cfg);
                            }
                            else
                            {
                                profiles.AddRange(JsonConvert.DeserializeObject <List <Profile> >(cfg));
                            }
                        }
                    }

                    using (var zip = new ZipFile())
                    {
                        var iconObject = new IconRootObject();
                        foreach (var profile in profiles)
                        {
                            var stream = CreateImageStream(model, profile);

                            string fmt = string.IsNullOrEmpty(profile.Format) ? "png" : profile.Format;
                            zip.AddEntry(profile.Folder + profile.Name + "." + fmt, stream);
                            stream.Flush();

                            iconObject.icons.Add(new IconObject(profile.Folder + profile.Name + "." + fmt, profile.Width + "x" + profile.Height));
                        }

                        var iconStr = JsonConvert.SerializeObject(iconObject, Formatting.Indented);

                        zip.AddEntry("icons.json", iconStr);

                        string zipFilePath = CreateFilePathFromId(zipId);
                        zip.Save(zipFilePath);
                    }
                }

                // Delete source image file from local disk
                File.Delete(multipartFileData.LocalFileName);
            }
            catch (OutOfMemoryException ex)
            {
                HttpResponseMessage httpResponseMessage = Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, ex);
                return(httpResponseMessage);
            }
            catch (Exception ex)
            {
                HttpResponseMessage httpResponseMessage = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
                return(httpResponseMessage);
            }

            string url = Url.Route("DefaultApi", new { controller = "image", id = zipId.ToString() });

            var uri             = new Uri(url, UriKind.Relative);
            var responseMessage = Request.CreateResponse(HttpStatusCode.Created,
                                                         new ImageResponse {
                Uri = uri
            });

            responseMessage.Headers.Location = uri;

            return(responseMessage);
        }
 private static Stream CreateImageStream(IconModel model, Profile profile)
 {
     if (model.SvgFile != null)
     {
         return RenderSvgToStream(model.SvgFile, profile.Width, profile.Height, profile.Format, model.Padding, model.Background);
     }
     else
     {
         return ResizeImage(model.InputImage, profile.Width, profile.Height, profile.Format, model.Padding, model.Background);
     }
 }