Example #1
0
        public ActionResult Upload(string title, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var mimeTypeExtension = new MimeTypeExtension();
                    MimeType mimeType = mimeTypeExtension.GetType(file.ContentType);

                    if (mimeType == MimeType.Photo)
                    {
                        var newPhoto = new Photo()
                        {
                            Id = file.FileName + Guid.NewGuid().ToString(),
                            FileName = file.FileName,
                            ContentType = file.ContentType,
                            Title = title,
                            DateAdded = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(),
                            UploadedBy = User.Identity.Name
                        };

                        // Lätt till ett jobb för worker rolen som ska skapa en thumbnail
                        JobQueue jobQueue = new JobQueue();
                        jobQueue.AddJob(newPhoto.RowKey);

                        // Ladda upp till blob
                        var blobContainer = new BlobContainer("rawimages");
                        newPhoto.RawUri = blobContainer.UploadBlob(file.InputStream, newPhoto.RowKey, file.ContentType);

                        // Lägg till i tabellen
                        TableAccess tableAccess = new TableAccess("photoinformation");
                        tableAccess.Add(newPhoto);

                        ViewData["Success"] = "Your photo were uploaded successfully!";
                        return RedirectToAction("Index");
                    }
                    if (mimeType == MimeType.Document)
                    {
                        var newDocument = new Document()
                        {
                            Id = file.FileName + Guid.NewGuid().ToString(),
                            FileName = file.FileName,
                            ContentType = file.ContentType,
                            Title = title,
                            DateAdded = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(),
                            ContentLength = file.ContentLength / 1024 + "kb",
                            UploadedBy = User.Identity.Name
                        };

                        // Ladda upp till blob
                        var blobContainer = new BlobContainer("documents");
                        newDocument.RawUri = blobContainer.UploadBlob(file.InputStream, newDocument.RowKey, file.ContentType);

                        // Lägg till i tabellen
                        TableAccess tableAccess = new TableAccess("documentinformation");
                        tableAccess.Add(newDocument);

                        return RedirectToAction("Index", "Document");
                    }
                    if (mimeType == MimeType.Video)
                    {
                        var newVideo = new Video()
                        {
                            Id = file.FileName + Guid.NewGuid().ToString(),
                            FileName = file.FileName,
                            ContentType = file.ContentType,
                            Title = title,
                            DateAdded = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(),
                            UploadedBy = User.Identity.Name
                        };

                        // Ladda upp till blob
                        var blobContainer = new BlobContainer("videos");
                        newVideo.RawUri = blobContainer.UploadBlob(file.InputStream, newVideo.RowKey, file.ContentType);

                        // Lägg till i tabellen
                        TableAccess tableAccess = new TableAccess("videoinformation");
                        tableAccess.Add(newVideo);

                        return RedirectToAction("Index", "Video");
                    }
                    if (mimeType == MimeType.Audio)
                    {
                        var newAudio = new Audio()
                        {
                            Id = file.FileName + Guid.NewGuid().ToString(),
                            FileName = file.FileName,
                            ContentType = file.ContentType,
                            Title = title,
                            DateAdded = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(),
                            UploadedBy = User.Identity.Name
                        };

                        // Ladda upp till blob
                        var blobContainer = new BlobContainer("audios");
                        newAudio.RawUri = blobContainer.UploadBlob(file.InputStream, newAudio.RowKey, file.ContentType);

                        // Lägg till i tabellen
                        TableAccess tableAccess = new TableAccess("audioinformation");
                        tableAccess.Add(newAudio);

                        return RedirectToAction("Index", "Audio");
                    }
                    if (mimeType == MimeType.NotAccepted)
                        ViewData["Error"] = "Unsupported content, try another! (maybe i´ve missed to add it as supported)";

                }
                catch (Exception ex)
                {
                    ViewData["Error"] = "Error: " + ex.Message;
                }
            }

            return View("Upload");
        }
Example #2
0
        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.TraceInformation("PhotoWorkerRole entry point called", "Information");
            var jobQueue = new JobQueue();

            while (true)
            {
                CloudQueueMessage jobMessage = jobQueue.Get();

                if (jobMessage != null)
                {
                    // Om det finns ett message
                    string jobName = jobMessage.AsString;
                    Trace.TraceInformation("Found job: " + jobName, "Information");

                    try
                    {
                        // Create a new / reference to a image blob
                        BlobContainer rawContainer = new BlobContainer("rawimages");
                        BlobContainer thumbContainer = new BlobContainer("thumbimages");

                        // Hämta bloben från rawimages containern
                        CloudBlockBlob rawBlob = rawContainer.GetBlob(jobName);
                        Trace.TraceInformation("Image to process: " + rawBlob.Name, "Information");

                        // Get the image information from table
                        TableAccess tableAccess = new TableAccess("photoinformation");
                        Photo photo = tableAccess.Get(jobName);

                        // Create a new image processor for the image
                        ImageProcessor imgProc = new ImageProcessor(photo.RawUri, photo.Title);

                        // Skapa en thumbnail
                        Trace.WriteLine("Processing job " + jobName);
                        Image thumb = imgProc.CreateTile(new Rectangle(0, 0, imgProc.Width, imgProc.Height));

                        // Encoder parameter for image quality
                        EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L);

                        // Set the Jpeg image codec
                        //ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
                        ImageCodecInfo jpegCodec = GetEncoderInfo(photo.ContentType);

                        EncoderParameters encoderParams = new EncoderParameters(1);
                        encoderParams.Param[0] = qualityParam;

                        // Upload the image
                        MemoryStream s = new MemoryStream();
                        thumb.Save(s, jpegCodec, encoderParams);
                        byte[] buffer = new byte[s.Length];
                        s.Read(buffer, 0, (int)s.Length);
                        s.Position = 0;

                        // Lägg till thumben i thumbcontainer
                        Trace.WriteLine("Thumbnail to add: " + photo.RowKey);
                        var thumbUri = thumbContainer.UploadBlob(s, photo.RowKey, photo.ContentType);
                        //var thumbUri = thumbContainer.AddThumbnailToContainer(photo.RowKey, thumb, rawBlob);

                        // Update the information of photo in the table
                        photo.ThumbUri = thumbUri;
                        tableAccess.Update(photo);

                        // Delete the message
                        jobQueue.Delete(jobMessage);
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine("Error processing job " + jobName + ": " +
                                        ex.Message, "Error");
                    }
                }
                else
                {
                    Trace.TraceInformation("Polling for job...", "Information");
                    Thread.Sleep(1000);
                }
            }
        }