예제 #1
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);
        }