Exemple #1
0
        public static void PutPhoto(string domainName, string itemName, string bucketName, string fileName, Stream fileContent, bool isPublic, AmazonSimpleDBClient sdbClient, AmazonS3Client s3Client)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                return;
            }

            BucketHelper.CheckForBucket(itemName, s3Client);

            PutObjectRequest putObjectRequest = new PutObjectRequest();

            putObjectRequest.BucketName  = bucketName;
            putObjectRequest.CannedACL   = S3CannedACL.PublicRead;
            putObjectRequest.Key         = fileName;
            putObjectRequest.InputStream = fileContent;
            s3Client.PutObject(putObjectRequest);

            DomainHelper.CheckForDomain(domainName, sdbClient);
            PutAttributesRequest putAttrRequest = new PutAttributesRequest()
            {
                DomainName = domainName,
                ItemName   = itemName
            };

            putAttrRequest.Attributes.Add(new ReplaceableAttribute
            {
                Name    = "PhotoThumbUrl",
                Value   = String.Format(Settings.Default.S3BucketUrlFormat, String.Format(Settings.Default.BucketNameFormat, HttpContext.Current.User.Identity.Name, itemName), fileName),
                Replace = true
            });
            sdbClient.PutAttributes(putAttrRequest);

            if (isPublic)
            {
                DomainHelper.CheckForDomain(Settings.Default.PetBoardPublicDomainName, sdbClient);
                putAttrRequest.DomainName = Settings.Default.PetBoardPublicDomainName;
                sdbClient.PutAttributes(putAttrRequest);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session[Settings.Default.FlashSessionKey] != null)
            {
                this.FlashLiteralWrapper.Visible = true;
                this.FlashLiteral.Text           = this.Session[Settings.Default.FlashSessionKey].ToString();
                this.Session[Settings.Default.FlashSessionKey] = null;
            }
            else
            {
                this.FlashLiteralWrapper.Visible = false;
            }

            this._petIdString = this.Request.QueryString["petid"];

            if (String.IsNullOrEmpty(this._petIdString))
            {
                this.StatsLiteral.Text    = "Add a New Pet";
                this.SaveStatsButton.Text = "Save New Pet";
            }
            else
            {
                this.PhotoPanel.Visible = true;
            }

            this._userBucketName = String.Format(Settings.Default.BucketNameFormat, this.Context.User.Identity.Name, this._petIdString);
            this._itemName       = this._petIdString ?? Guid.NewGuid().ToString();
            this._domainName     = String.Format(Settings.Default.SimpleDbDomainNameFormat, this.Context.User.Identity.Name);

            if (!this.Page.IsPostBack)
            {
                List <int> years = new List <int>(100);
                for (int i = 0; i < 100; i++)
                {
                    years.Add(DateTime.Now.AddYears(i * -1).Year);
                }
                this.YearDropDownList.DataSource = years.OrderByDescending(y => y);
                this.YearDropDownList.DataBind();
                this.SelectMonth();
                this.SelectDay();

                Pet           pet   = default(Pet);
                List <string> files = new List <string>();
                if (!String.IsNullOrEmpty(this._petIdString))
                {
                    //
                    // Try to get the requested pet from the user's private domain
                    //
                    DomainHelper.CheckForDomain(this._domainName, _simpleDBClient);

                    GetAttributesRequest getAttributeRequest = new GetAttributesRequest()
                    {
                        DomainName = this._domainName,
                        ItemName   = this._itemName
                    };

                    GetAttributesResponse getAttributeResponse = _simpleDBClient.GetAttributes(getAttributeRequest);
                    List <Attribute>      attrs = null;
                    bool showPublic             = false;

                    attrs = getAttributeResponse.Attributes;

                    showPublic = false;

                    //
                    // If we can't find it try the public domain
                    //
                    if (attrs.Count == 0)
                    {
                        showPublic = true;
                    }

                    if (showPublic)
                    {
                        Response.Redirect(String.Concat("PetProfile.aspx?petid", _petIdString));
                        return;
                    }

                    pet = new Pet
                    {
                        Name      = attrs.First(a => a.Name == "Name").Value,
                        Birthdate = attrs.First(a => a.Name == "Birthdate").Value,
                        Sex       = attrs.First(a => a.Name == "Sex").Value,
                        Type      = attrs.First(a => a.Name == "Type").Value,
                        Breed     = attrs.First(a => a.Name == "Breed").Value,
                        Likes     = attrs.First(a => a.Name == "Likes").Value,
                        Dislikes  = attrs.First(a => a.Name == "Dislikes").Value
                    };

                    this.Public.Checked = bool.Parse(attrs.First(a => a.Name == "Public").Value);

                    using (AmazonS3Client s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2))
                    {
                        BucketHelper.CheckForBucket(this._petIdString, s3Client);
                        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                        {
                            BucketName = this._userBucketName
                        };

                        ListObjectsResponse listObjectsResponse = s3Client.ListObjects(listObjectsRequest);
                        {
                            files = listObjectsResponse.S3Objects.Select(o => String.Format(Settings.Default.S3BucketUrlFormat, this._userBucketName, o.Key)).ToList();
                            string firstPhoto = files.FirstOrDefault();
                            this.PhotoThumbUrl.Value = firstPhoto ?? String.Empty;
                        }
                    }
                }

                if (pet != default(Pet))
                {
                    this.PetNameHeader.Text = pet.Name;
                    this.NameTextBox.Text   = pet.Name;
                    this.AnimalDropDownList.SelectedValue = pet.Type;
                    this.BreedTextBox.Text             = pet.Breed;
                    this.SexDropDownList.SelectedValue = pet.Sex;
                    if (pet.Birthdate != null)
                    {
                        DateTime birthdate = DateTime.Parse(pet.Birthdate);
                        this.YearDropDownList.SelectedValue  = birthdate.Year.ToString();
                        this.MonthDropDownList.SelectedValue = birthdate.Month.ToString();
                        this.DayDropDownList.SelectedValue   = birthdate.Day.ToString();
                    }
                    this.LikesTextBox.Text        = pet.Likes;
                    this.DislikesTextBox.Text     = pet.Dislikes;
                    this.PhotoRepeater.DataSource = files;
                    this.PhotoRepeater.DataBind();
                }
            }
        }