/// <summary>
        /// Writes the media object to a stream.
        /// </summary>
        /// <param name="mediaId">The media id.</param>
        /// <param name="output">The output.</param>
        private void WriteMedia(int mediaId, Stream output)
        {
            using (NpgsqlConnection conn = FileHandlerHelpers.GetPgConnection())
            {
                int noid = 0;
                using (NpgsqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT data FROM \"MediaContent\" WHERE id=:id;";
                    cmd.Parameters.Add("id", mediaId);
                    noid = Convert.ToInt32(cmd.ExecuteScalar());
                }

                NpgsqlTransaction  tran        = conn.BeginTransaction();
                LargeObjectManager lbm         = new LargeObjectManager(conn);
                LargeObject        largeObject = lbm.Open(noid, LargeObjectManager.READWRITE);
                largeObject.Seek(0);
                int    size   = largeObject.Size();
                byte[] buffer = new byte[size];
                int    read   = 0;
                int    offset = 0;
                while (offset < size)
                {
                    read = largeObject.Read(buffer, offset, Math.Min(102400, size - offset));
                    output.Write(buffer, offset, read);
                    offset += 102400;
                }
                largeObject.Close();
                tran.Commit();
            }
        }
        /// <summary>
        /// Process the Request.
        /// </summary>
        /// <param name="context">The HTTP context for the request.</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev03, 2009-05-24</remarks>
        public void ProcessRequest(HttpContext context)
        {
            HttpSessionState session = context.Session;
            int mediaId = Convert.ToInt32(context.Request.Params["id"]);

            if (mediaId < 0)
            {
                int uid = FileHandlerHelpers.Login(context.Request.Params["user"], context.Request.Params["password"]);
                context.Response.StatusCode = 200;
                context.Response.Write(uid < 0 ? uid.ToString() : "TRUE");
                session["uid"] = uid;
            }
            else
            {
                object uid = session["uid"];

                if (uid == null || (int)uid < 0)
                {
                    context.Response.StatusCode = 403;
                    return;
                }

                CachedMedia cachedMedia;
                if ((cachedMedia = HttpContext.Current.Cache[mediaId.ToString()] as CachedMedia) == null)
                {
                    cachedMedia = new CachedMedia(mediaId, GetMediaMimeType(mediaId));
                    WriteMedia(mediaId, cachedMedia.Data);
                    HttpContext.Current.Cache.Insert(cachedMedia.Id.ToString(), cachedMedia, null, Cache.NoAbsoluteExpiration, cacheTimeSpan);
                }
                context.Response.StatusCode  = 200;
                context.Response.ContentType = cachedMedia.MimeType;
                cachedMedia.Data.WriteTo(context.Response.OutputStream);
            }
        }
 /// <summary>
 /// Gets the media MIME type.
 /// </summary>
 /// <param name="mediaId">The media id.</param>
 /// <returns></returns>
 private string GetMediaMimeType(int mediaId)
 {
     using (NpgsqlConnection con = FileHandlerHelpers.GetPgConnection())
     {
         using (NpgsqlCommand cmd = con.CreateCommand())
         {
             cmd.CommandText = "SELECT value FROM \"MediaProperties\" WHERE media_id=:id and property=:prop";
             cmd.Parameters.Add("id", mediaId);
             cmd.Parameters.Add("prop", MediaProperty.MimeType.ToString());
             object result = cmd.ExecuteScalar();
             return((result == null || result is DBNull) ? string.Empty : result.ToString());
         }
     }
 }
示例#4
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        /// <remarks>Documented by Dev02, 2009-07-06</remarks>
        public void ProcessRequest(HttpContext context)
        {
            HttpSessionState session     = context.Session;
            Guid             extensionId = Guid.Empty;

            try
            {
                extensionId = new Guid(context.Request.Params["guid"]);
            }
            catch (Exception)
            { }

            if (extensionId == Guid.Empty)
            {
                int uid = FileHandlerHelpers.Login(context.Request.Params["user"], context.Request.Params["password"]);
                context.Response.StatusCode = 200;
                context.Response.Write(uid < 0 ? uid.ToString() : "TRUE");
                session["uid"] = uid;
            }
            else
            {
                object uid = session["uid"];

                if (uid == null || (int)uid < 0)
                {
                    context.Response.StatusCode = 403;
                    return;
                }

                MemoryStream extensionData = new MemoryStream();
                WriteExtensionData(extensionId, extensionData);

                context.Response.StatusCode  = 200;
                context.Response.ContentType = "application/bin";
                extensionData.WriteTo(context.Response.OutputStream);
            }
        }