Пример #1
0
        public void AtomNewImageTest()
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/DBlog/AtomImage.aspx");

            request.Method      = "POST";
            request.ContentType = "image/jpg";
            string usernamePassword   = string.Format("Administrator:");
            string basicAuthorization = string.Format("Basic {0}", Convert.ToBase64String(
                                                          Encoding.ASCII.GetBytes(usernamePassword)));

            request.Headers.Add("Authorization", basicAuthorization);
            byte[] postData = ThumbnailBitmap.GetBitmapDataFromText("x", 72, 100, 150);
            request.ContentLength = postData.Length;
            request.GetRequestStream().Write(postData, 0, postData.Length);
            request.GetRequestStream().Close();
            HttpWebResponse response         = (HttpWebResponse)request.GetResponse();
            string          responseLocation = response.Headers["Location"];

            Assert.IsNotEmpty(responseLocation);
            Console.WriteLine(responseLocation);
            // atom entry returned
            AtomEntry atomEntry = new AtomEntry();

            atomEntry.Load(response.GetResponseStream());
            Console.WriteLine(atomEntry.Id.Uri);
            int id = int.Parse(atomEntry.Id.Uri.ToString().Substring(atomEntry.Id.Uri.ToString().LastIndexOf("/") + 1));

            Assert.IsTrue(id > 0);
            Console.WriteLine(string.Format("Id: {0}", id));
            Blog.DeleteImage(Ticket, id);
        }
Пример #2
0
        public void AtomUpdateImageTest()
        {
            // create image
            TransitImage t_image = new TransitImage();

            t_image.Data = ThumbnailBitmap.GetBitmapDataFromText("x", 72, 100, 150);
            t_image.Name = Guid.NewGuid().ToString();
            t_image.Id   = Blog.CreateOrUpdateImage(Ticket, t_image);
            // update image
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
                string.Format("http://localhost/DBlog/AtomImage.aspx?id={0}", t_image.Id));

            request.Method      = "PUT";
            request.ContentType = "image/jpg";
            string usernamePassword   = string.Format("Administrator:");
            string basicAuthorization = string.Format("Basic {0}", Convert.ToBase64String(
                                                          Encoding.ASCII.GetBytes(usernamePassword)));

            request.Headers.Add("Authorization", basicAuthorization);
            request.ContentLength = t_image.Data.Length;
            request.GetRequestStream().Write(t_image.Data, 0, t_image.Data.Length);
            request.GetRequestStream().Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Assert.IsTrue(response.StatusCode == HttpStatusCode.OK, string.Format("Response code was {0}", response.StatusCode));
            Blog.DeleteImage(Ticket, t_image.Id);
        }
Пример #3
0
    public override PicturePage.TransitPicture GetRandomPictureWithThumbnail()
    {
        SnCore.Services.TransitPicture p = SessionManager.GetInstance <SnCore.Services.TransitPicture, string>(
            "AccountWebsite", SessionManager.ObjectService.GetRandomPictureByType);

        if (p == null || p.Bitmap == null)
        {
            p = new SnCore.Services.TransitPicture();
            const string s_Unavailable = "Thumbnail\nQueued";
            // flip the bitmaps horizontally
            p.Bitmap    = ThumbnailBitmap.GetBitmapDataFromText(s_Unavailable, 10, ThumbnailBitmap.s_FullSize.Height, ThumbnailBitmap.s_FullSize.Width);
            p.Thumbnail = ThumbnailBitmap.GetBitmapDataFromText(s_Unavailable, 10, ThumbnailBitmap.s_ThumbnailSize.Height, ThumbnailBitmap.s_ThumbnailSize.Width);
            p.Created   = p.Modified = DateTime.UtcNow;
        }

        TransitPicture result = new TransitPicture();

        result.Id       = p.Id;
        result.Bitmap   = p.Thumbnail;
        result.Created  = p.Created;
        result.Modified = p.Modified;
        result.Name     = p.Name;
        return(result);
    }
