Ejemplo n.º 1
0
        public async Task <DownloadedData> GetAsync(string url, CancellationToken token, Action <DownloadInformation> onDownloadStarted, TimeSpan?duration = null, string key = null, CacheType?cacheType = null)
        {
            string filename         = (string.IsNullOrWhiteSpace(key) ? _md5Helper.MD5(url) : _md5Helper.MD5(key))?.ToSanitizedKey();
            var    allowDiskCaching = AllowDiskCaching(cacheType);
            string filepath         = allowDiskCaching == false ? null : await _diskCache.GetFilePathAsync(filename).ConfigureAwait(false);

            if (allowDiskCaching)
            {
                byte[] data = await _diskCache.TryGetAsync(filename, token).ConfigureAwait(false);

                if (data != null)
                {
                    return new DownloadedData(filepath, data)
                           {
                               RetrievedFromDiskCache = true
                           }
                }
                ;
            }

            var downloadInformation = new DownloadInformation(url, key, filename, allowDiskCaching, duration);

            onDownloadStarted?.Invoke(downloadInformation);

            var bytes = await DownloadBytesAndCacheAsync(url, filename, token, duration, cacheType).ConfigureAwait(false);

            return(new DownloadedData(filepath, bytes));
        }
Ejemplo n.º 2
0
        public void EncodeNotExistVideoTest()
        {
            //Arragnge
            var videoMssg = new VideoMessage();

            var encodeProcess = new EncodeProcess(5, _encoder.Object, _videoRepository.Object, _mediaInfoReader.Object, _queueVideoRepository.Object, _fileSystem.Object);

            _videoRepository.Setup(m => m.ExistsEncodedVideo(It.IsAny <string>())).Returns(false);

            var downloadInfo = new DownloadInformation()
            {
                LocalPath        = "local path",
                LocalFilePath    = "local file path",
                QueueInformation = new QueueInformation()
                {
                    VideoMessage = videoMssg
                }
            };

            //Act
            encodeProcess.ProcessMethod(downloadInfo, new CancellationToken());

            //Assert
            _videoRepository.Verify(m => m.SetEncodingState(It.IsAny <string>(), EncodingState.InProcess, EncodingStage.Encoding, null), Times.Once());
            _videoRepository.Verify(m => m.ExistsEncodedVideo(It.IsAny <string>()), Times.Once());
            _mediaInfoReader.Verify(m => m.GetInformation(It.IsAny <string>()), Times.Once());
            _videoRepository.Verify(m => m.FillMediaInfoTables(It.IsAny <Dictionary <Enum, object> >(), It.IsAny <string>()), Times.Once());
            _videoRepository.Verify(m => m.GetVideoMediaInfo(It.IsAny <Dictionary <Enum, object> >()), Times.Once());
            _encoder.Verify(m => m.EncodeVideo(It.IsAny <VideoMediaInfo>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once());
            _encoder.Verify(m => m.EncodeScreenshot(It.IsAny <VideoMediaInfo>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once());
        }
Ejemplo n.º 3
0
        public void EncodeSuccessfulTest()
        {
            //Arragnge
            var videoMssg = new VideoMessage();
            var videoList = new List<VideoData>();
            var screenshotList = new List<ScreenshotData>();

            var encodeProcess = new EncodeProcess(5, _encoder.Object, _videoRepository.Object, _mediaInfoReader.Object, _queueVideoRepository.Object, _fileSystem.Object);

            _encoder.Setup(m => m.EncodeVideo(It.IsAny<VideoMediaInfo>(), It.IsAny<string>(), It.IsAny<string>())).Returns(videoList);
            _encoder.Setup(m => m.EncodeScreenshot(It.IsAny<VideoMediaInfo>(), It.IsAny<string>(), It.IsAny<string>())).Returns(screenshotList);

            var downloadInfo = new DownloadInformation()
                                   {
                                       LocalPath = "local path",
                                       LocalFilePath = "local file path",
                                       QueueInformation = new QueueInformation()
                                                              {
                                                                  VideoMessage = videoMssg
                                                              }
                                   };

            //Act
            var encodeInfo = encodeProcess.ProcessMethod(downloadInfo, new CancellationToken());

            //Assert
            Assert.AreEqual(downloadInfo.QueueInformation.VideoMessage, videoMssg);
            Assert.AreEqual(downloadInfo.LocalFilePath, encodeInfo.DownloadInformation.LocalFilePath);
            Assert.AreEqual(downloadInfo.LocalPath, encodeInfo.DownloadInformation.LocalPath);
            Assert.AreEqual(videoList, encodeInfo.EncodeVideoList);
            Assert.AreEqual(screenshotList, encodeInfo.EncodeScreenshotList);
        }
Ejemplo n.º 4
0
        public void EncodeSuccessfulTest()
        {
            //Arragnge
            var videoMssg      = new VideoMessage();
            var videoList      = new List <VideoData>();
            var screenshotList = new List <ScreenshotData>();

            var encodeProcess = new EncodeProcess(5, _encoder.Object, _videoRepository.Object, _mediaInfoReader.Object, _queueVideoRepository.Object, _fileSystem.Object);

            _encoder.Setup(m => m.EncodeVideo(It.IsAny <VideoMediaInfo>(), It.IsAny <string>(), It.IsAny <string>())).Returns(videoList);
            _encoder.Setup(m => m.EncodeScreenshot(It.IsAny <VideoMediaInfo>(), It.IsAny <string>(), It.IsAny <string>())).Returns(screenshotList);

            var downloadInfo = new DownloadInformation()
            {
                LocalPath        = "local path",
                LocalFilePath    = "local file path",
                QueueInformation = new QueueInformation()
                {
                    VideoMessage = videoMssg
                }
            };

            //Act
            var encodeInfo = encodeProcess.ProcessMethod(downloadInfo, new CancellationToken());

            //Assert
            Assert.AreEqual(downloadInfo.QueueInformation.VideoMessage, videoMssg);
            Assert.AreEqual(downloadInfo.LocalFilePath, encodeInfo.DownloadInformation.LocalFilePath);
            Assert.AreEqual(downloadInfo.LocalPath, encodeInfo.DownloadInformation.LocalPath);
            Assert.AreEqual(videoList, encodeInfo.EncodeVideoList);
            Assert.AreEqual(screenshotList, encodeInfo.EncodeScreenshotList);
        }
Ejemplo n.º 5
0
        public void BrunchOfDeleteTest()
        {
            //Arragnge
            var videoMsg = new VideoMessage()
            {
                Delete = true
            };

            var encodeProcess = new EncodeProcess(5, _encoder.Object, _videoRepository.Object, _mediaInfoReader.Object, _queueVideoRepository.Object, _fileSystem.Object);
            var downloadInfo  = new DownloadInformation()
            {
                QueueInformation = new QueueInformation()
                {
                    VideoMessage = videoMsg
                }
            };
            //Act
            var encodeInfo = encodeProcess.ProcessMethod(downloadInfo, new CancellationToken());

            //Assert
            Assert.AreEqual(downloadInfo, encodeInfo.DownloadInformation);

            _videoRepository.Verify(m => m.SetEncodingState(It.IsAny <string>(), EncodingState.InProcess, EncodingStage.Encoding, null), Times.Never());
            _mediaInfoReader.Verify(m => m.GetInformation(It.IsAny <string>()), Times.Never());
            _videoRepository.Verify(m => m.FillMediaInfoTables(It.IsAny <Dictionary <Enum, object> >(), It.IsAny <string>()), Times.Never());
            _videoRepository.Verify(m => m.GetVideoMediaInfo(It.IsAny <Dictionary <Enum, object> >()), Times.Never());
            _encoder.Verify(m => m.EncodeVideo(It.IsAny <VideoMediaInfo>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never());
            _encoder.Verify(m => m.EncodeScreenshot(It.IsAny <VideoMediaInfo>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never());
        }
Ejemplo n.º 6
0
            public void GetDownloadDetail(long productID, Action <DownloadInformation> doneCallbackAction)
            {
                var downloadInfo = new DownloadInformation
                {
                    isValid = false
                };

                AssetStoreOAuth.instance.FetchUserInfo(userInfo =>
                {
                    if (!userInfo.isValid)
                    {
                        downloadInfo.errorMessage = userInfo.errorMessage;
                        doneCallbackAction?.Invoke(downloadInfo);
                        return;
                    }

                    var httpRequest = m_AsyncHTTPClient.GetASyncHTTPClient($"{m_Host}{kDownloadUri}/{productID}");
                    httpRequest.header["Content-Type"]  = "application/json";
                    httpRequest.header["Authorization"] = "Bearer " + userInfo.accessToken.access_token;
                    httpRequest.doneCallback            = httpClient =>
                    {
                        var errorMessage = "Failed to parse JSON.";
                        if (httpClient.IsSuccess() && httpClient.responseCode == 200)
                        {
                            try
                            {
                                var res = Json.Deserialize(httpClient.text) as Dictionary <string, object>;
                                if (res != null)
                                {
                                    var downloadRes            = res["result"] as Dictionary <string, object>;
                                    var download               = downloadRes["download"] as Dictionary <string, object>;
                                    downloadInfo.isValid       = true;
                                    downloadInfo.CategoryName  = download["filename_safe_category_name"] as string;
                                    downloadInfo.PackageName   = download["filename_safe_package_name"] as string;
                                    downloadInfo.PublisherName = download["filename_safe_publisher_name"] as string;
                                    downloadInfo.PackageId     = download["id"] as string;
                                    downloadInfo.Key           = download["key"] as string;
                                    downloadInfo.Url           = download["url"] as string;
                                    doneCallbackAction?.Invoke(downloadInfo);
                                    return;
                                }
                            }
                            catch (Exception e)
                            {
                                errorMessage = e.Message;
                            }
                        }
                        else
                        {
                            errorMessage = httpClient.text;
                        }

                        downloadInfo.errorMessage = errorMessage;
                        doneCallbackAction?.Invoke(downloadInfo);
                    };
                    httpRequest.Begin();
                });
            }
Ejemplo n.º 7
0
        public virtual async Task <CacheStream> DownloadAndCacheIfNeededAsync(string url, TaskParameter parameters, Configuration configuration, CancellationToken token)
        {
            var allowCustomKey = !string.IsNullOrWhiteSpace(parameters.CustomCacheKey) &&
                                 (string.IsNullOrWhiteSpace(parameters.LoadingPlaceholderPath) || parameters.LoadingPlaceholderPath != url) &&
                                 (string.IsNullOrWhiteSpace(parameters.ErrorPlaceholderPath) || parameters.ErrorPlaceholderPath != url);

            string filename         = (allowCustomKey ? MD5Helper.MD5(parameters.CustomCacheKey) : MD5Helper.MD5(url))?.ToSanitizedKey();
            var    allowDiskCaching = AllowDiskCaching(parameters.CacheType);
            var    duration         = parameters.CacheDuration.HasValue ? parameters.CacheDuration.Value : configuration.DiskCacheDuration;
            string filePath         = null;

            if (allowDiskCaching)
            {
                var diskStream = await configuration.DiskCache.TryGetStreamAsync(filename).ConfigureAwait(false);

                if (diskStream != null)
                {
                    token.ThrowIfCancellationRequested();
                    filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);

                    return(new CacheStream(diskStream, true, filePath));
                }
            }

            token.ThrowIfCancellationRequested();

            var downloadInfo = new DownloadInformation(url, parameters.CustomCacheKey, filename, allowDiskCaching, duration);

            parameters.OnDownloadStarted?.Invoke(downloadInfo);

            var responseBytes = await Retry.DoAsync(
                async() => await DownloadAsync(url, token, configuration.HttpClient).ConfigureAwait(false),
                DelayBetweenRetry,
                parameters.RetryCount,
                () => configuration.Logger.Debug(string.Format("Retry download: {0}", url)));

            if (responseBytes == null)
            {
                throw new HttpRequestException("No Content");
            }

            if (allowDiskCaching)
            {
                await configuration.DiskCache.AddToSavingQueueIfNotExistsAsync(filename, responseBytes, duration).ConfigureAwait(false);
            }

            token.ThrowIfCancellationRequested();
            filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);

            token.ThrowIfCancellationRequested();
            var memoryStream = new MemoryStream(responseBytes, false);

            return(new CacheStream(memoryStream, false, filePath));
        }
Ejemplo n.º 8
0
 public void DownloadHandler(DownloadInformation info)
 {
     if (info.Status == DownloadStatus.Success)
     {
         Comics  = DownloadService.Comics;
         Loading = false;
     }
     else if (info.Status == DownloadStatus.Failed)
     {
         DialogService.Alert("Downloading failed", "", "Ok");
     }
     else
     {
         DialogService.Alert("This page doesn't exist", "", "Ok");
     }
 }
Ejemplo n.º 9
0
        public virtual async Task<CacheStream> DownloadAndCacheIfNeededAsync(string url, TaskParameter parameters, Configuration configuration, CancellationToken token)
        {
            var allowCustomKey = !string.IsNullOrWhiteSpace(parameters.CustomCacheKey)
                                       && (string.IsNullOrWhiteSpace(parameters.LoadingPlaceholderPath) || parameters.LoadingPlaceholderPath != url)
                                       && (string.IsNullOrWhiteSpace(parameters.ErrorPlaceholderPath) || parameters.ErrorPlaceholderPath != url);

            string filename = (allowCustomKey ? MD5Helper.MD5(parameters.CustomCacheKey) : MD5Helper.MD5(url))?.ToSanitizedKey();
            var allowDiskCaching = AllowDiskCaching(parameters.CacheType);
            var duration = parameters.CacheDuration.HasValue ? parameters.CacheDuration.Value : configuration.DiskCacheDuration;
            string filePath = null;

            if (allowDiskCaching)
            {
                var diskStream = await configuration.DiskCache.TryGetStreamAsync(filename).ConfigureAwait(false);
                if (diskStream != null)
                {
                    token.ThrowIfCancellationRequested();
                    filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);
                    return new CacheStream(diskStream, true, filePath);
                }
            }

            token.ThrowIfCancellationRequested();

            var downloadInfo = new DownloadInformation(url, parameters.CustomCacheKey, filename, allowDiskCaching, duration);
            parameters.OnDownloadStarted?.Invoke(downloadInfo);

            var responseBytes = await Retry.DoAsync(
                async () => await DownloadAsync(url, token, configuration.HttpClient).ConfigureAwait(false),
                DelayBetweenRetry,
                parameters.RetryCount,
                () => configuration.Logger.Debug(string.Format("Retry download: {0}", url)));

            if (responseBytes == null)
                throw new HttpRequestException("No Content");

            if (allowDiskCaching)
            {
                await configuration.DiskCache.AddToSavingQueueIfNotExistsAsync(filename, responseBytes, duration).ConfigureAwait(false);
            }

            token.ThrowIfCancellationRequested();
            filePath = await configuration.DiskCache.GetFilePathAsync(filename).ConfigureAwait(false);
            token.ThrowIfCancellationRequested();
            var memoryStream = new MemoryStream(responseBytes, false);
            return new CacheStream(memoryStream, false, filePath);
        }
Ejemplo n.º 10
0
        public virtual async Task <Stream> DownloadAndCacheIfNeededAsync(string url, ILoadableImageSource imageSource, CancellationToken token)
        {
            string     filename         = MD5Helper.MD5(url);
            bool       allowDiskCaching = AllowDiskCaching(_imageLoader.CacheType);
            IDiskCache?diskCache        = _imageLoader.DiskCache;
            TimeSpan   duration         = _imageLoader.DiskCacheDuration;

            if (allowDiskCaching)
            {
                Stream?diskStream = await diskCache !.TryGetStreamAsync(filename).ConfigureAwait(false);
                token.ThrowIfCancellationRequested();

                if (diskStream != null)
                {
                    return(diskStream);
                }
            }

            var downloadInfo = new DownloadInformation(duration);

#if LATER
            parameters.OnDownloadStarted?.Invoke(downloadInfo);
#endif

            byte[] responseBytes = await Retry.DoAsync(
                async() => await DownloadAsync(url, token, _imageLoader.HttpClient, imageSource, downloadInfo).ConfigureAwait(false),
                TimeSpan.FromMilliseconds(_imageLoader.RetryDelayInMs),
                _imageLoader.RetryCount,
                () => _imageLoader.Logger?.Debug($"Retry download: {url}")).ConfigureAwait(false);

            if (responseBytes == null)
            {
                throw new HttpRequestException("No Content");
            }

            if (allowDiskCaching)
            {
                await diskCache !.AddToSavingQueueIfNotExistsAsync(filename, responseBytes, downloadInfo.CacheValidity).ConfigureAwait(false);
            }

            token.ThrowIfCancellationRequested();

            return(new MemoryStream(responseBytes, false));
        }
Ejemplo n.º 11
0
        private void StartLoad()
        {
            long size = 0;

            try
            {
                HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create(this.task.DownloadUrl);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                size     = response.ContentLength;
                b_Thread = Get_bool(this.task.DownloadUrl);
            }
            catch (WebException e)
            {
                throw new WebException("错误,请添入正确下载地址");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            this.FileSize = size;
            int sizeOfEachThread    = (int)this.FileSize / this.task.NumberOfThread;
            int sizeOfTheLastThread = (int)this.FileSize % this.task.NumberOfThread;
            DownloadInformation i   = new DownloadInformation(0, 0);

            for (int j = 0; j < this.task.NumberOfThread; j++)
            {
                i.Start = j != 0 ? i.End + 1 : i.End;
                i.End   = j == this.task.NumberOfThread - 1 ? i.End + sizeOfEachThread + sizeOfTheLastThread : i.End + sizeOfEachThread;
                Thread th = new Thread(GetData);
                th.Name         = j.ToString();
                th.IsBackground = true;
                th.Start(i);
                note.Add(new DownloadInformation(i.Start, i.End, task.DownloadUrl, task.FileName, this.fileSize,
                                                 new DownloadInformation(i.Start, i.End)));
                i = new DownloadInformation(i.Start, i.End);
                G_thread_Collection.Add(th);
                lbo.Add(false);
            }
            CreateFile();
        }
Ejemplo n.º 12
0
        public async Task <CacheStream> GetStreamAsync(string url, CancellationToken token, Action <DownloadInformation> onDownloadStarted, TimeSpan?duration = null, string key = null, CacheType?cacheType = null)
        {
            string filename         = (string.IsNullOrWhiteSpace(key) ? _md5Helper.MD5(url) : _md5Helper.MD5(key))?.ToSanitizedKey();
            var    allowDiskCaching = AllowDiskCaching(cacheType);

            if (allowDiskCaching)
            {
                var diskStream = await _diskCache.TryGetStreamAsync(filename).ConfigureAwait(false);

                if (diskStream != null)
                {
                    return(new CacheStream(diskStream, true));
                }
            }

            var downloadInformation = new DownloadInformation(url, key, filename, allowDiskCaching, duration);

            onDownloadStarted?.Invoke(downloadInformation);

            var memoryStream = await DownloadStreamAndCacheAsync(url, filename, token, duration, cacheType).ConfigureAwait(false);

            return(new CacheStream(memoryStream, false));
        }
Ejemplo n.º 13
0
        public void EncodeExceptionHandlerTest()
        {
            //Arrange
            const string localPath     = "local path";
            var          videoMsg      = new VideoMessage();
            var          encodeProcess = new EncodeProcess(5, _encoder.Object, _videoRepository.Object, _mediaInfoReader.Object, _queueVideoRepository.Object, _fileSystem.Object);

            var downloadInfo = new DownloadInformation()
            {
                LocalPath        = localPath,
                QueueInformation = new QueueInformation()
                {
                    VideoMessage = videoMsg
                }
            };

            //Act
            encodeProcess.ExceptionHandler(new Exception(), downloadInfo);

            //Asert
            _videoRepository.Verify(m => m.SetEncodingState(It.IsAny <string>(), EncodingState.Failed, EncodingStage.Encoding, null), Times.Once());
            _fileSystem.Verify(m => m.DirectoryDelete(localPath), Times.Once());
            _queueVideoRepository.Verify(m => m.DeleteMessage(videoMsg), Times.Once());
        }
Ejemplo n.º 14
0
 public DownloadStartedEventArgs(DownloadInformation downloadInformation) : base(downloadInformation)
 {
 }
Ejemplo n.º 15
0
        protected virtual async Task <byte[]> DownloadAsync(string url, CancellationToken token, HttpClient client, TaskParameter parameters, DownloadInformation downloadInformation)
        {
            if (!parameters.Preload)
            {
                await Task.Delay(25).ConfigureAwait(false);

                token.ThrowIfCancellationRequested();
            }

            var progressAction = parameters.OnDownloadProgress;

            using (var httpHeadersTimeoutTokenSource = new CancellationTokenSource())
                using (var headersTimeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, httpHeadersTimeoutTokenSource.Token))
                {
                    httpHeadersTimeoutTokenSource.CancelAfter(TimeSpan.FromSeconds(Configuration.HttpHeadersTimeout));

                    try
                    {
                        var headerTimeoutToken = headersTimeoutTokenSource.Token;

                        using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, headerTimeoutToken).ConfigureAwait(false))
                        {
                            headerTimeoutToken.ThrowIfCancellationRequested();

                            if (!response.IsSuccessStatusCode)
                            {
                                if (response.Content == null)
                                {
                                    throw new DownloadHttpStatusCodeException(response.StatusCode);
                                }

                                using (response.Content)
                                {
                                    var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                                    throw new DownloadHttpStatusCodeException(response.StatusCode, content);
                                }
                            }

                            if (response.Content == null)
                            {
                                throw new DownloadException("No content");
                            }

                            var mediaType = response.Content.Headers?.ContentType?.MediaType;
                            if (!string.IsNullOrWhiteSpace(mediaType) && !mediaType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
                            {
                                if (InvalidContentTypes.Any(v => mediaType.StartsWith(v, StringComparison.OrdinalIgnoreCase)))
                                {
                                    throw new DownloadException($"Invalid response content type ({mediaType})");
                                }
                            }

                            if (!parameters.CacheDuration.HasValue && Configuration.TryToReadDiskCacheDurationFromHttpHeaders &&
                                response.Headers?.CacheControl?.MaxAge != null && response.Headers.CacheControl.MaxAge > TimeSpan.Zero)
                            {
                                downloadInformation.CacheValidity = response.Headers.CacheControl.MaxAge.Value;
                            }

                            ModifyParametersAfterResponse(response, parameters);

                            using (var httpReadTimeoutTokenSource = new CancellationTokenSource())
                                using (var readTimeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, httpReadTimeoutTokenSource.Token))
                                {
                                    var readTimeoutToken     = readTimeoutTokenSource.Token;
                                    var httpReadTimeoutToken = httpReadTimeoutTokenSource.Token;
                                    var total             = (int)(response.Content.Headers.ContentLength ?? -1);
                                    var canReportProgress = progressAction != null;

                                    httpReadTimeoutTokenSource.CancelAfter(TimeSpan.FromSeconds(Configuration.HttpReadTimeout));
                                    readTimeoutToken.ThrowIfCancellationRequested();

                                    try
                                    {
                                        using (var outputStream = new MemoryStream())
                                            using (var sourceStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                                            {
                                                httpReadTimeoutToken.Register(() => sourceStream.TryDispose());

                                                var totalRead = 0;
                                                var buffer    = new byte[Configuration.HttpReadBufferSize];
                                                var read      = 0;

                                                while ((read = await sourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
                                                {
                                                    readTimeoutToken.ThrowIfCancellationRequested();
                                                    outputStream.Write(buffer, 0, read);
                                                    totalRead += read;

                                                    if (canReportProgress)
                                                    {
                                                        progressAction(new DownloadProgress(totalRead, total));
                                                    }
                                                }

                                                if (outputStream.Length == 0)
                                                {
                                                    throw new DownloadException("Zero length stream");
                                                }

                                                if (outputStream.Length < 32)
                                                {
                                                    throw new DownloadException("Invalid stream");
                                                }

                                                return(outputStream.ToArray());
                                            }
                                    }
                                    catch (Exception ex) when(ex is OperationCanceledException || ex is ObjectDisposedException)
                                    {
                                        if (httpReadTimeoutTokenSource.IsCancellationRequested)
                                        {
                                            throw new DownloadReadTimeoutException();
                                        }

                                        throw;
                                    }
                                }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        if (httpHeadersTimeoutTokenSource.IsCancellationRequested)
                        {
                            throw new DownloadHeadersTimeoutException();
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
        }
Ejemplo n.º 16
0
        private void GetData(object l)
        {
            DownloadInformation ll = (DownloadInformation)l;

            if (!b_Thread)
            {
                are.WaitOne();
            }
            else
            {
                are.Set();
            }
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(this.task.DownloadUrl);

            req.Timeout = 15000;
            req.AddRange(ll.Start, ll.End);
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream          ss  = res.GetResponseStream();

            byte[]     buffer = new byte[10240];
            FileStream fs     = new FileStream(
                string.Format(task.FilePath + @"\" + task.FileName + Thread.CurrentThread.Name),
                FileMode.Create);

            try
            {
                int i;
                int nns = Convert.ToInt32(Thread.CurrentThread.Name);
                while ((i = ss.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fs.Write(buffer, 0, i);
                    note[nns].Start += i;
                    while (stop)
                    {
                        Thread.Sleep(100);
                    }
                    if (stop2)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                    Update();
                }
                fs.Close();
                ss.Close();
                lbo[Convert.ToInt32(Thread.CurrentThread.Name)] = true;
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
                SaveState();
            }
            finally
            {
                fs.Close();
                ss.Close();
                if (!b_Thread)
                {
                    are.Set();
                }
                else
                {
                    are.Set();
                }
            }
        }
 public DownloadStartedEventArgs(DownloadInformation downloadInformation)
 {
     DownloadInformation = downloadInformation;
 }
Ejemplo n.º 18
0
        protected virtual async Task <byte[]> DownloadAsync(string url, CancellationToken token, HttpClient client, ILoadableImageSource imageSource, DownloadInformation downloadInfo)
        {
            await Task.Delay(25, token).ConfigureAwait(false);

            token.ThrowIfCancellationRequested();

            using (var httpHeadersTimeoutTokenSource = new CancellationTokenSource())
                using (var headersTimeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, httpHeadersTimeoutTokenSource.Token))
                {
                    httpHeadersTimeoutTokenSource.CancelAfter(TimeSpan.FromSeconds(_imageLoader.HttpHeadersTimeout));

                    try
                    {
                        CancellationToken headerTimeoutToken = headersTimeoutTokenSource.Token;

                        using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, headerTimeoutToken).ConfigureAwait(false))
                        {
                            headerTimeoutToken.ThrowIfCancellationRequested();

                            if (!response.IsSuccessStatusCode)
                            {
                                if (response.Content == null)
                                {
                                    throw new DownloadHttpStatusCodeException(response.StatusCode);
                                }

                                using (response.Content)
                                {
                                    string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                                    throw new DownloadHttpStatusCodeException(response.StatusCode, content);
                                }
                            }

                            if (response.Content == null)
                            {
                                throw new DownloadException("No content");
                            }

                            string?mediaType = response.Content.Headers?.ContentType?.MediaType;
                            if (!string.IsNullOrWhiteSpace(mediaType) && !mediaType !.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
                            {
                                if (InvalidContentTypes.Any(v => mediaType.StartsWith(v, StringComparison.OrdinalIgnoreCase)))
                                {
                                    throw new DownloadException($"Invalid response content type ({mediaType})");
                                }
                            }

                            if (_imageLoader.TryToReadDiskCacheDurationFromHttpHeaders &&
                                response.Headers?.CacheControl?.MaxAge != null && response.Headers.CacheControl.MaxAge > TimeSpan.Zero)
                            {
                                downloadInfo.CacheValidity = response.Headers.CacheControl.MaxAge.Value;
                            }

                            using (var httpReadTimeoutTokenSource = new CancellationTokenSource())
                                using (var readTimeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, httpReadTimeoutTokenSource.Token))
                                {
                                    CancellationToken readTimeoutToken     = readTimeoutTokenSource.Token;
                                    CancellationToken httpReadTimeoutToken = httpReadTimeoutTokenSource.Token;
                                    int total = (int)(response.Content.Headers?.ContentLength ?? -1);

                                    httpReadTimeoutTokenSource.CancelAfter(_imageLoader.HttpClient.Timeout);
                                    readTimeoutToken.ThrowIfCancellationRequested();

                                    try
                                    {
                                        using (var outputStream = new MemoryStream())
                                            using (var sourceStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                                            {
                                                httpReadTimeoutToken.Register(() => sourceStream.TryDispose());

                                                int    totalRead       = 0;
                                                byte[] buffer          = new byte[_imageLoader.HttpReadBufferSize];
                                                int    read            = 0;
                                                int    currentProgress = 1;

#if LATER
                                                imageSource.RaiseDownloadProgress(new DownloadProgressEventArgs(currentProgress));
#endif

                                                while ((read = await sourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
                                                {
                                                    readTimeoutToken.ThrowIfCancellationRequested();
                                                    outputStream.Write(buffer, 0, read);
                                                    totalRead += read;

                                                    if (total > 0)
                                                    {
                                                        currentProgress = totalRead * 100 / total;
                                                    }
                                                    else
                                                    {
                                                        currentProgress = ++currentProgress;
                                                    }

                                                    if (currentProgress < 1)
                                                    {
                                                        currentProgress = 1;
                                                    }
                                                    if (currentProgress > 99)
                                                    {
                                                        currentProgress = 99;
                                                    }

#if LATER
                                                    imageSource.RaiseDownloadProgress(new DownloadProgressEventArgs(currentProgress));
#endif
                                                }

                                                if (outputStream.Length == 0)
                                                {
                                                    throw new DownloadException("Zero length stream");
                                                }

                                                if (outputStream.Length < 32)
                                                {
                                                    throw new DownloadException("Invalid stream");
                                                }

                                                currentProgress = 100;
#if LATER
                                                imageSource.RaiseDownloadProgress(new DownloadProgressEventArgs(currentProgress));
#endif

                                                return(outputStream.ToArray());
                                            }
                                    }
                                    catch (Exception ex) when(ex is OperationCanceledException || ex is ObjectDisposedException)
                                    {
                                        if (httpReadTimeoutTokenSource.IsCancellationRequested)
                                        {
                                            throw new DownloadReadTimeoutException();
                                        }

                                        throw;
                                    }
                                }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        if (httpHeadersTimeoutTokenSource.IsCancellationRequested)
                        {
                            throw new DownloadHeadersTimeoutException();
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
        }
			public DownloadStartedEventArgs(DownloadInformation downloadInformation)
			{
				DownloadInformation = downloadInformation;
			}
Ejemplo n.º 20
0
        public void EncodeNotExistVideoTest()
        {
            //Arragnge
            var videoMssg = new VideoMessage();

            var encodeProcess = new EncodeProcess(5, _encoder.Object, _videoRepository.Object, _mediaInfoReader.Object, _queueVideoRepository.Object, _fileSystem.Object);

            _videoRepository.Setup(m => m.ExistsEncodedVideo(It.IsAny<string>())).Returns(false);

            var downloadInfo = new DownloadInformation()
                                   {
                                       LocalPath = "local path",
                                       LocalFilePath = "local file path",
                                       QueueInformation = new QueueInformation()
                                                              {
                                                                  VideoMessage = videoMssg
                                                              }
                                   };

            //Act
            encodeProcess.ProcessMethod(downloadInfo, new CancellationToken());

            //Assert
            _videoRepository.Verify(m => m.SetEncodingState(It.IsAny<string>(), EncodingState.InProcess, EncodingStage.Encoding, null), Times.Once());
            _videoRepository.Verify(m => m.ExistsEncodedVideo(It.IsAny<string>()), Times.Once());
            _mediaInfoReader.Verify(m => m.GetInformation(It.IsAny<string>()), Times.Once());
            _videoRepository.Verify(m => m.FillMediaInfoTables(It.IsAny<Dictionary<Enum, object>>(), It.IsAny<string>()), Times.Once());
            _videoRepository.Verify(m => m.GetVideoMediaInfo(It.IsAny<Dictionary<Enum, object>>()), Times.Once());
            _encoder.Verify(m => m.EncodeVideo(It.IsAny<VideoMediaInfo>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once());
            _encoder.Verify(m => m.EncodeScreenshot(It.IsAny<VideoMediaInfo>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once());
        }
Ejemplo n.º 21
0
        public void EncodeExceptionHandlerTest()
        {
            //Arrange
            const string localPath = "local path";
            var videoMsg = new VideoMessage();
            var encodeProcess = new EncodeProcess(5, _encoder.Object, _videoRepository.Object, _mediaInfoReader.Object, _queueVideoRepository.Object, _fileSystem.Object);

             var downloadInfo = new DownloadInformation()
                                   {
                                       LocalPath=localPath,
                                       QueueInformation = new QueueInformation()
                                                              {
                                                                  VideoMessage = videoMsg
                                                              }
                                   };

            //Act
            encodeProcess.ExceptionHandler(new Exception(), downloadInfo);

            //Asert
            _videoRepository.Verify(m=>m.SetEncodingState(It.IsAny<string>(),EncodingState.Failed, EncodingStage.Encoding, null), Times.Once());
            _fileSystem.Verify(m=>m.DirectoryDelete(localPath), Times.Once());
            _queueVideoRepository.Verify(m=>m.DeleteMessage(videoMsg), Times.Once());
        }
Ejemplo n.º 22
0
        public void BrunchOfDeleteTest()
        {
            //Arragnge
            var videoMsg = new VideoMessage() {Delete = true};

            var encodeProcess = new EncodeProcess(5, _encoder.Object, _videoRepository.Object, _mediaInfoReader.Object, _queueVideoRepository.Object, _fileSystem.Object);
            var downloadInfo = new DownloadInformation()
                                   {
                                       QueueInformation = new QueueInformation()
                                                              {
                                                                  VideoMessage = videoMsg
                                                              }
                                   };
            //Act
            var encodeInfo = encodeProcess.ProcessMethod(downloadInfo, new CancellationToken());

            //Assert
            Assert.AreEqual(downloadInfo, encodeInfo.DownloadInformation);

            _videoRepository.Verify(m => m.SetEncodingState(It.IsAny<string>(), EncodingState.InProcess, EncodingStage.Encoding, null), Times.Never());
            _mediaInfoReader.Verify(m => m.GetInformation(It.IsAny<string>()), Times.Never());
            _videoRepository.Verify(m => m.FillMediaInfoTables(It.IsAny<Dictionary<Enum, object>>(), It.IsAny<string>()), Times.Never());
            _videoRepository.Verify(m => m.GetVideoMediaInfo(It.IsAny<Dictionary<Enum, object>>()), Times.Never());
            _encoder.Verify(m => m.EncodeVideo(It.IsAny<VideoMediaInfo>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never());
            _encoder.Verify(m => m.EncodeScreenshot(It.IsAny<VideoMediaInfo>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never());

        }