Beispiel #1
0
        public async Task<ActionResult> Create(PhotoViewModel photoViewModel, HttpPostedFileBase file, FormCollection collection)
        {
            if (!this.ModelState.IsValid)
            {
                return this.View();
            }

            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;
                await blob.UploadFromStreamAsync(file.InputStream);
                photo.BlobReference = file.FileName;
            }
            else
            {
                this.ModelState.AddModelError("File", new ArgumentNullException("file"));
                return this.View(photoViewModel);
            }

            // Save information to Table Storage
            var photoContext = this.GetPhotoContext();
            await photoContext.AddPhotoAsync(photo);

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

            return this.RedirectToAction("Index");
        }
        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 (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();
        }
        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();
        }
        private PhotoEntity FromViewModel(PhotoViewModel photoViewModel)
        {
            var photo = new PhotoEntity
                {
                    Title = photoViewModel.Title,
                    Description = photoViewModel.Description
                };

            photo.PartitionKey = photoViewModel.PartitionKey ?? photo.PartitionKey;
            photo.RowKey = photoViewModel.RowKey ?? photo.RowKey;
            return photo;
        }
        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();
        }
Beispiel #8
0
 private PhotoEntity FromViewModel(PhotoViewModel photoViewModel)
 {
     var photo = new PhotoEntity(this.User.Identity.Name);
     photo.RowKey = photoViewModel.RowKey ?? photo.RowKey;
     photo.Title = photoViewModel.Title;
     photo.Description = photoViewModel.Description;
     return photo;
 }
Beispiel #9
0
        public async Task<ActionResult> Edit(PhotoViewModel photoViewModel, FormCollection collection)
        {
            if (!ModelState.IsValid)
            {
                return this.View();
            }

            var photoContext = this.GetPhotoContext();
            var entityToUpdate = await photoContext.GetByIdAsync(photoViewModel.PartitionKey, photoViewModel.RowKey);

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

            // Update entity information from ViewModel
            entityToUpdate.Title = photoViewModel.Title;
            entityToUpdate.Description = photoViewModel.Description;
            await photoContext.UpdatePhotoAsync(entityToUpdate);

            return this.RedirectToAction("Index");
        }
 public ActionResult Edit(PhotoViewModel photoViewModel, FormCollection collection)
 {
     return View();
 }
 public ActionResult Create(PhotoViewModel photoViewModel, HttpPostedFileBase file, FormCollection collection)
 {
     return View();
 }
        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();
        }