public ActionResult Create(PhotoViewModel photoViewModel, HttpPostedFileBase file, bool Public, FormCollection collection)
        {
            if (this.ModelState.IsValid)
            {
                photoViewModel.PartitionKey = Public ? "Public" : this.User.Identity.Name;
                var photo = this.FromViewModel(photoViewModel);

                if (file != null)
                {
                    //Save file stream to Blob Storage
                    var blob = this.GetBlobContainer().GetBlockBlobReference(file.FileName);
                    blob.Properties.ContentType = file.ContentType;
                    var image = new System.Drawing.Bitmap(file.InputStream);
                    if (image != null)
                    {
                        blob.Metadata.Add("Width", image.Width.ToString());
                        blob.Metadata.Add("Height", image.Height.ToString());
                    }

                    blob.UploadFromStream(file.InputStream);
                    photo.BlobReference = file.FileName;
                }
                else
                {
                    this.ModelState.AddModelError("File", new ArgumentNullException("file"));
                    return this.View(photoViewModel);
                }

                //Save information to Table Storage
                var token = Public ? this.PublicTableSas : this.AuthenticatedTableSas;
                if (!this.User.Identity.IsAuthenticated)
                {
                    token = this.PublicTableSas;
                    photo.PartitionKey = "Public";
                }

                CloudTableClient cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(token));
                var photoContext = new PhotoDataServiceContext(cloudTableClient);

                photoContext.AddPhoto(photo);

                try
                {
                    //Send create notification
                    var msg = new CloudQueueMessage(string.Format("Photo Uploaded,{0}", photo.BlobReference));
                    this.GetCloudQueue().AddMessage(msg);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.TraceInformation("Error", "Couldn't send notification");
                }

                return this.RedirectToAction("Index");
            }

            return this.View();
        }
        public ActionResult Create(PhotoViewModel photoViewModel, HttpPostedFileBase file, bool Public, FormCollection collection)
        {
            if (this.ModelState.IsValid)
            {
                photoViewModel.PartitionKey = Public ? "Public" : this.User.Identity.Name;
                var photo = this.FromViewModel(photoViewModel);

                if (file != null)
                {
                    //Save file stream to Blob Storage
                    var blob = this.GetBlobContainer().GetBlockBlobReference(file.FileName);
                    blob.Properties.ContentType = file.ContentType;
                    blob.UploadFromStream(file.InputStream);
                    photo.BlobReference = file.FileName;
                }
                else
                {
                    this.ModelState.AddModelError("File", new ArgumentNullException("file"));
                    return this.View(photoViewModel);
                }

                //Save information to Table Storage
                var token = Public ? this.PublicTableSas : this.AuthenticatedTableSas;
                if (!this.User.Identity.IsAuthenticated)
                {
                    token = this.PublicTableSas;
                    photo.PartitionKey = "Public";
                }

                CloudTableClient cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(token));
                var photoContext = new PhotoDataServiceContext(cloudTableClient);

                photoContext.AddPhoto(photo);

                //Send create notification
                var msg = new CloudQueueMessage("Photo Uploaded");
                this.GetCloudQueue().AddMessage(msg);

                return this.RedirectToAction("Index");
            }

            return this.View();
        }
        public ActionResult Create(PhotoViewModel photoViewModel, HttpPostedFileBase file, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                var photo = FromViewModel(photoViewModel);

                // Upload blob
                if (file != null)
                {
                    var blob = GetBlobContainer().GetBlockBlobReference(file.FileName);
                    blob.Properties.ContentType = file.ContentType;
                    blob.UploadFromStream(file.InputStream);
                    photo.BlobReference = file.FileName;
                }
                else
                {
                    ModelState.AddModelError("File", new ArgumentNullException("file"));
                    return View(photoViewModel);
                }

                // Insert table entry
                photo.PartitionKey = User.Identity.IsAuthenticated ? User.Identity.Name : "Public";
                var cloudTableClient = this.storageAccount.CreateCloudTableClient();
                var photoContext = new PhotoDataServiceContext(cloudTableClient);
                photoContext.AddPhoto(photo);

                // Send create notification
                try
                {
                    var msg = new CloudQueueMessage("Photo Uploaded");
                    GetCloudQueue().AddMessage(msg);
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Failed to send notification: {0}", ex.Message);
                }

                return RedirectToAction("Index");
            }

            return View();
        }
        public ActionResult Create(PhotoViewModel photoViewModel, HttpPostedFileBase file, FormCollection collection)
        {
            if (this.ModelState.IsValid)
            {
                photoViewModel.PartitionKey = this.User.Identity.IsAuthenticated ? this.User.Identity.Name : "Public";
                var photo = this.FromViewModel(photoViewModel);

                if (file != null)
                {
                    //Save file stream to Blob Storage
                    var blob = this.GetBlobContainer().GetBlockBlobReference(file.FileName);
                    blob.Properties.ContentType = file.ContentType;
                    blob.UploadFromStream(file.InputStream);
                    photo.BlobReference = file.FileName;
                }
                else
                {
                    this.ModelState.AddModelError("File", new ArgumentNullException("file"));
                    return this.View(photoViewModel);
                }

                //Save information to Table Storage
                CloudTableClient cloudTableClient = this.StorageAccount.CreateCloudTableClient();
                var photoContext = new PhotoDataServiceContext(cloudTableClient);
                photoContext.AddPhoto(photo);

                try
                {
                    //Send create notification
                    var msg = new CloudQueueMessage("Photo Uploaded");
                    this.GetCloudQueue().AddMessage(msg);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.TraceInformation("Error", "Couldn't send notification");
                }

                return this.RedirectToAction("Index");
            }

            return this.View();
        }
        //
        // GET: /Home/Delete/5
        public ActionResult Delete(string partitionKey, string rowKey)
        {
            string token = partitionKey == "Public" ? this.PublicTableSas : this.AuthenticatedTableSas;

            CloudTableClient cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(token));
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            PhotoEntity photo = photoContext.GetById(partitionKey, rowKey);

            if (photo == null)
            {
                return this.HttpNotFound();
            }

            var viewModel = this.ToViewModel(photo);
            if (!string.IsNullOrEmpty(photo.BlobReference))
            {
                viewModel.Uri = this.GetBlobContainer().GetBlockBlobReference(photo.BlobReference).Uri.ToString();
            }

            return this.View(viewModel);
        }
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            CloudTableClient cloudTableClientAdmin = this.StorageAccount.CreateCloudTableClient();
            var photoContextAdmin = new PhotoDataServiceContext(cloudTableClientAdmin);

            if (this.User.Identity.IsAuthenticated)
            {
                this.AuthenticatedTableSas = photoContextAdmin.GetSas(this.User.Identity.Name, "admin");
                this.PublicTableSas = photoContextAdmin.GetSas("Public", "admin");
                this.QueueSas = this.StorageAccount.CreateCloudQueueClient().GetQueueReference("messagequeue").GetSharedAccessSignature(
                        new SharedAccessQueuePolicy() {  },
                        "add"
                        );
            }
            else
            {
                this.PublicTableSas = photoContextAdmin.GetSas("Public", "edit");
                this.AuthenticatedTableSas = null;
                this.QueueSas = null;
            }
        }
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            CloudTableClient cloudTableClientAdmin = this.StorageAccount.CreateCloudTableClient();
            var photoContextAdmin = new PhotoDataServiceContext(cloudTableClientAdmin);

            if (this.User.Identity.IsAuthenticated)
            {
                this.AuthenticatedTableSas = photoContextAdmin.GetSas(this.User.Identity.Name, SharedAccessTablePermissions.Add | SharedAccessTablePermissions.Delete | SharedAccessTablePermissions.Query | SharedAccessTablePermissions.Update);
                this.PublicTableSas = photoContextAdmin.GetSas("Public", SharedAccessTablePermissions.Add | SharedAccessTablePermissions.Delete | SharedAccessTablePermissions.Query | SharedAccessTablePermissions.Update);
                this.QueueSas = this.StorageAccount.CreateCloudQueueClient().GetQueueReference("messagequeue").GetSharedAccessSignature(
                   new SharedAccessQueuePolicy() { Permissions = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.Read, SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(15) },
                   null
                   );
            }
            else
            {
                this.PublicTableSas = photoContextAdmin.GetSas("Public", SharedAccessTablePermissions.Add | SharedAccessTablePermissions.Update | SharedAccessTablePermissions.Query);
                this.AuthenticatedTableSas = null;
                this.QueueSas = null;
            }
        }
        //
        // GET: /
        public ActionResult Index()
        {
            CloudTableClient cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(this.PublicTableSas));
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            var photoList = new List<PhotoViewModel>();

            var photos = photoContext.GetPhotos();
            if (photos.Count() > 0)
            {
                photoList = photos.Select(x => this.ToViewModel(x)).ToList();
            }

            var privatePhotos = new List<PhotoViewModel>();

            if (this.User.Identity.IsAuthenticated)
            {
                cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(this.AuthenticatedTableSas));
                photoContext = new PhotoDataServiceContext(cloudTableClient);

                photos = photoContext.GetPhotos();
                if (photos.Count() > 0)
                {
                    photoList.AddRange(photos.Select(x => this.ToViewModel(x)).ToList());
                }
            }

            return this.View(photoList);
        }
        public ActionResult Edit(PhotoViewModel photoViewModel, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                var photo = this.FromViewModel(photoViewModel);

                var token = photoViewModel.PartitionKey == "Public" ? this.PublicTableSas : this.AuthenticatedTableSas;

                CloudTableClient cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(token));
                var photoContext = new PhotoDataServiceContext(cloudTableClient);
                photoContext.UpdatePhoto(photo);

                return this.RedirectToAction("Index");
            }

            return this.View();
        }
        public ActionResult DeleteConfirmed(string partitionKey, string rowKey)
        {
            var token = partitionKey == "Public" ? this.PublicTableSas : this.AuthenticatedTableSas;
            CloudTableClient cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(this.PublicTableSas));
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            PhotoEntity photo = photoContext.GetById(partitionKey, rowKey);
            photoContext.DeletePhoto(photo);

            //Deletes the Image from Blob Storage
            if (!string.IsNullOrEmpty(photo.BlobReference))
            {
                var blob = this.GetBlobContainer().GetBlockBlobReference(photo.BlobReference);
                blob.DeleteIfExists();
            }

            //Send delete notification
            var msg = new CloudQueueMessage("Photo Deleted");
            this.GetCloudQueue().AddMessage(msg);

            return this.RedirectToAction("Index");
        }
