Пример #1
0
        public byte[] GetImage()
        {
            try
            {
                var fileHash = HttpProxy.GetQueryString(ImageProcessor.FILE_HASH);
                var type     = HttpProxy.GetQueryString("t");

                if (string.IsNullOrEmpty(fileHash))
                {
                    HttpProxy.SetResponse(400);
                }
                if (string.IsNullOrEmpty(type))
                {
                    type = "s";
                }
                var imageType = ImageProcessor.IMAGE_S_BASE64;
                switch (type.Trim().ToLower())
                {
                case "l":
                    imageType = ImageProcessor.IMAGE_L_BASE64;
                    break;

                case "m":
                    imageType = ImageProcessor.IMAGE_M_BASE64;
                    break;
                }

                JObject filter = new JObject();
                filter[ImageProcessor.FILE_HASH] = fileHash;
                var data = DBProxy.Get(ImageProcessor.MYPHOTO_COLLECTION, filter.ToString(), new List <string> {
                    imageType
                });
                if (data.Count == 0)
                {
                    Logger.Error("Data not found");
                    HttpProxy.SetResponse(404);
                    return(null);
                }
                else
                {
                    var base64 = string.Empty;
                    if (data[0][imageType] != null)
                    {
                        base64 = data[0][imageType].ToString();
                    }
                    else
                    {
                        try
                        {
                            base64 = KeyValueStorage.Get <string>(ImageProcessor.IMAGE_KEY_VALUE_BUCKET, ImageProcessor.GetFileKey(imageType, fileHash));
                        }
                        catch (KeyNotFoundException ex)
                        {
                            Logger.Error(ex.Message, ex);
                        }
                    }
                    if (string.IsNullOrEmpty(base64))
                    {
                        HttpProxy.SetResponse(404);
                        return(null);
                    }
                    HttpProxy.ResponseHeaders["Cache-Control"] = "public, max-age=172800";
                    return(System.Convert.FromBase64String(base64));
                }
            }
            catch (Exception ex)
            {
                HttpProxy.SetResponse(500);
                Logger.Error(ex.Message, ex);
                return(null);
            }
        }