示例#1
0
        public ActionResult Submit(MemeInfo model)
        {
            var file = Request.Files["file"];

            if (file == null || file.ContentLength == 0)
            {
                ModelState.AddModelError("file", "Upload File is requred.");
                return(View(model));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            else
            {
                using (var ctx = GetCtx())
                {
                    ctx.Memes.Add(new Meme
                    {
                        MimeType    = MimeMapping.GetMimeMapping(file.FileName),
                        File        = ReadFully(file.InputStream),
                        Description = model.Description,
                        Genre       = model.Genre,
                        Title       = model.Title,
                        UserId      = CurrentUserId()
                    });
                    ctx.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
        }
示例#2
0
        public ActionResult Submit(MemeInfo model)
        {
            var file = Request.Form.Files.FirstOrDefault();

            if (file == null || file.Length == 0)
            {
                ModelState.AddModelError("file", "Upload File is requred.");
                return(View(model));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            else
            {
                var type = MimeTypesMap.GetMimeType(file.FileName);

                using (var ctx = GetCtx())
                {
                    ctx.Memes.Add(new Meme
                    {
                        MimeType    = type,
                        File        = ReadFully(file.OpenReadStream()),
                        Description = model.Description,
                        Genre       = model.Genre,
                        Title       = model.Title,
                        UserId      = CurrentUserId()
                    });
                    ctx.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
        }
示例#3
0
        public override async Task <MemeInfo> RandomAsync()
        {
            WebClient wc;
            string    html;

            try
            {
                wc   = new WebClient();
                html = await wc.DownloadStringTaskAsync(_uris[UriType.Random]);
            }
            catch (WebException ex)
            {
                throw new ServiceOrConnectionException("Could not load the page", ex);
            }

            IConfiguration   config   = Configuration.Default;
            IBrowsingContext context  = BrowsingContext.New(config);
            IDocument        document = await context.OpenAsync(req => req.Content(html).Address(_baseUrl));

            IElement picDiv = document.DocumentElement.QuerySelector(".media-element-wrapper");

            IHtmlImageElement   img    = (IHtmlImageElement)picDiv.QuerySelector(".figure-holder figure img");
            IHtmlHeadingElement h      = (IHtmlHeadingElement)picDiv.QuerySelector(".content h1");
            IElement            player = picDiv.QuerySelector(".figure-holder figure player");

            if ((player == null && img == null) || h == null)
            {
                throw new NotFoundException(
                          "Either \"img\", \"player\" or \"h\" tag could not be found");
            }

            // Unfortunately on Kwejk random image page there's no link to the
            // original image page, so ViewURI = URI
            MemeInfo meme;

            if (player != null)
            {
                meme = new MemeInfo
                {
                    ViewURI = player.GetAttribute("source"),
                    URI     = player.GetAttribute("source"),
                    Alt     = string.Empty,
                    Name    = h.TextContent.Trim(),
                    Type    = MediaType.Video
                };
            }
            else
            {
                meme = new MemeInfo
                {
                    ViewURI = img.Source,
                    URI     = img.Source,
                    Alt     = img.AlternativeText,
                    Name    = h.TextContent.Trim(),
                    Type    = MediaType.Image
                };
            }

            return(meme);
        }
示例#4
0
 public Meme(MemeInfo memeInfo)
 {
     CatText      = memeInfo.CatText;
     DrummerText  = memeInfo.DrummerText;
     DrumText     = memeInfo.DrumText;
     Guid         = new Guid(Utils.MD5($"{CatText}{DrummerText}{DrumText}"));
     MemeWork     = new MemeWork(this);
     CreationDate = DateTime.Now;
 }
示例#5
0
        public async Task Random_ResultMemeInfo()
        {
            MemeInfo meme = await _wrapper.RandomAsync();

            Assert.IsTrue(new Regex(@"^https?:\/\/komixxy.pl\/.+$")
                          .IsMatch(meme.ViewURI));

            Assert.IsTrue(new Regex(@"^https?:\/\/komixxy.pl\/.+$")
                          .IsMatch(meme.URI));
        }
示例#6
0
        public async Task Random_ResultMemeInfo()
        {
            MemeInfo meme = await _wrapper.RandomAsync();

            // Due to the Kwejk random image page working, image URI and view URI
            // are the same
            Assert.AreEqual(meme.URI, meme.ViewURI);

            Assert.IsTrue(new Regex(@"^https?:\/\/.+\.kwejk\.pl\/.+$").IsMatch(meme.URI));
        }
示例#7
0
        public async Task <MemeInfo> GenerateAsync(
            string title,
            string description,
            byte[] image,
            string imageFilename)
        {
            HttpClient hc;
            string     html;

            try
            {
                hc = new HttpClient();

                MultipartFormDataContent content = new MultipartFormDataContent();
                content.Add(new StringContent(title), "tekst");
                content.Add(new StringContent(description), "opis");
                content.Add(new ByteArrayContent(image), "plik", imageFilename);

                HttpResponseMessage response =
                    await hc.PostAsync(_uris[UriType.Page], content);

                html = await response.Content.ReadAsStringAsync();
            }
            catch (WebException ex)
            {
                throw new ServiceOrConnectionException("Could not load page", ex);
            }

            IConfiguration   config   = Configuration.Default;
            IBrowsingContext context  = BrowsingContext.New(config);
            IDocument        document = await context.OpenAsync(req => req.Content(html));

            IHtmlInputElement demotDiv = (IHtmlInputElement)document.DocumentElement
                                         .QuerySelector("form > input[type=\"text\"][name=\"nazwa\"]");

            if (demotDiv == null)
            {
                throw new NotFoundException("\"input\" tag could not be found");
            }

            MemeInfo meme = new MemeInfo
            {
                ViewURI = demotDiv.Value,
                URI     = demotDiv.Value,
                Alt     = title,
                Name    = title,
                Type    = MediaType.Image
            };

            return(meme);
        }
示例#8
0
        public ActionResult <String> Create(MemeInfo memeInfo)
        {
            var guid     = Utils.MD5($"{memeInfo.CatText}{memeInfo.DrummerText}{memeInfo.DrumText}");
            var tempMeme = _memeRepo.Get(new Guid(guid));

            if (tempMeme != null)
            {
                return(Ok($"{tempMeme.Guid}:{tempMeme.MemeWork.Status}"));
            }
            var meme = new Meme(memeInfo);

            _memeRepo.Add(meme);
            return(Ok($"{meme.Guid}:{meme.MemeWork.Status}"));
        }
示例#9
0
        public async Task Generate_ResultMemeInfo()
        {
            const string title = "foo";

            MemeInfo meme = await _wrapper.GenerateAsync(
                title,
                "bar",
                File.ReadAllBytes("../../../Files/random-image.jpg"),
                "baz.jpg");

            Assert.AreEqual(meme.URI, meme.ViewURI);
            Assert.AreEqual(meme.Name, meme.Alt);

            Assert.AreEqual(title, meme.Name);

            Assert.IsTrue(new Regex(@"^http:\/\/demotmaker.com.pl\/obrazki\/.+\.jpg$")
                          .IsMatch(meme.URI));
        }
示例#10
0
        public ActionResult Update(MemeInfo model)
        {
            if (ModelState.IsValid)
            {
                using (var ctx = GetCtx())
                {
                    var meme = ctx.Memes.FirstOrDefault(m => m.MemeId == model.Id);
                    if (meme != null)
                    {
                        meme.Description = model.Description;
                        meme.Genre       = model.Genre;
                        meme.Title       = model.Title;
                        ctx.SaveChanges();

                        ViewBag.SuccessMessage = $"Meme \"{meme.Title}\" updated!";

                        DeleteMemeFromCache(model.Id);
                    }
                }
            }

            return(View("Detail", model));
        }
示例#11
0
        public override async Task <MemeInfo> RandomAsync()
        {
            WebClient wc;
            string    html;

            try
            {
                wc   = new WebClient();
                html = await wc.DownloadStringTaskAsync(_uris[UriType.Random]);
            }
            catch (WebException ex)
            {
                throw new ServiceOrConnectionException("Could not load the page", ex);
            }

            IConfiguration   config   = Configuration.Default;
            IBrowsingContext context  = BrowsingContext.New(config);
            IDocument        document = await context.OpenAsync(req => req.Content(html).Address(_baseUrl));

            IElement picDiv = document.DocumentElement
                              .QuerySelectorAll("#wrapper-wrap .left .ob-left-box-images")[RandomNthChild()];

            IHtmlImageElement img = (IHtmlImageElement)picDiv
                                    .QuerySelector(".left-wrap a img:last-child");
            IHtmlAnchorElement a = (IHtmlAnchorElement)picDiv
                                   .QuerySelector("h2 a");
            IHtmlInputElement input = (IHtmlInputElement)picDiv
                                      .QuerySelector(".left-wrap input[type=\"hidden\"]");
            IHtmlSourceElement src = (IHtmlSourceElement)picDiv
                                     .QuerySelector(".left-wrap video > source");

            if ((src == null && img == null) || a == null)
            {
                throw new NotFoundException(
                          "Either \"img\", \"source\" or \"a\" tag could not be found");
            }

            MemeInfo meme;

            if (src != null)
            {
                meme = new MemeInfo
                {
                    ViewURI = a.Href,
                    URI     = src.Source,
                    Alt     = string.Empty,
                    Name    = a.TextContent,
                    Type    = MediaType.Video
                };
            }
            else if (input != null)
            {
                meme = new MemeInfo
                {
                    ViewURI = a.Href,
                    URI     = input.Value,
                    Alt     = img.AlternativeText,
                    Name    = a.TextContent,
                    Type    = MediaType.Gif
                };
            }
            else
            {
                meme = new MemeInfo
                {
                    ViewURI = a.Href,
                    URI     = img.Source,
                    Alt     = img.AlternativeText,
                    Name    = a.TextContent,
                    Type    = MediaType.Image
                };
            }

            return(meme);
        }
示例#12
0
        public override async Task <MemeInfo> RandomAsync()
        {
            PseudoRandomImage image = GetPseudoRandomImage();

            WebClient wc;
            string    html;

            try
            {
                wc   = new WebClient();
                html = await wc.DownloadStringTaskAsync(image.Site);
            }
            catch (WebException ex)
            {
                throw new ServiceOrConnectionException("Could not load the page", ex);
            }

            IConfiguration   config   = Configuration.Default;
            IBrowsingContext context  = BrowsingContext.New(config);
            IDocument        document = await context.OpenAsync(req => req.Content(html).Address(_baseUrl));

            IElement picDiv = document.DocumentElement.QuerySelectorAll(
                "#content-container article .article-content")[image.NthChild];

            IHtmlImageElement img = (IHtmlImageElement)picDiv
                                    .QuerySelector(".article-image img");
            IHtmlAnchorElement a = (IHtmlAnchorElement)picDiv
                                   .QuerySelector(".article-title a");
            IHtmlSourceElement src = (IHtmlSourceElement)picDiv
                                     .QuerySelector(".article-image video > source");

            if ((img == null && src == null) || a == null)
            {
                throw new NotFoundException(
                          "Either \"img\", \"source\" or \"a\" tag could not be found");
            }

            MemeInfo meme;

            if (src != null)
            {
                meme = new MemeInfo
                {
                    ViewURI = a.Href,
                    URI     = src.Source,
                    Alt     = string.Empty,
                    Name    = a.TextContent.Trim(),
                    Type    = MediaType.Video
                };
            }
            else
            {
                meme = new MemeInfo
                {
                    ViewURI = a.Href,
                    URI     = img.Source,
                    Alt     = img.AlternativeText,
                    Name    = a.TextContent.Trim(),
                    Type    = MediaType.Image
                };
            }

            return(meme);
        }
 public static void SetMemeToCache(MemeInfo meme)
 {
     HttpRuntime.Cache[$"meme{meme.Id}"] = meme;
 }