Пример #4
0
 public static byte[] GetNewBitmap()
 {
     return(ThumbnailBitmap.GetBitmapDataFromText(GetNewString(), 12, 240, 100));
 }
    public override TransitPicture GetPictureWithBitmap(int id)
    {
        string url = Request["Src"];

        if (string.IsNullOrEmpty(url))
        {
            throw new Exception("Invalid url.");
        }

        string         key    = string.Format("AccountFeedItemPicture:{0}", url.GetHashCode());
        TransitPicture result = (TransitPicture)Cache[key];

        if (result != null)
        {
            return(result);
        }

        // fetch the image to get its size
        WebClient client = new WebClient();

        client.Headers["Accept"]     = "*/*";
        client.Headers["User-Agent"] = SessionManager.GetCachedConfiguration("SnCore.Web.UserAgent", "SnCore/1.0");
        result      = new TransitPicture();
        result.Name = url;

        try
        {
            byte[] data = client.DownloadData(url);

            if (data == null)
            {
                throw new Exception("Missing file data.");
            }

            result.Bitmap = new ThumbnailBitmap(new MemoryStream(data), new Size(0, 0),
                                                ThumbnailBitmap.s_FullSize, ThumbnailBitmap.s_ThumbnailSize).Bitmap;

            string created = (string)client.ResponseHeaders["Created"];
            if (string.IsNullOrEmpty(created))
            {
                created = (string)client.ResponseHeaders["Date"];
            }
            if (string.IsNullOrEmpty(created) || !DateTime.TryParse(created, out result.Created))
            {
                result.Created = DateTime.Now;
            }

            string modified = (string)client.ResponseHeaders["Modified"];
            if (string.IsNullOrEmpty(modified) || !DateTime.TryParse(modified, out result.Modified))
            {
                result.Modified = DateTime.Now;
            }
        }
        catch (Exception ex)
        {
            string message = string.Format("This image cannot be displayed.\n{0}\n{1}",
                                           ex.Message, url);
            result.Bitmap   = ThumbnailBitmap.GetBitmapDataFromText(message, 8, 0, 0);
            result.Modified = result.Created = DateTime.Now;
        }

        Cache.Insert(key, result, null, Cache.NoAbsoluteExpiration,
                     SessionManager.DefaultCacheTimeSpan);

        return(result);
    }
