示例#1
0
 public string ConstructUploadUrl(string filePathAndName)
 {
     return(base.ExecuteFunction("ConstructUploadUrl", delegate()
     {
         return AmazonUtility.ConstructAmazonUrl(this.AmazonCloudFrontUrl, this.AmazonPublicUrl, this.AmazonBucket, filePathAndName);
     }));
 }
示例#2
0
        public string UploadPhoto(Image image, ImageEncoding encoding, Size imageTargetSize, string filePathAndName)
        {
            return(base.ExecuteFunction("UploadPhoto", delegate()
            {
                filePathAndName = filePathAndName.Replace(" ", "%20"); //TODO:COULD: Sanitize this a littler better
                Image resizedImage = ImageFormatter.Resize(image, ResizeMode.Fill, (AnchorStyles.Middle | AnchorStyles.Center), imageTargetSize.Width, imageTargetSize.Height);

                using (AmazonS3Client client = new AmazonS3Client(this.AmazonKeyID, this.AmazonSecret, RegionEndpoint.USEast1))
                {
                    PutObjectResponse result = null;

                    ImageCodecInfo codecInfo = null;
                    EncoderParameters encoderParameters = null;
                    switch (encoding)
                    {
                    case ImageEncoding.PNG:
                        codecInfo = GetCodecInfo("image/png");
                        encoderParameters = new EncoderParameters(0);
                        if (!filePathAndName.ToLower().EndsWith("png"))
                        {
                            filePathAndName += ".png";
                        }
                        break;

                    case ImageEncoding.JPEG:
                    default:
                        encoderParameters = new EncoderParameters(1);
                        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 90L);
                        codecInfo = GetCodecInfo("image/jpeg");
                        if (!filePathAndName.ToLower().EndsWith("jpg"))
                        {
                            filePathAndName += ".jpg";
                        }
                        break;
                    }

                    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
                    {
                        resizedImage.Save(memoryStream, codecInfo, encoderParameters);
                        memoryStream.Position = 0;

                        PutObjectRequest request = new PutObjectRequest()
                        {
                            BucketName = this.AmazonBucket,
                            Key = filePathAndName,
                            CannedACL = S3CannedACL.PublicRead,
                            InputStream = memoryStream
                        };
                        result = client.PutObject(request);
                    }

                    if (result == null || (result.HttpStatusCode != HttpStatusCode.OK && result.HttpStatusCode != HttpStatusCode.Created))
                    {
                        throw new UIException("Error saving file to cloud storage. Please try again in a few moments.");
                    }
                    else
                    {
                        return AmazonUtility.ConstructAmazonUrl(this.AmazonCloudFrontUrl, this.AmazonPublicUrl, this.AmazonBucket, filePathAndName);
                    }
                }
            }));
        }