public void ProcessRequest(HttpContext context)
        {
            using (FilestreamRepository fr = new FilestreamRepository(ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString, "Media"))
            {
                string path = context.Request.Params["path"];

                if (String.IsNullOrEmpty(path))
                {
                    // return 404
                }

                string mimeType;

                MemoryStream s = fr.GetFileStream(path, out mimeType);

                context.Response.ContentType = mimeType;
                context.Response.AppendHeader("Content-Length", s.Length.ToString());
                context.Response.BinaryWrite(s.ToArray());
            }
        }
        public void AddFile(string path, System.IO.Stream stream, bool overrideIfExists)
        {
            this.Logger.Information("AddFile({0}, {1}, {2})", path, stream, overrideIfExists);

            int directory;
            string filename;

            if (UmbracoPath.MediaPathParse(path, out directory, out filename))
            {
                string mimeType = MimeTypeMap.GetMimeType(Path.GetExtension(path));

                using (FilestreamRepository fsr = new FilestreamRepository(this.ConnectionString, this.TableName))
                {
                    bool exists = fsr.FileExists(path);

                    if (overrideIfExists)
                    {
                        if (exists)
                        {
                            fsr.UpdateFile(path, stream, filename, mimeType);
                        }
                        else
                        {
                            fsr.AddFile(directory, path, mimeType, filename, stream);
                        }
                    }
                    else
                    {
                        if (exists)
                        {
                            throw new Exception(String.Format("File '{0}' already exists", path));
                        }
                        else
                        {
                            fsr.AddFile(directory, path, mimeType, filename, stream);
                        }
                    }
                }
            }
        }
        public void DeleteDirectory(string directory, bool recursive)
        {
            this.Logger.Information("DeleteDirectory({0}, {1})", directory, recursive);

            int dir;

            if (int.TryParse(directory, out dir))
            {
                using (FilestreamRepository fsr = new FilestreamRepository(this.ConnectionString, this.TableName))
                {
                    fsr.DeleteDirectory(dir);
                }
            }
            else
            {
                throw new Exception(String.Format("Cannot parse directory '{0}' as a number", directory));
            }
        }
        public System.IO.Stream OpenFile(string path)
        {
            this.Logger.Information("OpenFile({0})", path);

            path = UmbracoPath.MediaUrlParse(this.HandlerPath, path);

            using (FilestreamRepository fsr = new FilestreamRepository(this.ConnectionString, this.TableName))
            {
                string mimeType;
                return fsr.GetFileStream(path, out mimeType);
            }
        }
        public DateTimeOffset GetLastModified(string path)
        {
            this.Logger.Information("GetLastModified({0})", path);

            path = UmbracoPath.MediaUrlParse(this.HandlerPath, path);

            using (FilestreamRepository fsr = new FilestreamRepository(this.ConnectionString, this.TableName))
            {
                return fsr.GetModifiedDate(path);
            }
        }
        public IEnumerable<string> GetDirectories(string path)
        {
            this.Logger.Information("GetDirectories({0})", path);
            using (FilestreamRepository fsr = new FilestreamRepository(this.ConnectionString, this.TableName))
            {
                List<string> found = fsr.GetDirectories();
                if (found != null)
                {
                    return found;
                }
            }

            return new List<string>();
        }
        // Sometimes this gets called with n\x where n is directory and x is file, and sometimes
        // it gets called with what I returned from GetUrl
        public bool FileExists(string path)
        {
            this.Logger.Information("FileExists({0})", path);

            path = UmbracoPath.MediaUrlParse(this.HandlerPath, path);

            using (FilestreamRepository fsr = new FilestreamRepository(this.ConnectionString, this.TableName))
            {
                return fsr.FileExists(path);
            }
        }