Пример #6
0
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Nullable <DateTime> ims = IfModifiedSince;

                if (ims.HasValue)
                {
                    if (ims.Value.ToUniversalTime().AddSeconds(CacheDuration) > DateTime.UtcNow)
                    {
                        Response.StatusCode = 304;
                        return;
                    }
                }

                Picture p = null;

                switch (PageType)
                {
                case PicturePageType.Thumbnail:

                    if (RequestId == 0)
                    {
                        p = GetRandomPictureWithThumbnail();

                        if (p == null)
                        {
                            p         = new Picture();
                            p.Id      = 0;
                            p.Created = p.Modified = DateTime.Now;
                            p.Name    = Guid.NewGuid().ToString();
                            p.Bitmap  = ThumbnailBitmap.GetBitmapDataFromText("?", 72, 100, 150);
                        }
                    }
                    else
                    {
                        p = ims.HasValue ?
                            GetPictureWithThumbnail(RequestId, ims.Value) :
                            GetPictureWithThumbnail(RequestId);
                    }

                    break;

                case PicturePageType.Bitmap:

                    p = ims.HasValue ?
                        GetPictureWithBitmap(RequestId, ims.Value) :
                        GetPictureWithBitmap(RequestId);

                    break;
                }

                if (p == null)
                {
                    Response.StatusCode = (ims.HasValue ? 304 : 404);
                    return;
                }

                if (p.Bitmap == null)
                {
                    Response.Redirect("./images/site/access.gif", true);
                    return;
                }

                Response.Cache.SetLastModified(p.Modified.ToLocalTime());
                Response.Cache.SetCacheability(HttpCacheability.Private);

                p.Name = (string.IsNullOrEmpty(p.Name)) ? p.Id.ToString() + ".jpg" : p.Id.ToString() + "-" + p.Name;

                switch (PageType)
                {
                case PicturePageType.Thumbnail:
                    p.Name.Insert(0, "thumbnail-");
                    break;

                case PicturePageType.Bitmap:
                    if (!string.IsNullOrEmpty(Copyright))
                    {
                        ThumbnailBitmap bitmap = new ThumbnailBitmap(p.Bitmap);
                        bitmap.AddCopyright(Copyright);
                        p.Bitmap = bitmap.Bitmap;
                    }
                    break;
                }

                Response.ContentType = "image/" + Path.GetExtension(p.Name).TrimStart(".".ToCharArray());
                Response.AddHeader("Content-disposition", "attachment; filename=" + p.Name);
                Response.AddHeader("Created", p.Created.ToString("r"));
                Response.AddHeader("Modified", p.Modified.ToString("r"));
                Response.BinaryWrite(p.Bitmap);
                Response.End();
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
Пример #7
0
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Nullable <Size>     resize = Size;
            Nullable <DateTime> ims    = IfModifiedSince;

            if (ims.HasValue)
            {
                if (ims.Value.ToUniversalTime().AddSeconds(CacheDuration) > DateTime.UtcNow)
                {
                    Response.StatusCode = 304;
                    return;
                }
            }

            TransitPicture p = null;

            switch (PageType)
            {
            case PicturePageType.Thumbnail:

                if (RequestId == 0)
                {
                    p = GetRandomPictureWithThumbnail();

                    if (p == null)
                    {
                        p         = new TransitPicture();
                        p.Id      = 0;
                        p.Created = p.Modified = DateTime.Now;
                        p.Name    = Guid.NewGuid().ToString();
                        if (!resize.HasValue)
                        {
                            resize = new Size(100, 150);
                        }
                        p.Bitmap = ThumbnailBitmap.GetBitmapDataFromText("?", 72,
                                                                         resize.Value.Width, resize.Value.Height);
                    }
                }
                else
                {
                    p = ims.HasValue ?
                        GetPictureWithThumbnail(RequestId, ims.Value) :
                        GetPictureWithThumbnail(RequestId);
                }

                break;

            case PicturePageType.Bitmap:

                p = ims.HasValue ?
                    GetPictureWithBitmap(RequestId, ims.Value) :
                    GetPictureWithBitmap(RequestId);

                break;
            }

            if (p == null)
            {
                if (ims.HasValue)
                {
                    Response.StatusCode = 304;
                    return;
                }
                else
                {
                    throw new Exception("Invalid picture id.");
                }
            }

            Response.Cache.SetLastModified(p.Modified.ToLocalTime());
            Response.Cache.SetCacheability(HttpCacheability.Private);

            p.Name = (string.IsNullOrEmpty(p.Name)) ? p.Id.ToString() + ".jpg" : p.Id.ToString() + "-" + p.Name;

            if (resize.HasValue)
            {
                ThumbnailBitmap resized = new ThumbnailBitmap(new MemoryStream(p.Bitmap), new Size(0, 0),
                                                              resize.Value, resize.Value);

                switch (PageType)
                {
                case PicturePageType.Bitmap:
                    p.Bitmap = resized.Bitmap;
                    break;

                case PicturePageType.Thumbnail:
                    p.Bitmap = resized.Thumbnail;
                    break;
                }
            }

            switch (PageType)
            {
            case PicturePageType.Thumbnail:
                p.Name.Insert(0, "thumbnail-");
                break;
            }

            Response.ContentType = "image/" + Path.GetExtension(p.Name).TrimStart(".".ToCharArray());
            Response.AddHeader("Content-disposition", "attachment; filename=" + p.Name);
            Response.AddHeader("Created", p.Created.ToString("r"));
            Response.AddHeader("Modified", p.Modified.ToString("r"));
            Response.BinaryWrite(p.Bitmap);
            Response.End();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }