Ejemplo n.º 1
0
        public FileStreamResult StreamPrivate(Guid id)
        {
            string videoSecurity = TempData["VideoSecurity"] as string;

            if (!("VideoEdit" == videoSecurity || "MessageFromUrl" == videoSecurity || "MessagePreview" == videoSecurity))
            {
                throw new Exception("Video request is not authorized.");
            }

            Video vid = _videoRepository.GetById(id);

            BlobContainer container = AzureUtility.GetAzureContainer(vid.Path);

            BlobContents   contents = new BlobContents(new MemoryStream());
            BlobProperties blob     = container.GetBlob(vid.Path.Substring(vid.Path.IndexOf("/") + 1), contents, false);

            if (blob.ContentType == null)
            {
                throw new FormatException("No content type set for blob.");
            }

            Stream stream = contents.AsStream;

            stream.Seek(0, SeekOrigin.Begin);
            return(File(stream, blob.ContentType));
        }
Ejemplo n.º 2
0
        public ActionResult Delete(Guid id, string confirmButton)
        {
            Video vid = _videoRepository.GetById(id);

            if (null == vid)
            {
                return(View("NotFound"));
            }

            //Should also delete the physical file on the server
            BlobContainer container = AzureUtility.GetAzureContainer(vid.Path);

            container.DeleteBlob(vid.Path.Substring(vid.Path.IndexOf("/") + 1));

            _videoRepository.Delete(vid);
            _videoRepository.Save();

            return(View("Deleted"));
        }
Ejemplo n.º 3
0
        private string PostProcessVideo(UploadStatus status, decimal totalCount)
        {
            string resultMessage = String.Empty;
            string localFilePath = String.Empty;
            int    counter       = 0;

            //Setup the Azure Storage Container
            BlobContainer container = AzureUtility.GetAzureContainer(null);

            foreach (UploadedFile file in status.GetUploadedFiles())
            {
                try
                {
                    localFilePath = file.LocationInfo["fileName"].ToString();

                    //Save a skeleton record of the upload in the database
                    Video vid = _videoRepository.New();
                    _videoRepository.AddVideoToUser(vid, User.Identity.Name);
                    vid.Description         = file.FormValues["fileDescription"];
                    vid.StartProcessingDate = DateTime.Now;
                    vid.OriginalFileFormat  = file.ContentType;
                    vid.UploadSize          = file.ContentLength;
                    _videoRepository.Save();
                    Guid videoId = vid.Id;

                    string encodedFilePath = FFmpegEncoder.EncodeToWmv(localFilePath, file.ContentType);

                    if (!String.IsNullOrEmpty(encodedFilePath))
                    {
                        string fileNameOnly = encodedFilePath.Substring(encodedFilePath.LastIndexOf("Upload") + 7);

                        //TODO: Take a screenshot/Thumbnail of the video & upload it

                        BlobProperties properties = AzureUtility.CollectBlobMetadata(file, fileNameOnly, User.Identity.Name);

                        // Create the blob & Upload
                        long finalSize = 0;
                        using (FileStream uploadedFile = new FileStream(encodedFilePath, FileMode.Open, FileAccess.Read))
                        {
                            BlobContents fileBlob = new BlobContents(uploadedFile);
                            finalSize = fileBlob.AsStream.Length;
                            container.CreateBlob(properties, fileBlob, true);
                        }

                        //Create the database record for this video
                        vid = _videoRepository.GetById(videoId);
                        vid.CreationDate = DateTime.Now;
                        vid.FinalSize    = finalSize;
                        vid.Path         = String.Format("{0}/{1}", container.ContainerName, fileNameOnly);
                        _videoRepository.Save();

                        resultMessage = string.Format("Your video ({0}) is ready for you to use.", file.FormValues["fileDescription"]);

                        //Delete the local copy of the encoded file
                        System.IO.File.Delete(encodedFilePath);
                    }
                    else
                    {
                        resultMessage = "ERROR: video (" + file.FormValues["fileDescription"] + ") could not be converted to a recognizable video format.";
                    }

                    //Create a notification record so the user knows that processing is done for this video
                    Notification note = _notificationRepository.New();
                    _notificationRepository.AddNotificationToUser(note, User.Identity.Name);
                    note.UserNotified = false;
                    note.Message      = resultMessage;
                    note.CreationDate = DateTime.Now;
                    _notificationRepository.Save();
                }
                catch (Exception ex)
                {
                    resultMessage = string.Format("ERROR: we tried to process the video, {0}, but it did not finish.  You might want to try again.", file.FormValues["fileDescription"]);

                    //Create a notification record so the user knows that there was an error
                    Notification note = _notificationRepository.New();
                    _notificationRepository.AddNotificationToUser(note, User.Identity.Name);
                    note.UserNotified = false;
                    note.Message      = resultMessage;
                    note.CreationDate = DateTime.Now;
                    _notificationRepository.Save();

                    throw new Exception(resultMessage, ex);
                }
                finally
                {
                    //Delete the local copy of the original file
                    System.IO.File.Delete(localFilePath);

                    counter++;
                }
            }

            return(resultMessage);
        }