// Delete file from the server
        private void DeleteFile(HttpContext context)
        {
            var _getlen    = 10;
            var fileName   = context.Request["f"];
            var fileExt    = fileName.Remove(0, fileName.LastIndexOf('.')).ToLower();
            var hasThumb   = Regex.Match(fileName.ToLower(), AmazonHelper.ImgExtensions()).Success;
            var keyName    = GetKeyName(context, HttpUtility.UrlDecode(context.Request["f"]));
            var client     = AmazonHelper.GetS3Client();
            var extrequest = new GetObjectRequest()
                             .WithByteRange(0, _getlen)
                             .WithKey(keyName)
                             .WithBucketName(StorageRoot);
            var extresponse = client.GetObject(extrequest);
            var length      = extresponse.ContentLength;

            extresponse.Dispose();
            if (length == _getlen + 1)
            {
                var delrequest = new DeleteObjectRequest()
                                 .WithKey(keyName)
                                 .WithBucketName(StorageRoot);
                var delresponse = client.DeleteObject(delrequest);
                delresponse.Dispose();
                if (hasThumb)
                {
                    try
                    {
                        keyName = keyName.Replace(fileName, "thumbs/" + fileName.Replace(fileExt, ".png"));
                        var thumbcheck = new GetObjectRequest()
                                         .WithByteRange(0, _getlen)
                                         .WithKey(keyName)
                                         .WithBucketName(StorageRoot);
                        var thumbCheckResponse = client.GetObject(thumbcheck);
                        length = extresponse.ContentLength;
                        thumbCheckResponse.Dispose();
                        if (length == _getlen + 1)
                        {
                            var thumbdelrequest = new DeleteObjectRequest()
                                                  .WithKey(keyName)
                                                  .WithBucketName(StorageRoot);
                            var thumbdelresponse = client.DeleteObject(thumbdelrequest);
                            delresponse.Dispose();
                        }
                    }
                    catch (Exception ex)
                    {
                        var messg = ex.Message;
                    }
                }
            }
        }
        private void ListCurrentFiles(HttpContext context)
        {
            var bucket = ConfigurationManager.AppSettings["Assets_Amazon_Bucket"];

            var prefix  = GetPrefix(context);
            var doctype = context.Request["doctype"];
            var files   = new List <AmazonFilesStatus>();
            var client  = AmazonHelper.GetS3Client();
            var request = new ListObjectsRequest().WithBucketName(bucket).WithDelimiter("/").WithPrefix(prefix);

            var l = client.ListObjects(request);

            foreach (var fl in l.S3Objects)
            {
                if (fl.Size > 0)
                {
                    if (String.IsNullOrWhiteSpace(doctype) || (doctype == getDocType(fl.Key)))
                    {
                        var fs = new AmazonFilesStatus(fl, bucket, prefix);
                        files.Add(fs);
                    }
                }
            }
            var typeObj = new Dictionary <String, String>();

            typeObj.Add("doc", AmazonHelper.DocExtensions());
            typeObj.Add("image", AmazonHelper.ImgExtensions());
            typeObj.Add("media", AmazonHelper.MediaExtensions());
            var retObj = new FileReturn()
            {
                Files = files, AssetTypes = typeObj
            };

            string jsonObj = js.Serialize(retObj);

            context.Response.AddHeader("Content-Disposition", "inline, filename=\"files.json\"");
            context.Response.Write(jsonObj);
            context.Response.ContentType = "application/json";
        }
