static void Thumb(Image img)
        {
            var    file = UploadService.GetFileInfo(img.Id);
            string filename_without_extension = Path.GetFileNameWithoutExtension(file.Name);
            string thumb      = filename_without_extension + DownSite.Constants.Seperator + "thumb.jpg";
            var    thumb_file = FileCache.GetFile(thumb);

            if (thumb_file != null)
            {
                return;
            }

            thumb_file = new FileInfo(Path.Combine(FileCache.GetCacheDir().FullName, thumb));

            var mimetypes = new string[] { MimeTypes.ImageJpg, MimeTypes.ImagePng, MimeTypes.ImageGif };

            if (!mimetypes.Contains(img.MimeType))
            {
                VideoThumbnailer.MakeThumbnail(file.FullName, thumb_file.FullName);
            }
            else
            {
                using (var fs = file.OpenRead())
                {
                    using (Bitmap bmp = new Bitmap(fs))
                    {
                        using (var thumb2 = bmp.GetThumbnailImage(80, 80, null, IntPtr.Zero))
                        {
                            ImageService.SaveJpeg(thumb2, thumb_file.FullName);
                        }
                    }
                }
            }
        }
        public object Delete(ImageRequest request)
        {
            Database.Db.Delete <Image>(x => x.Id == request.Id);

            FileInfo fi = UploadService.GetFileInfo(request.Id);

            if (fi != null)
            {
                fi.Delete();
            }

            DirectoryInfo di = FileCache.GetCacheDir();

            FileInfo[] cachefiles = di.GetFiles(request.Id.ToString().Replace("-", "") + "*");
            foreach (var f in cachefiles)
            {
                f.Delete();
            }
            return(new HttpResult(System.Net.HttpStatusCode.OK, "File deleted."));
        }
        public static Tuple <Image, FileInfo> Load(Guid id)
        {
            var img = Database.Db.Select <Image>(x => x.Id == id).SingleOrDefault();

            if (img == null)
            {
                return(null);
            }

            if (img.MimeType.StartsWith("video"))
            {
                ConvertVideo(img);
            }
            else
            {
                ConvertImage(img);
            }

            var s = UploadService.GetFileInfo(id);

            return(Tuple.Create(img, s));
        }
        public static void Save(Guid id, IDbConnection db, string mimetype, string filename, Stream s)
        {
            string extension = UploadService.MimeTypeExtension(mimetype);

            if (extension == null)
            {
                extension = Path.GetExtension(filename);
            }

            string path = UploadService.PutFile(id, extension, s);
            var    img  = new Image()
            {
                Id = id, MimeType = mimetype, FileName = filename
            };

            if (img.MimeType.StartsWith("video"))
            {
                var video = VideoProbe.Probe(path);
                if (video != null)
                {
                    var res = video.Resolution;
                    img.Width  = res.Width;
                    img.Height = res.Height;

                    ConvertVideo(img);
                }
            }
            else if (mimetype.StartsWith("image"))
            {
                using (var bmp = new Bitmap(path))
                {
                    img.Width  = bmp.Width;
                    img.Height = bmp.Height;
                    ConvertImage(img);
                }
            }
            db.Insert <Image>(img);
        }
        static void ConvertImage(Image img)
        {
            foreach (int w in ImageService.ResizeWidths)
            {
                if (img.Width > w)
                {
                    int h3 = (int)((double)img.Height / (double)img.Width * (double)w);
                    if (h3 % 2 == 1)
                    {
                        h3 -= 1;
                    }
                    var file = UploadService.GetFileInfo(img.Id);
                    if (file == null)
                    {
                        Console.WriteLine("???");
                        continue;
                    }

                    string output = Path.Combine(FileCache.GetCacheDir().FullName, img.Id + DownSite.Constants.Seperator + w + "x0" + ".jpg");

                    if (File.Exists(output))
                    {
                        continue;
                    }

                    int h2 = h3;
                    int w2 = w;
                    Console.WriteLine("converting image to " + w2 + "x" + h2);
                    using (var bmp = new Bitmap(file.FullName))
                    {
                        using (var resized = resizeImage(bmp, new Size(w2, h2)))
                        {
                            ImageService.SaveJpeg(resized, output);
                        }
                    }
                }
            }
        }
        static void ConvertVideo(Image img)
        {
            foreach (int h in ImageService.ResizeHeights)
            {
                if (img.Height > h)
                {
                    int w = (int)((double)img.Width / (double)img.Height * (double)h);
                    if (w % 2 == 1)
                    {
                        w -= 1;
                    }
                    var file = UploadService.GetFileInfo(img.Id);
                    if (file == null)
                    {
                        Console.WriteLine("???");
                        continue;
                    }

                    string output = Path.Combine(FileCache.GetCacheDir().FullName, img.Id + DownSite.Constants.Seperator + "0x" + h + ".mp4");

                    if (File.Exists(output))
                    {
                        continue;
                    }

                    if (ConvertQueue.Any(x => x.Output == output))
                    {
                        Console.WriteLine("already in convert queue: " + output);
                        continue;
                    }

                    Console.WriteLine("add to convert queue: " + output);
                    ConvertQueue.Add(new ConvertInfo(w, h, img, output, file));
                }
            }
        }