Пример #1
0
        public void TestScale()
        {
            var b = new Bitmap(Path.Combine(ImagesDir, "test.jpg"));

            Assert.AreEqual(500, b.Width);
            Assert.AreEqual(740, b.Height);

            var expected = new[] {
                new{ maxW = (int?)500, maxH = (int?)740, crop = false, strech = false, w = 500, h = 740 },
                new{ maxW = (int?)600, maxH = (int?)null, crop = false, strech = false, w = 500, h = 740 },
                new{ maxW = (int?)600, maxH = (int?)null, crop = false, strech = true, w = 600, h = 888 },
                new{ maxW = (int?)100, maxH = (int?)100, crop = true, strech = false, w = 100, h = 100 },
                new{ maxW = (int?)100, maxH = (int?)100, crop = false, strech = false, w = 68, h = 100 },
                new{ maxW = (int?)1000, maxH = (int?)500, crop = false, strech = true, w = 338, h = 500 },
                new{ maxW = (int?)500, maxH = (int?)1000, crop = true, strech = true, w = 500, h = 1000 },
            };

            foreach (var o in expected)
            {
                String format = new object[] { o.maxW, 'x', o.maxH, o.crop ? "crop" : null, o.strech ? "stretch" : null }
                .Select(p => p == null ? "" : p.ToString())
                .Join("");

                var result = ImgUtil.Scale(b, o.maxW, o.maxH, o.crop, o.strech);
                Assert.AreEqual(o.w, result.Width, format);
                Assert.AreEqual(o.h, result.Height, format);

                //Save result for a visual check
                ImgUtil.Compress(result, Path.Combine(ResultDir, format + ".png"), ImageFormat.Png, 100);
            }
        }
Пример #2
0
            public void OnRequest(HttpRequestHead request, IDataProducer requestBody,
                                  IHttpResponseDelegate response)
            {
                //auth
                if (Password != null && !request.Headers.Any(kv => (kv.Value ?? "").Contains(Password)))
                {
                    if (!request.Uri.Contains(Password))
                    {
                        ReplyText(response, "Pas d'accord !", false, null);
                        return;
                    }
                    Redirect(response, request.Uri, new Dictionary <string, string> {
                        { "Set-Cookie", "auth=" + Password + "; path=/" }
                    });
                    return;
                }

                try
                {
                    log.Info(new[] { request.Method, request.Uri }.Join("\t"));

                    String url   = request.Uri;
                    var    parts = url.Split(new[] { '?' }, 2);
                    String path  = HttpUtility.UrlDecode(parts[0]);

                    if (path.EndsWith("/"))
                    {
                        path += "index.htm";
                    }

                    if (ReplyFile(response, QueryPathToFile(path)))
                    {
                        return;
                    }

                    var o = HttpUtility.ParseQueryString(parts.GetOrDefault(1) ?? "");
                    if (path == "/*movies")
                    {
                        ReplyJson(response, o, DM.Instance.GetJson());
                        return;
                    }

                    if (path == "/*play")
                    {
                        if (DM.Instance.PlayFile(o["f"]))
                        {
                            ReplyJson(response, o, "{success:true}");
                            return;
                        }
                    }

                    if (path == "/*searchImdb")
                    {
                        var q = o["q"];
                        if (q.IsNullOrEmpty())
                        {
                            var g = Scanner.ParseMovieName(o["f"]);
                            q = g.GuessedTitle + " " + g.GuessedYear;
                        }

                        var results = new IMDBClient().Find(q);
                        ReplyJson(response, o, new Serializer(typeof(SearchImdb)).Serialize(new SearchImdb(q, results)));
                        return;
                    }

                    if (path == "/*setMatch")
                    {
                        var id   = o["id"];
                        var file = o["f"];
                        if (!id.IsNullOrEmpty() || !file.IsNullOrEmpty())
                        {
                            if (id.IsNullOrEmpty())
                            {
                                DM.Instance.AddUnmatched(file);
                            }
                            else if (file.IsNullOrEmpty())
                            {
                                DM.Instance.UnmatchMovie(id);
                            }
                            else
                            {
                                DM.Instance.AddMovie(Scanner.FetchMovie(file, id, true));
                            }
                        }

                        ReplyJson(response, o, DM.Instance.GetJson());
                        return;
                    }

                    if (path == "/*setTag")
                    {
                        var  id  = o["id"];
                        var  tag = o["tag"];
                        bool del = o["del"] == "1";

                        DM.Instance.SetTag(tag, id, del);

                        ReplyJson(response, o, DM.Instance.GetJson());
                        return;
                    }

                    //TODO: resize image in a thread
                    if (path.StartsWith("/*cover/"))
                    {
                        var p = path.Split('/', 4);
                        if (p.Length != 4)
                        {
                            throw new Exception("invalid scale request");
                        }

                        var format = RegScaleFormat.Match(p[2]);
                        if (!format.Success)
                        {
                            throw new Exception("Invalid format");
                        }

                        String img = Path.Combine(DM.CoverDir, p[3]);
                        if (!File.Exists(img))
                        {
                            img = QueryPathToFile("img/nocover.jpg");
                        }
                        if (!File.Exists(img))
                        {
                            throw new Exception("Image not found " + img);
                        }

                        String dir = Path.Combine(ScaledDir, format.Groups[0].Value);
                        Directory.CreateDirectory(dir);
                        String scaledPath = Path.Combine(dir, Path.GetFileName(img));

                        if (!File.Exists(scaledPath))
                        {
                            using (var b = new Bitmap(img))
                            {
                                String w     = format.Groups[1].Value;
                                String h     = format.Groups[2].Value;
                                String flags = format.Groups[3].Value;
                                using (var scaled = ImgUtil.Scale(b,
                                                                  w.IsNullOrEmpty() ? (int?)null : int.Parse(w),
                                                                  h.IsNullOrEmpty() ? (int?)null : int.Parse(h),
                                                                  flags.Contains('c'),
                                                                  flags.Contains('s')))
                                {
                                    ImgUtil.Compress(scaled, scaledPath, ImageFormat.Jpeg, 90);
                                }
                            }
                        }
                        if (ReplyFile(response, scaledPath))
                        {
                            return;
                        }
                    }

                    ReplyText(response, "The resource you requested ('" + path + "') could not be found.", false, "404 Not Found");
                }
                catch (Exception e)
                {
                    log.Error(e.Message);
                    ReplyText(response, e.Message + "\n\n" + e.StackTrace, false, "500 Internal Server Error");
                }
            }