Ejemplo n.º 1
0
        public ActionResult Home(string message, HttpPostedFileBase file)
        {
            if (string.IsNullOrWhiteSpace(message) && file == null)
            {
                return RedirectToAction("Home");
            }

            if (message != null) message = message.Trim();

            if (_mu != null) _ua = new UserAccount(Convert.ToInt32(_mu.ProviderUserKey));

            if (_ua.UserAccountID ==  18136)
            {
                // check if over posting user can take a hint to stop
                var lastUpdate = new StatusUpdate();
                lastUpdate.GetMostRecentUserStatus(_ua.UserAccountID);

                if (lastUpdate.CreateDate > DateTime.UtcNow.AddHours(-24))
                {
                    TempData["user_error"] =
                        "Please STOP posting so much, you are only allowed to post once ever 24 hours! Thank you for keeping the wall clean.";

                    return RedirectToAction("Home");
                }
            }

            ViewBag.CurrentUser = _ua.ToUnorderdListItem;

            var su = new StatusUpdate();

            su.GetMostRecentUserStatus(_ua.UserAccountID);

            DateTime startTime = DateTime.UtcNow;

            TimeSpan span = startTime.Subtract(su.CreateDate);

            // TODO: this is not working properly, preventing posts
            if (su.Message == message && file == null)
            {
                // double post
                return RedirectToAction("Home");
            }
            su = new StatusUpdate();

            if (file != null && Utilities.IsImageFile(file.FileName))
            {
                var b = new Bitmap(file.InputStream);

                const CannedAcl acl = CannedAcl.PublicRead;

                var s3 = new S3Service
                {
                    AccessKeyID = AmazonCloudConfigs.AmazonAccessKey,
                    SecretAccessKey = AmazonCloudConfigs.AmazonSecretKey
                };

                var pitem = new PhotoItem {CreatedByUserID = _ua.UserAccountID, Title = message};

                Image fullPhoto = b;

                string fileNameFull = Utilities.CreateUniqueContentFilename(file);

                Stream maker = fullPhoto.ToAStream(ImageFormat.Jpeg);

                s3.AddObject(
                    maker,
                    maker.Length,
                    AmazonCloudConfigs.AmazonBucketName,
                    fileNameFull,
                    file.ContentType,
                    acl);

                pitem.FilePathRaw = fileNameFull;

                // resized
                Image photoResized = b;

                string fileNameResize = Utilities.CreateUniqueContentFilename(file);

                photoResized = ImageResize.FixedSize(photoResized, 500, 375, Color.Black);

                maker = photoResized.ToAStream(ImageFormat.Jpeg);

                s3.AddObject(
                    maker,
                    maker.Length,
                    AmazonCloudConfigs.AmazonBucketName,
                    fileNameResize,
                    file.ContentType,
                    acl);

                pitem.FilePathStandard = fileNameResize;

                // thumb
                Image thumbPhoto = b;

                thumbPhoto = ImageResize.Crop(thumbPhoto, 150, 150, ImageResize.AnchorPosition.Center);

                string fileNameThumb = Utilities.CreateUniqueContentFilename(file);

                maker = thumbPhoto.ToAStream(ImageFormat.Jpeg);

                s3.AddObject(
                    maker,
                    maker.Length,
                    AmazonCloudConfigs.AmazonBucketName,
                    fileNameThumb,
                    file.ContentType,
                    acl);

                pitem.FilePathThumb = fileNameThumb;

                pitem.Create();

                su.PhotoItemID = pitem.PhotoItemID;
            }

            su.UserAccountID = _ua.UserAccountID;
            su.Message = message;
            su.CreatedByUserID = _ua.UserAccountID;
            su.IsMobile = Request.Browser.IsMobileDevice;
            su.Create();

            if (Request.Browser.IsMobileDevice)
            {
                return new RedirectResult(Url.Action("Home") + "#most_recent");
            }
            return RedirectToAction("Home");
        }