public async Task <ActionResult> Create([Bind(Include = "fileId,destinationEmail")] FileConversionCommon.File file, HttpPostedFileBase uploadFile) { CloudBlockBlob imageBlob = null; // A production app would implement more robust input validation. // For example, validate that the image file size is not too large. if (ModelState.IsValid) { if (uploadFile != null && uploadFile.ContentLength != 0) { imageBlob = await UploadAndSaveBlobAsync(uploadFile); file.fileURL = imageBlob.Uri.ToString(); } file.postedDate = DateTime.Now; db.Files.Add(file); await db.SaveChangesAsync(); Trace.TraceInformation("Created AdId {0} in database", file.fileId); if (imageBlob != null) { var queueMessage = new CloudQueueMessage(file.fileId.ToString()); await filesQueue.AddMessageAsync(queueMessage); Trace.TraceInformation("Created queue message for AdId {0}", file.fileId); } return(RedirectToAction("Index")); } return(View(file)); }
private void ProcessQueueMessage(CloudQueueMessage msg) { Trace.TraceInformation("Processing queue message {0}", msg); // Queue message contains AdId. var fileId = int.Parse(msg.AsString); FileConversionCommon.File f = db.Files.Find(fileId); if (f == null) { throw new Exception(String.Format("AdId {0} not found, can't create thumbnail", fileId.ToString())); } Uri blobUri = new Uri(f.fileURL); string blobName = blobUri.Segments[blobUri.Segments.Length - 1]; CloudBlockBlob inputBlob = this.filesBlobContainer.GetBlockBlobReference(blobName); string thumbnailName = Path.GetFileNameWithoutExtension(inputBlob.Name) + "thumb.jpg"; CloudBlockBlob outputBlob = this.filesBlobContainer.GetBlockBlobReference(thumbnailName); using (Stream input = inputBlob.OpenRead()) using (Stream output = outputBlob.OpenWrite()) { ConvertImageToThumbnailJPG(input, output); outputBlob.Properties.ContentType = "image/jpeg"; } Trace.TraceInformation("Generated thumbnail in blob {0}", thumbnailName); f.fileURL = outputBlob.Uri.ToString(); db.SaveChanges(); Trace.TraceInformation("Updated thumbnail URL in database: {0}", f.fileURL); // Remove message from queue. this.filesQueue.DeleteMessage(msg); }
public async Task <ActionResult> DeleteConfirmed(int id) { FileConversionCommon.File file = await db.Files.FindAsync(id); db.Files.Remove(file); await db.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <ActionResult> Edit([Bind(Include = "fileId,fileURL,convertedFilelURL,postedDate,destinationEmail")] FileConversionCommon.File file) { if (ModelState.IsValid) { db.Entry(file).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(file)); }
// GET: Files/Details/5 public async Task <ActionResult> Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } FileConversionCommon.File file = await db.Files.FindAsync(id); if (file == null) { return(HttpNotFound()); } return(View(file)); }