Exemplo n.º 1
0
        public void DownloadNotExistVideoTest()
        {
            //Arrange
            string localFilePath = null;
            
            _fileSystemWrapper.Setup(m => m.GetTempPath()).Returns("my temp path");
            _fileSystemWrapper.Setup(m => m.PathCombine(It.IsAny<string>(), It.IsAny<string>())).Returns<string, string>(Path.Combine);

            _videoiRepository.Setup(m => m.ExistsEncodedVideo(It.IsAny<string>())).Returns(false);
            _videoiRepository.Setup(m => m.DownloadOriginalVideo(It.IsAny<string>(), It.IsAny<string>())).Callback<string, string>((hash, filePath) => localFilePath = filePath);

            var queueInformation = new QueueInformation()
            {
                VideoMessage = new VideoMessage()
            };
            
            var downloadProcess = new DownloadProcess(5, _videoiRepository.Object, _fileSystemWrapper.Object);

            //Act
            downloadProcess.ProcessMethod(queueInformation, new CancellationToken());

            //Assert
            _videoiRepository.Verify(m => m.SetEncodingState(It.IsAny<string>(), EncodingState.InProcess, EncodingStage.Downloading, null), Times.Once());
            _videoiRepository.Verify(m=>m.ExistsEncodedVideo(It.IsAny<string>()), Times.Once());
            _videoiRepository.Verify(m => m.DownloadOriginalVideo(It.IsAny<string>(), localFilePath), Times.Once());
        }
Exemplo n.º 2
0
        public void DownloadSuccessfulTest()
        {
            //Arrange
            string localFilePath = null;

            _videoiRepository.Setup(m => m.DownloadOriginalVideo(It.IsAny<string>(), It.IsAny<string>())).Callback<string, string>((hash, filePath) => localFilePath = filePath);
            
            _fileSystemWrapper.Setup(m => m.GetTempPath()).Returns("my temp path");
            _fileSystemWrapper.Setup(m => m.PathCombine(It.IsAny<string>(), It.IsAny<string>())).Returns<string,string>(Path.Combine);
            
            var queueInformation = new QueueInformation()
                                       {
                                           VideoMessage = new VideoMessage()
                                       };
            

            var downloadProcess = new DownloadProcess(5, _videoiRepository.Object, _fileSystemWrapper.Object);
            
            //Act
            var downloadInfo =  downloadProcess.ProcessMethod(queueInformation, new CancellationToken());

            //Assert
            Assert.AreEqual(queueInformation.VideoMessage, downloadInfo.QueueInformation.VideoMessage);
            Assert.AreEqual(localFilePath, downloadInfo.LocalFilePath);
            Assert.AreEqual(Path.GetDirectoryName(localFilePath), downloadInfo.LocalPath);
        }
        public void EncodeTest()
        {
            //Arrange
            var cloudStorageConfiguration = new CloudStorageConfiguration(CloudStorageAccount.DevelopmentStorageAccount, new Version());

            var queueVideoRepository = new QueueVideoRepository(cloudStorageConfiguration.StorageAccount.CreateCloudQueueClient());
            var videoRepository = new FakeVideoRepository(_blobSource, _blobDestination);
            var screenshotRepository = new FakeScreenshotRepository(_blobDestination);
            var mediaInfoReader = new MediaInfoReader();
            var encoder = new Encoder();
            var fileSystemWrapper = new FileSystemWrapper();

            var queueProcess = new QueueProcess(1000, queueVideoRepository);
            var downloadProcess = new DownloadProcess(5, videoRepository, fileSystemWrapper);
            var encoderProcess = new EncodeProcess(5, encoder, videoRepository, mediaInfoReader, queueVideoRepository, fileSystemWrapper);
            var uploadProcess = new UploadProcess(5, videoRepository, screenshotRepository, fileSystemWrapper);
            var finishProcess = new FinishProcess(queueVideoRepository, videoRepository, fileSystemWrapper);

            var queueContainer = new ProcessContainer<object, QueueInformation, DownloadInformation>(queueProcess, downloadProcess);
            var downloadContainer = new ProcessContainer<QueueInformation, DownloadInformation, EncodeInformation>(downloadProcess, encoderProcess);
            var encoderContainer = new ProcessContainer<DownloadInformation, EncodeInformation, UploadInformation>(encoderProcess, uploadProcess);
            var uploadContainer = new ProcessContainer<EncodeInformation, UploadInformation, object>(uploadProcess, finishProcess);

            var processManager = new EncodeProcessManager(queueVideoRepository.DeleteMessageLocal);
            processManager.Add(queueContainer);
            processManager.Add(downloadContainer);
            processManager.Add(encoderContainer);
            processManager.Add(uploadContainer);

            var timer = new Timer(UpdateMessages, queueVideoRepository, (int) queueVideoRepository.InvisibleTime.TotalMilliseconds/2, (int) queueVideoRepository.InvisibleTime.TotalMilliseconds/2);

            //Act & Assert
            Task task = processManager.Start();
            StartQueueWork();

            Thread.Sleep(30000);


            while (queueVideoRepository.ApproximateMessageCount > 0)
            {
                Thread.Sleep(60000);
            }

            //Thread.Sleep(50*60*1000);

            processManager.Stop();

            task.Wait();
        }
