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)); }
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); }
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); }
void FilterList(WebData webData, ITable table, string nameField, string guidField, string text) { var ids = new Set <long>(); if (text == null) { ids.IncludeRange(table.FindRows(Search.None, ResultOption.Limit(20))); } else { ids.AddRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text(text + "%")) & Search.FieldNotEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Group(guidField) + ResultOption.Limit(20))); if (ids.Count < 20) { ids.IncludeRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text("% " + text + "%")) & Search.FieldNotEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Group(guidField) + ResultOption.Limit(20))); } if (ids.Count < 20) { ids.IncludeRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text(text + "%")) & Search.FieldEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Limit(20 - ids.Count))); } if (ids.Count < 20) { ids.IncludeRange(table.FindRows(Search.FieldLike(nameField, MDBSearch.Text("% " + text + "%")) & Search.FieldEquals(guidField, null), ResultOption.SortAscending(nameField) + ResultOption.Group(nameField) + ResultOption.Limit(20 - ids.Count))); } } var json = new JsonWriter(); json.BeginArray("results"); if (ids.Count > 0) { //get items var values = table.GetValues <string>(nameField, false, ids.SubRange(0, Math.Min(20, ids.Count))); foreach (var value in values) { json.BeginObject(); json.String("id", value); json.String("text", value); json.EndObject(); } } json.EndArray(); var message = WebMessage.Create(webData.Method, $"Filter {nameField} {text}"); webData.Answer = WebAnswer.Json(webData.Request, message, json.ToString()); }
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)); } }
public void AvatarTest(WebData data, long?color = null, long?nose = null, long?eyes = null, long?mouth = null, long?face = null, long?rotate = null) { if (!color.HasValue) { color = DefaultRNG.UInt16 & 0xFF; } if (!nose.HasValue) { nose = DefaultRNG.UInt16 & 0x1F; } if (!eyes.HasValue) { eyes = DefaultRNG.UInt16 & 0x1F; } if (!mouth.HasValue) { mouth = DefaultRNG.UInt16 & 0x1F; } if (!face.HasValue) { face = DefaultRNG.UInt16 & 0x1F; } if (!rotate.HasValue) { rotate = DefaultRNG.UInt16 & 0x0F; } long id = (color.Value & 0xFF) | (nose.Value & 0x1F) << 8 | (eyes.Value & 0x1F) << 13 | (mouth.Value & 0x1F) << 18 | (face.Value & 0x1F) << 23 | (rotate.Value & 0x0F) << 28; var html = new HtmlPageBuilder(data.Request); html.Breadcrump.Add(new WebLink() { Link = "/avatar/get?id=" + id, Text = "Avatar " + id }); html.Content.CardOpen(Bootstrap4.GetLink("Avatar ID " + id, "/Avatar?id=" + id)); html.Content.Image("/avatar/get?id=" + id, "img-fluid"); html.Content.ListGroupOpen(); html.Content.ListGroupItemOpen(); html.Content.Text("Color: " + color); html.Content.ListGroupItemClose(); html.Content.ListGroupItemOpen(); html.Content.Text("Nose: " + nose); html.Content.ListGroupItemClose(); html.Content.ListGroupItemOpen(); html.Content.Text("Eyes: " + eyes); html.Content.ListGroupItemClose(); html.Content.ListGroupItemOpen(); html.Content.Text("Mouth: " + mouth); html.Content.ListGroupItemClose(); html.Content.ListGroupItemOpen(); html.Content.Text("Face: " + face); html.Content.ListGroupItemClose(); html.Content.ListGroupItemOpen(); html.Content.Text("Rotate: " + rotate); html.Content.ListGroupItemClose(); html.Content.ListGroupClose(); html.Content.CardClose(); var msg = WebMessage.Create(data.Method, "Created avatar."); data.Answer = html.ToAnswer(msg); data.Answer.SetCacheTime(TimeSpan.FromDays(1)); }