Пример #1
0
        public string GetUserVideos(int topicId, int questionId)
        {
            var userId        = CommonController.GetCurrentUserId();
            var userQuestions = new List <VideoDetail>();

            using (var db = new AmandaDevEntities())
            {
                userQuestions = db.Database
                                .SqlQuery <VideoDetail>("[dbo].[GetTopicVideos] @UserId, @TopicId, @QuestionId", new SqlParameter("@UserId", userId), new SqlParameter("@TopicId", topicId), new SqlParameter("@QuestionId", questionId)).ToList();
            }
            return(JsonConvert.SerializeObject(userQuestions));
        }
Пример #2
0
        public JsonResult UploadPhoto(string formValues, HttpPostedFileBase uploadFile)
        {
            var uploadResult  = new UploadReturnResult();
            var azureUploader = new AzureUploader();
            int thumbHeight   = 54;
            int fullHeight    = 600;
            int thumbWidth;
            int fullWidth;

            try
            {
                var uploadFormValues = JsonConvert.DeserializeObject <UploadDataValuesModel>(formValues);

                if (uploadFile.ContentLength > 0)
                {
                    var normalMap = (Bitmap)Bitmap.FromStream(uploadFile.InputStream);

                    thumbWidth = (thumbHeight * normalMap.Width) / normalMap.Height;
                    var thumbMap = new Bitmap(normalMap, new Size(thumbWidth, thumbHeight));

                    //fullWidth = (fullHeight * normalMap.Width) / normalMap.Height;
                    //var fullMap = new Bitmap(normalMap, new Size(fullWidth, fullHeight));

                    var key          = Guid.NewGuid().ToString();
                    var keyName      = key + ".png";
                    var thumbKeyName = key + "_thumb.png";

                    var imageStream = new MemoryStream();
                    normalMap.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
                    imageStream.Position = 0;
                    using (var fileStream = imageStream)
                    {
                        uploadResult = azureUploader.UploadPhoto(keyName, fileStream);
                    }

                    if (uploadResult.Success)
                    {
                        var thumbImageStream = new MemoryStream();
                        thumbMap.Save(thumbImageStream, System.Drawing.Imaging.ImageFormat.Png);
                        thumbImageStream.Position = 0;
                        using (var thumbFileStream = thumbImageStream)
                        {
                            uploadResult = azureUploader.UploadPhoto(thumbKeyName, thumbFileStream);
                        }
                    }

                    if (!uploadResult.Success)
                    {
                        return(Json(new
                        {
                            Successful = false,
                            ErrorMessage = uploadResult.Message
                        }));
                    }

                    using (var db = new AmandaDevEntities())
                    {
                        var questions = new List <Question>();
                        if (uploadFormValues.questionId != "0")
                        {
                            questions = new List <Question> {
                                new Question {
                                    Id = Convert.ToInt32(uploadFormValues.questionId)
                                }
                            };
                        }
                        db.ExternalMedias.Add(new ExternalMedia
                        {
                            Key          = keyName,
                            ThumbKey     = thumbKeyName,
                            ThumbUrl     = "https://tellyt.blob.core.windows.net/photos/" + thumbKeyName,
                            Url          = "https://tellyt.blob.core.windows.net/photos/" + keyName,
                            Type         = "Photo",
                            LastModified = DateTime.Now,
                            UserId       = CommonController.GetCurrentUserId(),
                            MediaType    = "Image",
                            Questions    = questions,
                            Name         = keyName
                        });
                        db.SaveChanges();
                    }

                    return(Json(new
                    {
                        Successful = uploadResult.Success,
                        ErrorMessage = uploadResult.Message
                    }));
                }
                else
                {
                    return(Json(new
                    {
                        Successful = false,
                        ErrorMessage = "The file you uploaded was empty."
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    Successful = false,
                    ErrorMessage = "There was an error uploading your file. Please contact technical support and provide the following error message: " + ex.Message
                }));
            }
        }
Пример #3
0
        //[HttpPost]
        //public JsonResult GetPhotos()
        //{
        //  var returnList = new List<PhotoResult>();
        //  var userId = CommonController.GetCurrentUserId();
        //  using (var db = new AmandaDevEntities())
        //  {
        //    foreach(var externalMedia in db.ExternalMedias.Where(e => e.UserId == userId && e.Type == "Photo"))
        //    {
        //      //var thumbUrlRequest = new GetPreSignedUrlRequest().....
        //      //var thumbUrl =
        //    }
        //  }
        //}

        private async Task SaveVideo(string formValues, HttpPostedFileBase uploadFile)
        {
            var uploadResult = new UploadReturnResult();

            var azureUploader        = new AzureUploader();
            var videoCreationSuccess = false;

            var uploadFormValues = JsonConvert.DeserializeObject <UploadDataValuesModel>(formValues);
            var b            = new BinaryReader(uploadFile.InputStream);
            var byteData     = b.ReadBytes(uploadFile.ContentLength);
            var memoryStream = new MemoryStream(byteData);

            memoryStream.Seek(0, SeekOrigin.Begin);

            using (var fileStream = memoryStream)
            {
                uploadResult = await azureUploader.UploadWebcamVideo(uploadFormValues.fileName, fileStream);

                if (uploadResult.Success)
                {
                    var videoEncoderUrl = ConfigurationManager.AppSettings["VideoEncoderUrl"] + "/convertvideo";
                    var httpClient      = new HttpClient();
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    ServicePointManager.SecurityProtocol =
                        SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                    var convertVideoInput = new ConvertVideoInput
                    {
                        inputFileType = "webm", outputFileType = "mp4", keyName = uploadFormValues.fileName, thumbnailSize = "720x405"
                    };
                    var postBody = JsonConvert.SerializeObject(convertVideoInput);

                    var convertVideoResponse = httpClient.PostAsync(videoEncoderUrl, new StringContent(postBody, Encoding.UTF8, "application/json")).Result;
                    if (convertVideoResponse.IsSuccessStatusCode)
                    {
                        var resultString = convertVideoResponse.Content.ReadAsStringAsync().Result;
                        var result       = JsonConvert.DeserializeObject <ValidationResult>(resultString);

                        if (result.valid)
                        {
                            var durationSeconds = Convert.ToDouble(result.duration);
                            //Convert to hours:minutes:seconds
                            TimeSpan time         = TimeSpan.FromSeconds(durationSeconds);
                            var      recordedTime = time.ToString(@"mm\:ss");

                            var questionNumber = Convert.ToInt32(uploadFormValues.questionId);

                            using (var db = new AmandaDevEntities())
                            {
                                var answeredQuestion = db.Questions.Where(q => q.Id == questionNumber).ToList();
                                db.Videos.Add(new Video
                                {
                                    AccountHash     = "videos",
                                    Stream          = uploadFormValues.fileName,
                                    ExternalVideoId = 0,
                                    Location        = "tellyt.blob.core.windows.net",
                                    Questions       = answeredQuestion,
                                    UserId          = CommonController.GetCurrentUserId(),
                                    RecordedTime    = recordedTime,
                                    type            = "MP4",
                                    LastModified    = DateTime.Now
                                });
                                db.SaveChanges();
                            }
                        }
                    }
                }
            }
        }