Exemplo n.º 4
0
        public void DownloadExistVideoTest()
        {
            //Arrange
            var downloadProcess = new DownloadProcess(5, _videoiRepository.Object, _fileSystemWrapper.Object);

            _videoiRepository.Setup(m => m.ExistsEncodedVideo(It.IsAny<string>())).Returns(true);

            var queueInformation = new QueueInformation()
            {
                VideoMessage = new VideoMessage()
            };
            
            //Act
            downloadProcess.ProcessMethod(queueInformation, new CancellationToken());

            //Assert
            _videoiRepository.Verify(m => m.SetEncodingState(It.IsAny<string>(), EncodingState.InProcess, EncodingStage.Downloading, null), Times.Once());
            _videoiRepository.Verify(m => m.ExistsEncodedVideo(It.IsAny<string>()), Times.Once());
            _videoiRepository.Verify(m => m.DownloadOriginalVideo(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
        }
Exemplo n.º 5
0
        public void DownloadExceptionHandlerTest()
        {
            //Arrange
            var downloadProcess = new DownloadProcess(5, _videoiRepository.Object, _fileSystemWrapper.Object);

            var queueInformation = new QueueInformation()
                                       {
                                           VideoMessage = new VideoMessage()
                                                              {
                                                                  Delete = true
                                                              }
                                       };

            _fileSystemWrapper.Setup(m => m.DirectoryExists(It.IsAny<string>())).Returns(true);

            //Act
            downloadProcess.ExceptionHandler(new Exception(), queueInformation);
            
            //Asert
            _videoiRepository.Verify(m => m.SetEncodingState(It.IsAny<string>(), EncodingState.Failed, EncodingStage.Downloading, null), Times.Once());

            _fileSystemWrapper.Verify(m => m.DirectoryExists(It.IsAny<string>()), Times.Once());
            _fileSystemWrapper.Verify(m => m.DirectoryDelete(It.IsAny<string>()), Times.Once());
        }
Exemplo n.º 6
0
        public void BrunchOfDeleteTest()
        {
            //Arragnge
            var downloadProcess = new DownloadProcess(5, _videoiRepository.Object, _fileSystemWrapper.Object);

            var queueInformation = new QueueInformation()
                                       {
                                           VideoMessage = new VideoMessage()
                                                              {
                                                                  Delete = true
                                                              }
                                       };
            
            //Act
            var downloadInfo = downloadProcess.ProcessMethod(queueInformation, new CancellationToken());

            //Assert
            _videoiRepository.Verify(m => m.SetEncodingState(It.IsAny<string>(), EncodingState.InProcess, EncodingStage.Downloading, null), Times.Never());
            _videoiRepository.Verify(m => m.DownloadOriginalVideo(It.IsAny<string>(), It.IsAny<string>()), Times.Never());

            Assert.AreEqual(queueInformation.VideoMessage, downloadInfo.QueueInformation.VideoMessage);
        }
        public void ProcessRequest(HttpContext context)
        {
            var request  = context.Request;
            var response = context.Response;

            // Accepting user request
            var idStr = request.QueryString["id"];

            try
            {
                int id;
                if (!int.TryParse(idStr, out id))
                {
                    InvalidRequest(context, "Invalid request!");
                    return;
                }
                UploadedFile uploadedFile;
                using (var db = new UploadDb())
                {
                    db.Configuration.AutoDetectChangesEnabled = false;
                    db.Configuration.ProxyCreationEnabled     = false;

                    var file = db.Files.FirstOrDefault(a => a.UploadedFileID == id);
                    if (file == null)
                    {
                        InvalidRequest(context, "File does not exists!");
                        response.StatusCode = 404;
                        return;
                    }
                    uploadedFile = file;
                }

                //SiteException.LogException(new Exception(
                //	string.Format("UploadedFileID:{0}, IsPublic:{1}, UploadDate:{2}, Filename:{3}",
                //		uploadedFile.UploadedFileID,
                //		uploadedFile.IsPublic,
                //		uploadedFile.UploadDate,
                //		uploadedFile.Filename)));

                if (uploadedFile.IsPublic == false)
                {
                    // check the owner
                    var user = UserManager.GetUser();
                    if (user == null)
                    {
                        var succeed = UserManager.BasicAuthorize(context);
                        if (!succeed)
                        {
                            return;
                        }
                        user = UserManager.GetUser();
                    }

                    // not the file owner!
                    if (user == null || user.UserID != uploadedFile.UserId)
                    {
                        context.Response.Clear();
                        context.Response.Write("You do not have access to download this file!");
                        context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        context.Response.Flush();
                        context.Response.End();
                        return;
                    }
                }

                // file path
                var fileName = UploadedFileManager.MapToPhysicalPath(uploadedFile);

                // reading file info
                var fileInfo   = new FileInfo(fileName);
                var fileLength = fileInfo.Length;

                // Download information class
                using (var downloadInfo = new DownloadDataInfo(fileName))
                {
                    downloadInfo.DisplayFileName = UploadedFileManager.GetUrlFileName(uploadedFile);

                    // Reading request download range
                    var requestedRanges = HeadersParser.ParseHttpRequestHeaderMultipleRange(context.Request, fileLength);

                    // apply the ranges to the download info
                    downloadInfo.InitializeRanges(requestedRanges);

                    string etagMatched;
                    int    outcomeStausCode = 200;

                    // validating the ranges specified
                    if (!HeadersParser.ValidatePartialRequest(context.Request, downloadInfo, out etagMatched, ref outcomeStausCode))
                    {
                        // the request is invalid, this is the invalid code
                        context.Response.StatusCode = outcomeStausCode;

                        // show to the client what is the real ETag
                        if (!string.IsNullOrEmpty(etagMatched))
                        {
                            context.Response.AppendHeader("ETag", etagMatched);
                        }

                        // stop the preoccess
                        // but don't hassle with error messages
                        return;
                    }

                    // user ID, or IP or anything you use to identify the user
                    //var userIP = context.Request.UserHostAddress;

                    // Option 1: limiting the download speed for this file for this user!
                    //UserSpeedLimitManager.StartNewDownload(downloadInfo, userIP, DownloadLimit);

                    // Option 2: Limiting only this connection
                    downloadInfo.LimitTransferSpeed(DownloadLimit);

                    // It is very important to destory the DownloadProcess object
                    // Here the using block does it for us.
                    using (var process = new DownloadProcess(downloadInfo))
                    {
                        var state = DownloadProcess.DownloadProcessState.None;
                        try
                        {
                            // start the download
                            state = process.ProcessDownload(context.Response);
                        }
                        catch (HttpException)
                        {
                            // preventing:
                            // System.Web.HttpException (0x800703E3): The remote host closed the connection. The error code is 0x800703E3.
                        }

                        // checking the state of the download
                        if (state == DownloadProcess.DownloadProcessState.LastPartfinished)
                        {
                            // all parts of download are finish, do something here!
                            using (var db = new UploadDb())
                            {
                                var dbFile = db.Files.FirstOrDefault(a => a.UploadedFileID == uploadedFile.UploadedFileID);
                                if (dbFile != null)
                                {
                                    dbFile.Downloaded++;
                                    dbFile.LastDownload = DateTime.Now.ToUniversalTime();
                                    db.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SiteException.LogException(ex, "ID: " + idStr);
                throw;
            }
        }