Example #11
0
 private PhotoDataServiceContext GetPhotoContext()
 {
     var sasToken = SasService.GetSasForTable(this.User.Identity.Name);
     var cloudTableClient = new CloudTableClient(this.uriTable, new StorageCredentials(sasToken));
     var photoContext = new PhotoDataServiceContext(cloudTableClient);
     return photoContext;
 }
        public ActionResult DeleteConfirmed(string id)
        {
            this.RefreshAccessCredentials();

            //Delete information From Table Storage
            CloudTableClient cloudTableClient = new CloudTableClient(this.Uri, new StorageCredentials(Session["Sas"].ToString()));
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            PhotoEntity photo = photoContext.GetById("Public", id);

            if (photo == null)
            {
                photo = photoContext.GetById(this.User.Identity.Name, id);
                if (photo == null)
                {
                    return this.HttpNotFound();
                }
            }
            photoContext.DeletePhoto(photo);

            //Deletes the Image from Blob Storage
            if (!string.IsNullOrEmpty(photo.BlobReference))
            {
                var blob = this.GetBlobContainer().GetBlockBlobReference(photo.BlobReference);
                blob.DeleteIfExists();
            }

            //Send delete notification
            var msg = new CloudQueueMessage("Photo Deleted");
            this.GetCloudQueue().AddMessage(msg);

            return this.RedirectToAction("Index");
        }
        public void RefreshAccessCredentials()
        {
            if ((Session["ExpireTime"] as DateTime? == null) || ((DateTime)Session["ExpireTime"] < DateTime.UtcNow))
            {
                CloudTableClient cloudTableClientAdmin = this.StorageAccount.CreateCloudTableClient();
                var photoContextAdmin = new PhotoDataServiceContext(cloudTableClientAdmin);

                Session["Sas"] = photoContextAdmin.GetSas("readonly", "Public");

                if (this.User != null)
                {
                    Session["MySas"] = photoContextAdmin.GetSas("admin", this.User.Identity.Name);
                    Session["Sas"] = photoContextAdmin.GetSas("admin", "Public");
                }

                Session["ExpireTime"] = DateTime.UtcNow.AddMinutes(15);
            }
        }
        //
        // GET: /Home/Delete/5
        public ActionResult Delete(string partitionKey, string rowKey)
        {
            var cloudTableClient = this.storageAccount.CreateCloudTableClient();
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            PhotoEntity photo = photoContext.GetById(partitionKey, rowKey);

            if (photo == null)
            {
                return HttpNotFound();
            }

            var viewModel = ToViewModel(photo);
            if (!String.IsNullOrEmpty(photo.BlobReference))
            {
                viewModel.Uri = GetBlobContainer().GetBlockBlobReference(photo.BlobReference).Uri.ToString();
            }

            // Send create notification
            try
            {
                var msg = new CloudQueueMessage("Photo Deleted");
                GetCloudQueue().AddMessage(msg);
            }
            catch (Exception ex)
            {
                Trace.TraceError("Failed to send notification: {0}", ex.Message);
            }

            return View(viewModel);
        }
        public ActionResult DeleteConfirmed(string partitionKey, string rowKey)
        {
            var cloudTableClient = this.storageAccount.CreateCloudTableClient();
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            var photo = photoContext.GetById(partitionKey, rowKey);
            photoContext.DeletePhoto(photo);

            if (!String.IsNullOrEmpty(photo.BlobReference))
            {
                var blob = GetBlobContainer().GetBlockBlobReference(photo.BlobReference);
                blob.DeleteIfExists();
            }

            return RedirectToAction("Index");
        }
        //
        // GET: /
        public ActionResult Index()
        {
            CloudTableClient cloudTableClient = this.StorageAccount.CreateCloudTableClient();
            var photoContext = new PhotoDataServiceContext(cloudTableClient);

            return this.View(photoContext.GetPhotos().Select(x => this.ToViewModel(x)).ToList());
        }
        public ActionResult Edit(PhotoViewModel photoViewModel, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                var photo = this.FromViewModel(photoViewModel);

                //Update information in Table Storage
                CloudTableClient cloudTableClient = this.StorageAccount.CreateCloudTableClient();
                var photoContext = new PhotoDataServiceContext(cloudTableClient);
                photoContext.UpdatePhoto(photo);

                return this.RedirectToAction("Index");
            }

            return this.View();
        }
        public ActionResult DeleteConfirmed(string partitionKey, string rowKey)
        {
            CloudTableClient cloudTableClient = this.StorageAccount.CreateCloudTableClient();
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            PhotoEntity photo = photoContext.GetById(partitionKey, rowKey);
            photoContext.DeletePhoto(photo);

            //Deletes the Image from Blob Storage
            if (!string.IsNullOrEmpty(photo.BlobReference))
            {
                var blob = this.GetBlobContainer().GetBlockBlobReference(photo.BlobReference);
                blob.DeleteIfExists();
            }

            try
            {
                //Send delete notification
                var msg = new CloudQueueMessage("Photo Deleted");
                this.GetCloudQueue().AddMessage(msg);
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.TraceInformation("Error", "Couldn't send notification");
            }

            return this.RedirectToAction("Index");
        }
        public ActionResult Delete(string id)
        {
            this.RefreshAccessCredentials();

            CloudTableClient cloudTableClient = new CloudTableClient(this.Uri, new StorageCredentials(Session["Sas"].ToString()));
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            PhotoEntity photo = photoContext.GetById("Public", id);

            if (photo == null)
            {
                photo = photoContext.GetById(this.User.Identity.Name, id);
                if (photo == null)
                {
                    return this.HttpNotFound();
                }
            }

            var viewModel = this.ToViewModel(photo);
            if (!string.IsNullOrEmpty(photo.BlobReference))
            {
                viewModel.Uri = this.GetBlobContainer().GetBlockBlobReference(photo.BlobReference).Uri.ToString();
            }

            return this.View(viewModel);
        }
        public ActionResult Share(string partitionKey, string rowKey)
        {
            var cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(this.AuthenticatedTableSas));
            var photoContext = new PhotoDataServiceContext(cloudTableClient);

            PhotoEntity photo = photoContext.GetById(partitionKey, rowKey);
            if (photo == null)
            {
                return this.HttpNotFound();
            }

            string sas = string.Empty;
            if (!string.IsNullOrEmpty(photo.BlobReference))
            {
                CloudBlockBlob blobBlockReference = this.GetBlobContainer().GetBlockBlobReference(photo.BlobReference);
                sas = photoContext.GetSasForBlob(blobBlockReference, SharedAccessBlobPermissions.Read);
            }

            if (!string.IsNullOrEmpty(sas))
            {
                return View("Share", null, sas);
            }

            return RedirectToAction("Index");
        }
        public ActionResult ToPublic(string partitionKey, string rowKey)
        {
            CloudTableClient cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(this.AuthenticatedTableSas));
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            var photo = photoContext.GetById(partitionKey, rowKey);
            if (photo == null)
            {
                return this.HttpNotFound();
            }

            photoContext.DeletePhoto(photo);

            cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(this.PublicTableSas));
            photoContext = new PhotoDataServiceContext(cloudTableClient);
            photo.PartitionKey = "Public";
            photoContext.AddPhoto(photo);

            return RedirectToAction("Index");
        }
        //
        // GET: /Home/Delete/5
        public ActionResult Delete(string partitionKey, string rowKey)
        {
            CloudTableClient cloudTableClient = this.StorageAccount.CreateCloudTableClient();
            var photoContext = new PhotoDataServiceContext(cloudTableClient);
            PhotoEntity photo = photoContext.GetById(partitionKey, rowKey);

            if (photo == null)
            {
                return this.HttpNotFound();
            }

            var viewModel = this.ToViewModel(photo);
            if (!string.IsNullOrEmpty(photo.BlobReference))
            {
                viewModel.Uri = this.GetBlobContainer().GetBlockBlobReference(photo.BlobReference).Uri.ToString();
            }

            return this.View(viewModel);
        }
        public ActionResult DeleteConfirmed(string partitionKey, string rowKey)
        {
            if (ModelState.IsValid)
            {
                string token = partitionKey == "Public" ? this.PublicTableSas : this.AuthenticatedTableSas;

                CloudTableClient cloudTableClient = new CloudTableClient(this.UriTable, new StorageCredentials(token));
                var photoContext = new PhotoDataServiceContext(cloudTableClient);
                PhotoEntity photo = photoContext.GetById(partitionKey, rowKey);

                if (photo == null)
                {
                    return this.HttpNotFound();
                }

                photoContext.DeletePhoto(photo);

                //Deletes the Image from Blob Storage
                if (!string.IsNullOrEmpty(photo.BlobReference))
                {
                    var blob = this.GetBlobContainer().GetBlockBlobReference(photo.BlobReference);
                    blob.DeleteIfExists();
                }

                try
                {
                    //Send delete notification
                    var msg = new CloudQueueMessage(string.Format("Photo Deleted,{0}", photo.BlobReference));
                    this.GetCloudQueue().AddMessage(msg);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.TraceInformation("Error", "Couldn't send notification");
                }

            }
            return this.RedirectToAction("Index");
        }
        //
        // GET: /
        public ActionResult Index()
        {
            this.RefreshAccessCredentials();

            CloudTableClient cloudTableClient = new CloudTableClient(this.Uri, new StorageCredentials(Session["Sas"].ToString()));
            var photoContext = new PhotoDataServiceContext(cloudTableClient);

            var publicPhotos = photoContext.GetPhotos("Public").Select(x => this.ToViewModel(x)).ToList();
            var privatePhotos = new List<PhotoViewModel>();

            if (this.User != null)
            {
                privatePhotos = photoContext.GetPhotos(this.User.Identity.Name).Select(x => this.ToViewModel(x)).ToList();
            }

            return this.View();
        }