Esempio n. 1
0
        private static byte[] GetDatabaseBinaryAttribute(string id, DbBinaryConfig dbBinaryConfig)
        {
            IDMLIdentifiers         identifiers = null;
            IDatabaseAccessProvider dbAccessProvider;

            if (string.IsNullOrEmpty(dbBinaryConfig.DBConnection))
            {
                dbAccessProvider = DatabaseAccess.ForCurrentDatabase;
                identifiers      = DatabaseAccess.ForCurrentDatabase.DatabaseServices.DMLService.Identifiers;
            }
            else
            {
                dbAccessProvider = DatabaseAccess.ForDBConnection(dbBinaryConfig.DBConnection);
                identifiers      = DatabaseAccess.ForDBConnection(dbBinaryConfig.DBConnection).DatabaseServices.DMLService.Identifiers;
            }

            string selectSQL = "SELECT " + identifiers.EscapeIdentifier(dbBinaryConfig.Attribute.ToUpper()) + " FROM " +
                               dbBinaryConfig.EntityGetter(null, BuiltInFunction.GetCurrentLocale()) +
                               " WHERE " + identifiers.EscapeIdentifier(dbBinaryConfig.Id.ToUpper()) + " = ";

            if (dbBinaryConfig.IsAlphaId)
            {
                selectSQL += "'" + BuiltInFunction.EncodeSql(id) + "'";
            }
            else
            {
                selectSQL += BuiltInFunction.EncodeSql(id);
            }

            return(GetBinaryFromDb(selectSQL, dbBinaryConfig.Attribute, dbAccessProvider));
        }
