Ejemplo n.º 1
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.º 2
0
        public void CastTest()
        {
            var webAnswer2 = WebAnswer.Cast(new Uri(@"http://localhost"), "Test Answer");

            Assert.AreEqual("Test Answer", webAnswer2.Answer);
            Assert.AreEqual("localhost", webAnswer2.Url.Host);
        }
Ejemplo n.º 3
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.º 4
0
    public DataTable GetAnswers(string strToken, int intQuestionID)
    {
        DataTable Result = new DataTable();

        if (ValidAuth(strToken) == true)
        {
            var AllAnswers = new WebAnswer();
            AllAnswers.QuestionID = intQuestionID;
            Result = AllAnswers.SelectAllAnswers();
        }

        return(Result);
    }
Ejemplo n.º 5
0
    public List <WebAnswer> GetAnswersList(string strToken, int intQuestionID)
    {
        var AllAnswers = new WebAnswer();
        List <WebAnswer> ListOfAnswers = new List <WebAnswer>();

        if (ValidAuth(strToken) == true)
        {
            AllAnswers.QuestionID = intQuestionID;

            ListOfAnswers = AllAnswers.SelectAllAnswersList();
        }

        return(ListOfAnswers);
    }
Ejemplo n.º 6
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.º 7
0
        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());
        }
Ejemplo n.º 8
0
    public List <WebAnswer> SelectAllAnswersList()
    {
        List <WebAnswer> ReturnList = new List <WebAnswer>();

        DataTable ListOfQuestions = new DataTable();

        ListOfQuestions = SelectAllAnswers();

        foreach (DataRow x in ListOfQuestions.Rows)
        {
            WebAnswer NewAnswer = new WebAnswer();

            NewAnswer.AnswerID          = Convert.ToInt32(x["pkAnswer_ID"]);
            NewAnswer.AnswerName        = Convert.ToString(x["Answer_Name"]);
            NewAnswer.AnswerDescription = Convert.ToString(x["Answer_Description"]);
            NewAnswer.AnswerOrder       = Convert.ToInt32(x["Answer_Order"]);
            NewAnswer.QuestionID        = Convert.ToInt32(x["fkQuestion_ID"]);

            ReturnList.Add(NewAnswer);
        }

        return(ReturnList);
    }
Ejemplo n.º 9
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));
         }
 }