コード例 #1
0
        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."));
        }
コード例 #2
0
        static HttpResult ResizeHelper(ImageRequest request, Image img, int[] sizes, Func <int, string> x)
        {
            foreach (var h in sizes)
            {
                string tmp = x(h);
                if (request.Param.Contains(tmp))
                {
                    string extension = null;
                    string mimetype  = null;
                    string end       = DownSite.Constants.Seperator + tmp;
                    if (img.MimeType.StartsWith("video"))
                    {
                        extension = "mp4";
                        mimetype  = "video/mp4";
                    }
                    else if (img.MimeType.StartsWith("image"))
                    {
                        extension = "jpg";
                        mimetype  = "image/jpg";
                    }

                    if (extension != null)
                    {
                        var file = FileCache.GetFile(img.Id + end + "." + extension);
                        if (file != null)
                        {
                            return(new HttpResult(file, mimetype)
                            {
                            });
                        }
                    }
                }
            }

            return(null);
        }
コード例 #3
0
        public object Get(ImageRequest request)
        {
            var img = Image.Load(request.Id);

            if (img != null)
            {
                bool thumb3 = request.Param.Contains("thumb");

                if (!thumb3)
                {
                    var res = ResizeHelper(request, img.Item1, ResizeHeights, x => "0x" + x);
                    if (res != null)
                    {
                        return(res);
                    }
                    res = ResizeHelper(request, img.Item1, ResizeWidths, x => x + "x0");
                    if (res != null)
                    {
                        return(res);
                    }
                }

                if (thumb3)
                {
                    string filename_without_extension = Path.GetFileNameWithoutExtension(img.Item2.Name);
                    string thumb      = filename_without_extension + DownSite.Constants.Seperator + "thumb.jpg";
                    var    thumb_file = FileCache.GetFile(thumb);
                    if (thumb_file != null)
                    {
                        return(new HttpResult(thumb_file, MimeTypes.ImageJpg)
                        {
                        });
                    }

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

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

                    if (!mimetypes.Contains(img.Item1.MimeType))
                    {
                        if (VideoThumbnailer.MakeThumbnail(img.Item2.FullName, thumb_file.FullName))
                        {
                            return(new HttpResult(thumb_file, MimeTypes.ImageJpg)
                            {
                            });
                        }
                        else
                        {
                            return(new HttpResult(System.Net.HttpStatusCode.NotFound, string.Format("No thumbnails for type {0}.", img.Item1.MimeType)));
                        }
                    }
                    else
                    {
                        using (var fs = img.Item2.OpenRead())
                        {
                            using (Bitmap bmp = new Bitmap(fs))
                            {
                                using (var thumb2 = bmp.GetThumbnailImage(80, 80, null, IntPtr.Zero))
                                {
                                    SaveJpeg(thumb2, thumb_file.FullName);
                                    return(new HttpResult(thumb_file, MimeTypes.ImageJpg)
                                    {
                                    });
                                }
                            }
                        }
                    }
                }
                else
                {
                    var res = new HttpResult(img.Item2, string.IsNullOrWhiteSpace(img.Item1.MimeType) ? MimeTypes.ImageJpg : img.Item1.MimeType)
                    {
                    };
                    if (img.Item1.MimeType == "application/octet-stream")
                    {
                        res.Options.Add("Content-Disposition", "attachment; filename=\"" + img.Item1.FileName + "\"");
                    }
                    return(res);
                }
            }
            return(new HttpResult(System.Net.HttpStatusCode.NotFound, "No image with that ID."));
        }