コード例 #1
0
        public ActionResult Edit(Image model, HttpPostedFileBase newImage)
        {
            if (ModelState.IsValid)
            {
                var image = db.Images.Find(model.ID);
                if (image == null)
                {
                    return(new HttpNotFoundResult());
                }

                // S3 credentials
                var s3BktName         = ConfigurationManager.AppSettings["S3BucketName"];
                var s3AccessKey       = ConfigurationManager.AppSettings["AWSAccessKeyId"];
                var s3SecretAccessKey = ConfigurationManager.AppSettings["AWSSecretAccessKey"];
                var s3Region          = ConfigurationManager.AppSettings["AWSRegion"];
                var s3FolderName      = "Test";
                var s3ObjectKey       = s3FolderName + "/" + Path.GetFileName(newImage.FileName);

                var awsS3Helper = new AwsS3Helper();
                if (awsS3Helper.ConnectS3(s3AccessKey, s3SecretAccessKey, s3Region))
                {
                    // Delete the existing image first
                    awsS3Helper.DeleteS3Object(image.ImagePathS3, s3BktName, null);

                    awsS3Helper.UploadS3File(s3ObjectKey, s3BktName, newImage.ContentType, newImage.InputStream);
                }

                string filePath     = GetPartialFilePath(newImage);
                var    oldImagePath = image.ImagePath;

                // Saving in DB
                image.ImageTitle       = Path.GetFileNameWithoutExtension(newImage.FileName);
                image.ImagePath        = filePath;
                image.ImagePathS3      = s3ObjectKey;
                image.OriginalFileName = Path.GetFileName(newImage.FileName);

                db.Entry(image).State = EntityState.Modified;
                db.SaveChanges();

                // Delete existing file
                string sFName = HttpContext.Server.MapPath(oldImagePath);
                System.IO.File.Delete(sFName);

                // Saving in physical location
                filePath = Server.MapPath(filePath);
                newImage.SaveAs(filePath);

                return(RedirectToAction("Index"));
            }

            return(View());
        }
コード例 #2
0
        public ActionResult DeleteConfirmed(int id)
        {
            Image img = db.Images.Find(id);

            if (img == null)
            {
                return(HttpNotFound());
            }
            // remove the file from disk...
            try
            {
                string sFName = HttpContext.Server.MapPath(img.ImagePath);
                System.IO.File.Delete(sFName);
            }
            catch (Exception exc)
            {
                throw new HttpException(500, exc.Message);
            }

            // Remove file from S3
            // S3 Related works
            var s3BktName         = ConfigurationManager.AppSettings["S3BucketName"];
            var s3AccessKey       = ConfigurationManager.AppSettings["AWSAccessKeyId"];
            var s3SecretAccessKey = ConfigurationManager.AppSettings["AWSSecretAccessKey"];
            var s3Region          = ConfigurationManager.AppSettings["AWSRegion"];

            var awsS3Helper = new AwsS3Helper();

            if (awsS3Helper.ConnectS3(s3AccessKey, s3SecretAccessKey, s3Region))
            {
                awsS3Helper.DeleteS3Object(img.ImagePathS3, s3BktName, null);
            }

            // Saving database entry
            db.Images.Remove(img);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }