Exemplo n.º 1
0
        public static MemeRequest GetMemeRequest(string url)
        {
            var result = new MemeRequest();

            if (String.IsNullOrEmpty(url))
            {
                return(result);
            }

            // strip off any application path prefix
            var applicationPath = HttpContext.Current.Request.ApplicationPath ?? "/";

            if (applicationPath == "/")
            {
                url = url.Substring(applicationPath.Length);
            }
            else
            {
                // trim the path, and the slash after it, e.g. /builder/
                url = url.Substring(1 + applicationPath.Length);
            }

            // strip off any action names
            url = _ActionNames.Replace(url, "");

            // urls look like this:  /meme-name/first-line/second-line
            // so we're basically delimiting on slashes
            // if there are surprise slashes, they'll end up in the second line



            var firstSlash = url.IndexOf('/');

            if (firstSlash > 0)
            {
                result.Name = url.Substring(0, firstSlash);
            }
            else
            {
                // we only have the meme name (and maybe not even that!). Time to bail out
                result.Name = url;
                return(result);
            }

            if (url.Length > firstSlash)
            {
                result.Lines = url.Substring(firstSlash + 1).Split(new[] { '/' }, StringSplitOptions.None).ToList();
            }

            return(result);
        }
Exemplo n.º 2
0
        public ActionResult Builder()
        {
            var url         = Request.ServerVariables["HTTP_URL"];
            var memeRequest = MemeRequest.FromUrl(url, Server);

            var meme = MemeUtilities.FindMeme(GlobalMemeConfiguration.Memes, memeRequest.Name);


            int memeLineCount;

            if (meme == null)
            {
                memeRequest.Name  = "ihyk";
                memeRequest.Lines = new List <string>()
                {
                    "I'll have you know I tried other meme generators", "and only wasted hours and hours of my life"
                };
                memeLineCount = GlobalMemeConfiguration.Memes[memeRequest.Name].Lines.Count;
            }
            else
            {
                memeLineCount = meme.Lines.Count;
            }

            var lines = Enumerable.Range(0, memeLineCount)
                        .Select((item, index)
                                => memeRequest.Lines.Count > index
                        ? memeRequest.Lines[index]
                        : string.Empty).ToList();

            var viewModel = new BuilderViewModel
            {
                Memes        = GlobalMemeConfiguration.Memes.GetMemes(),
                SelectedMeme = memeRequest.Name,
                Lines        = lines
            };

            return(View(viewModel));
        }
Exemplo n.º 3
0
        public ActionResult Make()
        {
            var url         = Request.ServerVariables["HTTP_URL"];
            var memeRequest = MemeRequest.FromUrl(url, Server);
            var meme        = MemeUtilities.FindMeme(GlobalMemeConfiguration.Memes, memeRequest.Name);

            if (meme == null)
            {
                // TODO: update this flow to return a proper HTTP 404 code, too
                meme = GlobalMemeConfiguration.NotFoundMeme;
                memeRequest.Lines = new List <string> {
                    "404", "Y U NO USE VALID MEME NAME?"
                };
            }

            // if we want to do this earlier in the process consider
            // if we still need to worry about png vs. jpg. The browser
            // probably doesn't care, but it might be weird if the extension
            // doesn't match the mimetype
            var hasExtension = UrlExtension.IsMatch(url);

            if (!hasExtension)
            {
                return(Redirect(url + Path.GetExtension(meme.ImageFileName)));
            }

            var renderParameters = new RenderParameters
            {
                FullImagePath = HttpContext.Server.MapPath(meme.ImagePath),
                DebugMode     = memeRequest.IsDebugMode,
                FullWatermarkImageFilePath = HttpContext.Server.MapPath("~/Content/UpBoatWatermark.png"),
                WatermarkImageHeight       = 25,
                WatermarkImageWidth        = 25,
                WatermarkText        = "upboat.me",
                WatermarkFont        = "Arial",
                WatermarkFontStyle   = FontStyle.Regular,
                WatermarkFontSize    = 9,
                WatermarkStroke      = Color.Black,
                WatermarkFill        = Color.White,
                WatermarkStrokeWidth = 1,
                PrivateFonts         = MemeConfig.PrivateFontCollection,
                Lines = meme.Lines.Select(l => new LineParameters
                {
                    Bounds = l.Bounds,
                    DoForceTextToAllCaps = l.DoForceTextToAllCaps,
                    Fill          = l.Fill,
                    Font          = l.Font,
                    FontSize      = l.FontSize,
                    FontStyle     = l.FontStyle,
                    HeightPercent = l.HeightPercent,
                    Stroke        = l.Stroke,
                    StrokeWidth   = l.StrokeWidth,
                    TextAlignment = l.TextAlignment,
                    HugBottom     = l.HugBottom
                }).ToList()
            };

            for (var x = 0; x < renderParameters.Lines.Count; x++)
            {
                if (x < memeRequest.Lines.Count)
                {
                    renderParameters.Lines[x].Text = memeRequest.Lines[x].SanitizeMemeText(renderParameters.Lines[x].DoForceTextToAllCaps);
                }
            }

            var renderer = new Renderer();

            var bytes = renderer.Render(renderParameters);

            Analytics.TrackMeme(HttpContext, memeRequest.Name);

            return(new FileContentResult(bytes, meme.ImageType));
        }