public S3File(string virtualPath, S3VirtualPathProvider provider) : base(virtualPath) { this.provider = provider; //Must be inside virtual filesystem folder if (!provider.IsPathVirtual(virtualPath)) throw new ArgumentException("S3 file path must be located within " + provider.VirtualFilesystemPrefix); /////////// Parse path into bucket and key //Strip prefix String path = VirtualPath.Substring(provider.VirtualFilesystemPrefix.Length); //strip leading slashes path = path.TrimStart(new char[] { '/', '\\' }); //Now execute filter! path = provider.FilterPath(path); //strip leading slashes again path = path.TrimStart(new char[] { '/', '\\' }); int keyStartsAt = path.IndexOf('/'); if (keyStartsAt < 0) throw new ArgumentException("S3 file path must specify a bucket" + path); //Get bucket bucket = path.Substring(0, keyStartsAt); //Get key key = path.Substring(keyStartsAt + 1).TrimStart(new char[] { '/', '\\' }); }
public S3File(string virtualPath, S3VirtualPathProvider provider) : base(virtualPath) { this.provider = provider; //Must be inside virtual filesystem folder if (!provider.IsPathVirtual(virtualPath)) { throw new ArgumentException("S3 file path must be located within " + provider.VirtualFilesystemPrefix); } /////////// Parse path into bucket and key //Strip prefix String path = VirtualPath.Substring(provider.VirtualFilesystemPrefix.Length); //strip leading slashes path = path.TrimStart(new char[] { '/', '\\' }); //Now execute filter! path = provider.FilterPath(path); //strip leading slashes again path = path.TrimStart(new char[] { '/', '\\' }); int keyStartsAt = path.IndexOf('/'); if (keyStartsAt < 0) { throw new ArgumentException("S3 file path must specify a bucket" + path); } //Get bucket bucket = path.Substring(0, keyStartsAt); //Get key key = path.Substring(keyStartsAt + 1).TrimStart(new char[] { '/', '\\' }); }
public IPlugin Install(Configuration.Config c) { if (vpp != null) throw new InvalidOperationException("This plugin can only be installed once, and cannot be uninstalled and reinstalled."); if (string.IsNullOrEmpty(vpath)) vpath = "~/s3/"; string[] bucketArray = null; if (!string.IsNullOrEmpty(buckets)) bucketArray = buckets.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); else c.configurationSectionIssues.AcceptIssue(new Issue("S3Reader", "S3Reader cannot function without a list of permitted bucket names.", "Please specify a comma-delimited list of buckets in the <add name='S3Reader' buckets='bucketa,bucketb' /> element.", IssueSeverity.ConfigurationError)); for (int i = 0; i < bucketArray.Length; i++) bucketArray[i] = bucketArray[i].Trim(); vpp = new S3VirtualPathProvider(this.S3Client, vpath,TimeSpan.MaxValue, new TimeSpan(0, 1, 0, 0), delegate(S3VirtualPathProvider s, S3PathEventArgs ev) { if (bucketArray == null) ev.ThrowException(); ev.AssertBucketMatches(bucketArray); }, !includeModifiedDate); c.Pipeline.PostAuthorizeRequestStart += delegate(IHttpModule sender2, HttpContext context) { //Only work with database images //This allows us to resize database images without putting ".jpg" after the ID in the path. if (!RequireImageExtension && vpp.IsPathVirtual(c.Pipeline.PreRewritePath)) c.Pipeline.SkipFileTypeCheck = true; //Skip the file extension check. FakeExtensions will still be stripped. }; c.Pipeline.RewriteDefaults += delegate(IHttpModule sender, HttpContext context, Configuration.IUrlEventArgs e) { //Only work with database images //Non-images will be served as-is //Cache all file types, whether they are processed or not. if (CacheUnmodifiedFiles && vpp.IsPathVirtual(e.VirtualPath)) e.QueryString["cache"] = ServerCacheMode.Always.ToString(); }; c.Pipeline.PostRewrite += delegate(IHttpModule sender, HttpContext context, Configuration.IUrlEventArgs e) { //Only work with database images //If the data is untrusted, always re-encode each file. if (UntrustedData && vpp.IsPathVirtual(e.VirtualPath)) e.QueryString["process"] = ImageResizer.ProcessWhen.Always.ToString(); }; if (asVpp) { try { //Registers the virtual path provider. HostingEnvironment.RegisterVirtualPathProvider(vpp); } catch (SecurityException) { asVpp = false; c.configurationSectionIssues.AcceptIssue(new Issue("S3Reader", "S3Reader could not be installed as a VirtualPathProvider due to missing AspNetHostingPermission." ,"It was installed as an IVirtualImageProvider instead, which means that only image URLs will be accessible, and only if they contain a querystring.\n" + "Set vpp=false to tell S3Reader to register as an IVirtualImageProvider instead. <add name='S3Reader' vpp=false />", IssueSeverity.Error)); } } if (!asVpp) { c.Plugins.VirtualProviderPlugins.Add(vpp); } // c.Plugins.add_plugin(this); return this; }
public IPlugin Install(Configuration.Config c) { if (vpp != null) { throw new InvalidOperationException("This plugin can only be installed once, and cannot be uninstalled and reinstalled."); } if (string.IsNullOrEmpty(vpath)) { vpath = "~/s3/"; } string[] bucketArray = null; if (!string.IsNullOrEmpty(buckets)) { bucketArray = buckets.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); } else { c.configurationSectionIssues.AcceptIssue(new Issue("S3Reader", "S3Reader cannot function without a list of permitted bucket names.", "Please specify a comma-delimited list of buckets in the <add name='S3Reader' buckets='bucketa,bucketb' /> element.", IssueSeverity.ConfigurationError)); } for (int i = 0; i < bucketArray.Length; i++) { bucketArray[i] = bucketArray[i].Trim(); } vpp = new S3VirtualPathProvider(this.S3Client, vpath, TimeSpan.MaxValue, new TimeSpan(0, 1, 0, 0), delegate(S3VirtualPathProvider s, S3PathEventArgs ev) { if (bucketArray == null) { ev.ThrowException(); } ev.AssertBucketMatches(bucketArray); }, !includeModifiedDate); c.Pipeline.PostAuthorizeRequestStart += delegate(IHttpModule sender2, HttpContext context) { //Only work with database images //This allows us to resize database images without putting ".jpg" after the ID in the path. if (!RequireImageExtension && vpp.IsPathVirtual(c.Pipeline.PreRewritePath)) { c.Pipeline.SkipFileTypeCheck = true; //Skip the file extension check. FakeExtensions will still be stripped. } }; c.Pipeline.RewriteDefaults += delegate(IHttpModule sender, HttpContext context, Configuration.IUrlEventArgs e) { //Only work with database images //Non-images will be served as-is //Cache all file types, whether they are processed or not. if (CacheUnmodifiedFiles && vpp.IsPathVirtual(e.VirtualPath)) { e.QueryString["cache"] = ServerCacheMode.Always.ToString(); } }; c.Pipeline.PostRewrite += delegate(IHttpModule sender, HttpContext context, Configuration.IUrlEventArgs e) { //Only work with database images //If the data is untrusted, always re-encode each file. if (UntrustedData && vpp.IsPathVirtual(e.VirtualPath)) { e.QueryString["process"] = ImageResizer.ProcessWhen.Always.ToString(); } }; if (asVpp) { try { //Registers the virtual path provider. HostingEnvironment.RegisterVirtualPathProvider(vpp); } catch (SecurityException) { asVpp = false; c.configurationSectionIssues.AcceptIssue(new Issue("S3Reader", "S3Reader could not be installed as a VirtualPathProvider due to missing AspNetHostingPermission." , "It was installed as an IVirtualImageProvider instead, which means that only image URLs will be accessible, and only if they contain a querystring.\n" + "Set vpp=false to tell S3Reader to register as an IVirtualImageProvider instead. <add name='S3Reader' vpp=false />", IssueSeverity.Error)); } } if (!asVpp) { c.Plugins.VirtualProviderPlugins.Add(vpp); } // c.Plugins.add_plugin(this); return(this); }