Beispiel #1
0
 public async Task SaveFile(string key, string contentType, Stream stream, ImageDisOptions options)
 {
     using (var client = new AmazonS3Client(_awsAccessKeyId, _awsSecretAccessKey, _region))
     {
         await client.PutObjectAsync(new PutObjectRequest
         {
             BucketName  = _bucketName,
             Key         = key,
             ContentType = contentType,
             InputStream = stream,
             CannedACL   = S3CannedACL.PublicRead
         });
     }
 }
        private string ContentTypeToExtension(string contentType, ImageDisOptions options)
        {
            if (options.AllowedImageTypes != null)
            {
                foreach (var allowedImageType in options.AllowedImageTypes)
                {
                    if (contentType.Equals(allowedImageType.ContentType, StringComparison.OrdinalIgnoreCase))
                    {
                        return(allowedImageType.FileExtension);
                    }
                }
            }

            throw new Exception("Unsupported content type.");
        }
        public async Task SaveFile(string key, string contentType, Stream stream, ImageDisOptions options)
        {
            if (!Directory.Exists(_filePath))
            {
                Directory.CreateDirectory(_filePath);
            }

            using (var f = File.OpenWrite($"{_filePath.TrimEnd('\\')}\\{key}.{ContentTypeToExtension(contentType, options)}"))
            {
                var buffer = new byte[8 * 1024];
                int len;
                while ((len = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    await f.WriteAsync(buffer, 0, len);
                }
            }
        }
        public async Task <ImageDisFile> GetFile(string key, ImageDisOptions options)
        {
            var file = Directory.GetFiles(_filePath, $"{key}.*").FirstOrDefault();

            using (var f = File.OpenRead(file))
            {
                var ms = new MemoryStream();
                await f.CopyToAsync(ms);

                ms.Seek(0, SeekOrigin.Begin);

                return(new ImageDisFile
                {
                    ContentType = ExtensionToContentType(Path.GetExtension(file).TrimStart('.'), options),
                    Stream = ms
                });
            }
        }
Beispiel #5
0
        public async Task <ImageDisFile> GetFile(string key, ImageDisOptions options)
        {
            using (var client = new AmazonS3Client(_awsAccessKeyId, _awsSecretAccessKey, _region))
            {
                var obj = await client.GetObjectAsync(new GetObjectRequest
                {
                    BucketName = _bucketName,
                    Key        = key
                });

                var stream = new MemoryStream();
                await obj.ResponseStream.CopyToAsync(stream);

                stream.Seek(0, SeekOrigin.Begin);

                return(new ImageDisFile
                {
                    ContentType = obj.Headers.ContentType,
                    Stream = stream
                });
            }
        }
Beispiel #6
0
 public static void UseImageDis(this IApplicationBuilder app, ImageDisOptions options)
 {
     app.Map(options.Path, subApp => subApp.RunImageDis(options));
 }
Beispiel #7
0
 public static void RunImageDis(this IApplicationBuilder builder, ImageDisOptions options)
 {
     builder.UseMiddleware <ImageDisMiddleware>(options);
 }
Beispiel #8
0
 public static void UseImageDis(this IAppBuilder app, ImageDisOptions options)
 {
     app.Use<ImageDisMiddleware>(options);
 }
Beispiel #9
0
 public ImageDisMiddleware(RequestDelegate next, ImageDisOptions options)
 {
     _next          = next;
     _options       = options;
     _getImageRegex = new Regex(@"/([A-Za-z0-9\.]+)", RegexOptions.IgnoreCase);
 }
Beispiel #10
0
 public static void UseImageDis(this IAppBuilder app, ImageDisOptions options)
 {
     app.Use <ImageDisMiddleware>(options);
 }
Beispiel #11
0
 public ImageDisMiddleware(OwinMiddleware next, ImageDisOptions options)
     : base(next)
 {
     _options       = options;
     _getImageRegex = new Regex(options.Path + @"/([A-Za-z0-9\.]+)", RegexOptions.IgnoreCase);
 }