Esempio n. 2
0
        private void Page_Load(object sender, System.EventArgs e)
        {
            HttpContext  current   = HttpContext.Current;
            HttpRequest  request   = current.Request;
            HttpResponse response  = current.Response;
            int          cacheTime = 0;

            if ((!string.Equals(HttpContext.Current.Request.HttpMethod, "GET", StringComparison.CurrentCultureIgnoreCase)) &&
                (!string.Equals(HttpContext.Current.Request.HttpMethod, "HEAD", StringComparison.CurrentCultureIgnoreCase)))
            {
                response.Clear();
                response.Status     = "405 Method Not Allowed";
                response.StatusCode = 405;
                response.AppendHeader("Allow", "GET, HEAD");
                response.StatusDescription = "Method Not Allowed";
                response.Write("Method Not Allowed");
                response.End();
                return;
            }

            string parameters = request.PathInfo;

            if (parameters.Substring(1).IndexOf("/") == parameters.Substring(1).LastIndexOf("/"))
            {
                parameters = "/" + RC4CryptHelper.DecryptWithFixedKey(parameters.Substring(1, parameters.LastIndexOf("/") - 1).Replace("-", "+").Replace("_", "/")) + parameters.Substring(parameters.LastIndexOf("/"));
            }
            else
            {
                parameters = "";
            }
            Match m = ParametersRegex.Match(parameters);

            if (!m.Success)
            {
                response.Clear();
                response.Status            = "400 Bad Request";
                response.StatusCode        = 400;
                response.StatusDescription = "Bad Request";
                response.Write("Bad Request");
                response.End();
                return;
            }

            _entity    = m.Groups["Entity"].Value.ToLower();
            _attribute = m.Groups["Attribute"].Value.ToLower();
            _id        = m.Groups["Id"].Value;
            _filename  = m.Groups["Filename"].Value.ToLower();

            object objt = htDbImgConfig[_entity + "/" + _attribute];

            if (objt == null)
            {
                NotFound(response);
                return;
            }
            DbImgConfig dbimgconfig;

            dbimgconfig = (DbImgConfig)objt;

            byte[] buffer  = new byte[0];
            string md5Hash = "";

            string reqcachecontrol = "";
            string reqpragma       = "";

            if (Request.Headers["Cache-Control"] != null)
            {
                reqcachecontrol = Request.Headers["Cache-Control"].ToString().Trim();
            }
            if (Request.Headers["Pragma"] != null)
            {
                reqpragma = Request.Headers["Pragma"].ToString().Trim();
            }
            bool refreshItem = ((reqcachecontrol == "no-cache") || (reqpragma == "no-cache") || (reqcachecontrol == "max-age=0"));

            object tmpHash = HttpRuntime.Cache.Get("DbImage/" + _entity + "/" + _attribute + "/" + _id + "/" + _filename);

            if ((!refreshItem) && (tmpHash != null))
            {
                DbImgCacheFile cacheinfo = (DbImgCacheFile)tmpHash;
                md5Hash = cacheinfo.md5Hash;
                buffer  = cacheinfo.buffer;
            }
            else
            {
                OutSystems.HubEdition.Extensibility.Data.DMLService.IDMLIdentifiers identifiers = null;
                IDatabaseAccessProvider dbAccessProvider;

                if (string.IsNullOrEmpty(dbimgconfig.DBConnection))
                {
                    dbAccessProvider = DatabaseAccess.ForCurrentDatabase;
                    identifiers      = DatabaseAccess.ForCurrentDatabase.DatabaseServices.DMLService.Identifiers;
                }
                else
                {
                    dbAccessProvider = DatabaseAccess.ForDBConnection(dbimgconfig.DBConnection);
                    identifiers      = DatabaseAccess.ForDBConnection(dbimgconfig.DBConnection).DatabaseServices.DMLService.Identifiers;
                }

                string select = "SELECT " + identifiers.EscapeIdentifier(dbimgconfig.Attribute.ToUpper()) + " FROM " +
                                dbimgconfig.EntityGetter(null, BuiltInFunction.GetCurrentLocale()) +
                                " WHERE " + identifiers.EscapeIdentifier(dbimgconfig.Id.ToUpper()) + " = ";
                if (dbimgconfig.IsAlphaId)
                {
                    select += "'" + BuiltInFunction.EncodeSql(_id) + "'";
                }
                else
                {
                    select += BuiltInFunction.EncodeSql(_id);
                }

                using (Transaction trans = dbAccessProvider.GetReadOnlyTransaction()) {
                    try {
                        Command cmd = trans.CreateCommand(select);
                        using (IDataReader reader = cmd.ExecuteReader()) {
                            if ((reader.IsClosed) || (!reader.Read()))
                            {
                                NotFound(response);
                                return;
                            }

                            long size = reader.GetBytes(0, 0, null, 0, 0);
                            buffer = new byte[size];
                            reader.GetBytes(0, 0, buffer, 0, buffer.Length);
                        }
                    } catch (Exception) {
                        NotFound(response);
                        return;
                    }
                }
                md5Hash = HashHelper.Hash(buffer);

                if (dbimgconfig.CacheTime != 0)
                {
                    DateTime absoluteexpire = DateTime.Now;
                    switch (dbimgconfig.CacheTime)
                    {
                    case 1: absoluteexpire = absoluteexpire.AddHours(1); break;

                    case 2: absoluteexpire = absoluteexpire.AddDays(1); break;

                    case 3: absoluteexpire = absoluteexpire.AddDays(7); break;
                    }
                    CacheItemPriority cachepriority = CacheItemPriority.Normal;
                    HttpRuntime.Cache.Insert("DbImage/" + _entity + "/" + _attribute + "/" + _id + "/" + _filename, new DbImgCacheFile(md5Hash, buffer), null, absoluteexpire, TimeSpan.Zero, cachepriority, null);
                }
            }

            cacheTime = dbimgconfig.CacheTime;

            response.Clear();
            response.Status            = "200 OK";
            response.StatusCode        = 200;
            response.StatusDescription = "OK";
            response.ContentType       = GetMIMEType();
            response.AppendHeader("Content-MD5", md5Hash);
            response.AppendHeader("Last-Modified", "Sat, 01 Jan 2000 00:00:00 GMT");
            response.AppendHeader("Cache-Control", _cachecontrolstr[cacheTime]);
            response.AppendHeader("ETag", "\"" + md5Hash + "\"");
            response.AppendHeader("Accept-Ranges", "none");
            switch (cacheTime)
            {
            case 0: {
                response.Expires = -1;
                break;
            }

            case 1: {
                response.Expires = 60;                         // 1 hour
                break;
            }

            case 2: {
                response.Expires = 1440;                         // 1 day - 60*24
                break;
            }

            case 3: {
                response.Expires = 10080;                         // 1 week - 60*24*7
                break;
            }
            }
            if (string.Equals(HttpContext.Current.Request.HttpMethod, "GET", StringComparison.CurrentCultureIgnoreCase))
            {
                response.BinaryWrite(buffer);
            }
            response.End();
        }