示例#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);
            }
        }
示例#2
0
文件: DataContext.cs 项目: vebin/BD2
        public DataContext(Frontend frontend, byte[] id, DatabasePath path)
            : base(frontend)
        {
            Log.WriteLine ("DataContext-{0}..ctor(frontend={1}, id={2}, path={3})", id.ToHexadecimal (), frontend.ToString (), id.ToHexadecimal (), path);
            this.id = id;
            this.path = path;
            KeyValueStorageConfiguration kvscPrecomputedQueries = new KeyValueStorageConfiguration ("PrecomputedQueries", "Sqlite");
            KeyValueStorageConfiguration kvscMeta = new KeyValueStorageConfiguration ("Meta", "Sqlite");
            KeyValueStorageConfiguration kvscTemporary = new KeyValueStorageConfiguration ("Temporary", "Sqlite");
            precomputedQueries = kvscPrecomputedQueries.OpenStorage<byte[]> (path);
            meta = kvscMeta.OpenStorage<byte[]> (path);
            temporary = kvscTemporary.OpenStorage<byte[]> (path);
            try {
                byte[] cid = meta.Get ("ID".SHA256 ());
                if (cid == null) {
                    meta.Put ("ID".SHA256 (), id);
                    meta.Put ("Solid".SHA256 (), new byte[] { 0 });
                } else {
                    if (ByteSequenceComparer.Shared.Compare (cid, id) != 0)
                        throw new InvalidDataException ("Wrong ID");//should i even do this?
                }
            } catch {
                meta.Put ("ID".SHA256 (), id);
                meta.Put ("Solid".SHA256 (), new byte[] { 0 });
            }
            foreach (var t in frontend.GetTables ())
                perTableRows.Add (t, new SortedDictionary<byte[], Row> (ByteSequenceComparer.Shared));
            foreach (var kvp in temporary) {
                byte[] objectID = kvp.Key;
                //BaseDataObject bdo = new BaseDataObject (this.Frontend, objectID);
                System.IO.MemoryStream ms = new MemoryStream (kvp.Value, 16, kvp.Value.Length - 16);
                byte[] GuidBytes = new byte[16];
                System.Buffer.BlockCopy (kvp.Value, 0, GuidBytes, 0, 16);
                BaseDataObjectVersion bdov = BaseDataObjectTypeIdAttribute.GetAttribFor (new Guid (GuidBytes)).Deserialize (this.Frontend, null, ms.ToArray ());
                var row = bdov as Row;
                if (!references.ContainsKey (bdov.ID)) {
                    if (row != null) {
                        perTableRows [row.Table].Add (bdov.ID, row);
                    }
                    objects.Add (bdov.ID, row);
                }
                foreach (var r in bdov.ReplacingIDs) {
                    if (!references.ContainsKey (r)) {
                        references.Add (r, new SortedSet<byte[]> (ByteSequenceComparer.Shared));
                    }
                    references [r].Add (bdov.ID);
                }

            }
            //this.frontendInstanceBase = (FrontendInstance)frontendInstanceBase;
        }