Beispiel #3
0
        public AmazonFilesStatus(S3Object s3File, string bucket, string prefix)
        {
            var filename = s3File.Key.Replace(prefix, "");
            var fileExt  = filename.Remove(0, filename.LastIndexOf('.'));

            type    = getContentTypeByExtension(fileExt);
            isimage = Regex.Match(filename.ToLower(), AmazonHelper.ImgExtensions()).Success;
            var client  = AmazonHelper.GetS3Client();
            var metareq = new GetObjectMetadataRequest().WithBucketName(bucket).WithKey(s3File.Key);
            var meta    = client.GetObjectMetadata(metareq);
            var height  = 0;
            var width   = 0;

            if (isimage)
            {
                height = String.IsNullOrWhiteSpace(meta.Headers["x-amz-meta-height"])? 0 : Int32.Parse(meta.Headers["x-amz-meta-height"]);
                width  = String.IsNullOrWhiteSpace(meta.Headers["x-amz-meta-width"])? 0 : Int32.Parse(meta.Headers["x-amz-meta-width"]);
            }

            var size = Convert.ToInt32(s3File.Size);

            SetValues(filename, bucket, prefix, size, height, width);
        }
        // Upload entire file
        private void UploadWholeFile(HttpContext context, List <AmazonFilesStatus> statuses)
        {
            for (int i = 0; i < context.Request.Files.Count; i++)
            {
                var file = context.Request.Files[i];

                MemoryStream thumbStream = new MemoryStream();
                var          fileExt     = file.FileName.Remove(0, file.FileName.LastIndexOf('.')).ToLower();
                var          needsThumb  = Regex.Match(file.FileName.ToLower(), AmazonHelper.ImgExtensions()).Success;
                var          keyName     = GetKeyName(context, file.FileName.ToLower());
                var          len         = file.ContentLength;
                var          height      = 0;
                var          width       = 0;
                if (needsThumb)
                {
                    file.InputStream.CopyTo(thumbStream);
                }
                var client  = AmazonHelper.GetS3Client();
                var request = new PutObjectRequest()
                              .WithKey(keyName)
                              .WithBucketName(StorageRoot)
                              .WithContentType(file.ContentType)
                              .WithCannedACL(S3CannedACL.PublicRead)
                              .WithAutoCloseStream(true)
                              .WithStorageClass(S3StorageClass.ReducedRedundancy)
                              .WithInputStream(file.InputStream);
                if (needsThumb)
                {
                    Image img = Image.FromStream(thumbStream);
                    height = img.Height;
                    width  = img.Width;
                    thumbStream.Position = 0;

                    request.AddHeader("x-amz-meta-width", width.ToString());
                    request.AddHeader("x-amz-meta-height", height.ToString());
                    var orientation = width > height ? "h" : "v";
                    request.AddHeader("x-amz-meta-orient", orientation);
                }

                var response = client.PutObject((PutObjectRequest)request);
                response.Dispose();
                if (needsThumb)
                {
                    var thumbKeyName     = VirtualPathUtility.RemoveTrailingSlash(keyName.Replace(file.FileName.ToLower(), "")) + "/thumbs/" + file.FileName.ToLower().Replace(fileExt, ".png");
                    var thumbImageStream = GetThumbnail(thumbStream);
                    var thumbrequest     = new PutObjectRequest()
                                           .WithKey(thumbKeyName)
                                           .WithBucketName(StorageRoot)
                                           .WithContentType("image/png")
                                           .WithCannedACL(S3CannedACL.PublicRead)
                                           .WithAutoCloseStream(true)
                                           .WithStorageClass(S3StorageClass.ReducedRedundancy)
                                           .WithInputStream(thumbImageStream);

                    var thumbresponse = client.PutObject((PutObjectRequest)thumbrequest);
                    thumbStream.Dispose();
                }
                var fileStatus = new AmazonFilesStatus(file.FileName.ToLower(), StorageRoot, GetPrefix(context), len, height, width);

                //string fullName = Path.GetFileName(file.FileName);
                statuses.Add(fileStatus);
            }
        }
        private string getDocType(string key)
        {
            var fileExt = key.Remove(0, key.LastIndexOf('.')).ToLower();

            return(Regex.Match(key.ToLower(), AmazonHelper.ImgExtensions()).Success ? "image" : "doc");
        }