Ejemplo n.º 1
0
 public void GetImage(WebData webData, long imageID, bool thumb = false)
 {
     try
     {
         MDBImage img  = mdb.Images.TryGetStruct(imageID);
         MDBFile  file = mdb.Files.TryGetStruct(img.FileID);
         if (file.ID > 0)
         {
             string fullPath = file.GetFullPath(mdb);
             if (thumb)
             {
                 if (TryGetThumb(webData, img, fullPath))
                 {
                     return;
                 }
             }
             webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(fullPath, "/mdb/image/get"), File.ReadAllBytes(fullPath), img.MimeType);
             webData.Answer.AllowCompression = false;
             webData.Answer.SetCacheTime(TimeSpan.FromHours(1));
             return;
         }
     }
     catch { }
     webData.Result.AddMessage(webData.Method, WebError.NotFound, "Image {0} cannot be found!", imageID);
 }
Ejemplo n.º 2
0
        public void GetAlbumReplacementImage(WebData webData, long id, bool thumb)
        {
            if (id < 0 || id >= ReplaceAlbumImages.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(id));
            }
            var replacementImage = ReplaceAlbumImages[id];

            if (thumb)
            {
                webData.Answer = WebAnswer.Raw(
                    webData.Request,
                    WebMessage.Create(webData.Method, replacementImage.FileName),
                    replacementImage.ThumbData,
                    replacementImage.MimeType);
            }
            else
            {
                webData.Answer = WebAnswer.Raw(
                    webData.Request,
                    WebMessage.Create(webData.Method, replacementImage.FileName),
                    replacementImage.Data,
                    replacementImage.MimeType);
            }

            webData.Answer.AllowCompression = false;
            webData.Answer.Headers.Add("MDBReplacementImage", "NotAvailable");
            webData.Answer.SetCacheTime(TimeSpan.FromDays(1));
        }
Ejemplo n.º 3
0
 bool TryGetThumb(WebData webData, MDBImage img, string fileName)
 {
     try
     {
         //try to load thumb
         var    hash          = Base32.Safe.Encode(Hash.FromString(Hash.Type.SHA256, fileName));
         string thumbFileName = FileSystem.Combine(mdb.CacheFolder, "Thumbs", hash + ".jpg");
         var    mime          = MimeTypes.FromExtension(".jpg");
         if (File.Exists(thumbFileName))
         {
             try
             {
                 webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(webData.Method, thumbFileName), File.ReadAllBytes(thumbFileName), mime);
                 webData.Answer.SetCacheTime(TimeSpan.FromDays(1));
                 return(true);
             }
             catch { /*file access error, writing in progress ?, wait for lock and retry*/ }
         }
         //wait until last thumb generation is finished
         byte[] data;
         lock (ThumbCreateSyncRoot)
         {
             //do a second check after lock is released...
             if (File.Exists(thumbFileName))
             {
                 try
                 {
                     webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(webData.Method, thumbFileName), File.ReadAllBytes(thumbFileName), mime);
                     webData.Answer.SetCacheTime(TimeSpan.FromDays(1));
                     return(true);
                 }
                 catch { /*file access error, recreate thumb*/ }
             }
             //generate thumb
             using (var bmp = Bitmap32.FromFile(fileName))
             {
                 data = WebImage.RenderThumb(bmp, thumbFileName);
             }
         }
         webData.Answer = WebAnswer.Raw(webData.Request, WebMessage.Create(webData.Method, thumbFileName), data, mime);
         webData.Answer.AllowCompression = false;
         webData.Answer.SetCacheTime(TimeSpan.FromDays(1));
         return(true);
     }
     catch (Exception ex)
     {
         this.LogError(ex, "Could not load / create thumb for {0}", fileName);
     }
     return(false);
 }
Ejemplo n.º 4
0
 void Draw(WebData data, int color, int nose, int eyes, int mouth, int face, int rotate)
 {
     using (var stream = new MemoryStream())
         using (var bmp = new Bitmap32(AvatarSize, AvatarSize))
         {
             var faceColor = ARGB.FromHSI(color / 256.0f, 1, 1);
             DrawFace(face, faceColor, faces, bmp);
             DrawEyes(eyes, this.eyes, bmp);
             DrawMouth(mouth, mouths, bmp);
             DrawNose(nose, noses, bmp);
             using (var result = new Bitmap32(AvatarSize, AvatarSize))
             {
                 result.Draw(bmp, 0, 0, new Translation()
                 {
                     Rotation = ((rotate % 16) - 7) * 0.02f
                 });
                 result.Save(stream);
             }
             var msg = WebMessage.Create(data.Method, "Avatar created");
             data.Answer = WebAnswer.Raw(data.Request, msg, stream.GetBuffer(), "image/png");
             data.Answer.AllowCompression = false;
             data.Answer.SetCacheTime(TimeSpan.FromDays(1));
         }
 }