Ejemplo n.º 1
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            var service = GetStorageService();

            if (InputObject != null)
            {
                Bucket     = InputObject.Bucket;
                ObjectName = InputObject.Name;
            }

            string uri        = GetBaseUri(Bucket, ObjectName);
            var    downloader = new MediaDownloader(service);

            // Write object contents to the pipeline if no -OutFile is specified.
            if (string.IsNullOrEmpty(OutFile))
            {
                // Start with a 1MiB buffer. We could get the object's metadata and use its exact
                // file size, but making a web request << just allocating more memory.
                using (var memStream = new MemoryStream(1024 * 1024))
                {
                    var result = downloader.Download(uri, memStream);
                    if (result.Status == DownloadStatus.Failed || result.Exception != null)
                    {
                        throw result.Exception;
                    }

                    // Stream cursor is at the end (data just written).
                    memStream.Position = 0;
                    using (var streamReader = new StreamReader(memStream))
                    {
                        string objectContents = streamReader.ReadToEnd();
                        WriteObject(objectContents);
                    }
                }

                return;
            }

            // Write object contents to disk. Fail if the local file exists, unless -Force is specified.
            string qualifiedPath = GetFullPath(OutFile);
            bool   fileExists    = File.Exists(qualifiedPath);

            if (fileExists && !Force.IsPresent)
            {
                throw new PSArgumentException($"File '{qualifiedPath}' already exists. Use -Force to overwrite.");
            }


            using (var writer = new FileStream(qualifiedPath, FileMode.Create))
            {
                var result = downloader.Download(uri, writer);
                if (result.Status == DownloadStatus.Failed || result.Exception != null)
                {
                    throw result.Exception;
                }
            }
        }
Ejemplo n.º 2
0
        public void Download_Error()
        {
            var downloadUri = "http://www.sample.com?alt=media";
            var chunkSize   = 100;

            // reset the steam
            StreamContent.Position = 0;

            var handler = new MultipleChunksMessageHandler();

            handler.StatusCode  = HttpStatusCode.BadRequest;
            handler.ChunkSize   = chunkSize;
            handler.DownloadUri = new Uri(downloadUri);

            using (var service = new MockClientService(new BaseClientService.Initializer()
            {
                HttpClientFactory = new MockHttpClientFactory(handler)
            }))
            {
                var downloader = new MediaDownloader(service);
                downloader.ChunkSize = chunkSize;
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                downloader.Download(downloadUri, outputStream);

                var lastProgress = progressList.LastOrDefault();
                Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed));
            }
        }
Ejemplo n.º 3
0
        // Downloading a file stream with progress
        public void DownloadToFile(string bucketName)
        {
            StorageService storage = CreateStorageClient();

            var objectToDownload = storage.Objects.Get(bucketName, "testFile.txt").Execute();

            var downloader = new MediaDownloader(storage);

            // Subscribe a function to the event ProgressChanged
            // The return type of the event is IDownloadProgress which has properties Status and BytesDownloaded
            downloader.ProgressChanged += progress =>
            {
                Console.WriteLine($"{progress.Status} {progress.BytesDownloaded} bytes");
            };



            using (var fileStream = new FileStream(@"C:\Users\Varderes\Desktop\downloadedFile.txt", FileMode.Create))
            {
                var progress = downloader.Download(objectToDownload.MediaLink, fileStream);

                if (progress.Status == DownloadStatus.Completed)
                {
                    Console.WriteLine("Downloaded testFile.txt to downloadedFile.txt");
                }
            }
        }
Ejemplo n.º 4
0
        public void Download_Error_PlaintextResponse(string responseText)
        {
            var downloadUri = "http://www.sample.com";
            var chunkSize   = 100;

            var handler = new MultipleChunksMessageHandler {
                ErrorResponse = responseText
            };

            handler.StatusCode = HttpStatusCode.NotFound;
            handler.ChunkSize  = chunkSize;
            // The media downloader adds the parameter...
            handler.DownloadUri = new Uri(downloadUri + "?alt=media");

            using (var service = CreateMockClientService(handler))
            {
                var downloader = new MediaDownloader(service);
                downloader.ChunkSize = chunkSize;
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                downloader.Download(downloadUri, outputStream);

                var lastProgress = progressList.LastOrDefault();
                Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed));
                GoogleApiException exception = (GoogleApiException)lastProgress.Exception;
                Assert.That(exception.HttpStatusCode, Is.EqualTo(handler.StatusCode));
                Assert.That(exception.Message, Is.EqualTo(responseText));
                Assert.IsNull(exception.Error);
            }
        }
Ejemplo n.º 5
0
        private void DownloadObjectImpl(
            string baseUri,
            Stream destination,
            DownloadObjectOptions options,
            IProgress <IDownloadProgress> progress)
        {
            // URI will definitely not be null; that's constructed internally.
            Preconditions.CheckNotNull(destination, nameof(destination));
            var downloader = new MediaDownloader(Service);

            options?.ModifyDownloader(downloader);
            string uri = options == null ? baseUri : options.GetUri(baseUri);

            if (progress != null)
            {
                downloader.ProgressChanged += progress.Report;
                progress.Report(InitialDownloadProgress.Instance);
            }
            var result = downloader.Download(uri, destination);

            if (result.Status == DownloadStatus.Failed)
            {
                throw result.Exception;
            }
        }
Ejemplo n.º 6
0
        public void Download_Error_JsonResponse()
        {
            using (var service = new MockClientService())
            {
                var downloader = new MediaDownloader(service);
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                downloader.Download(_httpPrefix + "BadRequestJson", outputStream);

                var lastProgress = progressList.LastOrDefault();
                Assert.Equal(DownloadStatus.Failed, lastProgress.Status);
                GoogleApiException exception = (GoogleApiException)lastProgress.Exception;
                Assert.Equal(HttpStatusCode.BadRequest, exception.HttpStatusCode);
                // Just a smattering of checks - if these two pass, it's surely okay.
                Assert.Equal(BadRequestError.Code, exception.Error.Code);
                Assert.Equal(BadRequestError.Errors[0].Message, exception.Error.Errors[0].Message);
                Assert.NotNull(exception.Error.ErrorResponseContent);
                Assert.Contains(BadRequestError.Errors[0].Message, exception.Error.ErrorResponseContent);
            }
        }
        public void Download_Error_PlaintextResponse()
        {
            using (var service = new MockClientService())
            {
                var downloader = new MediaDownloader(service);
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                downloader.Download(_httpPrefix + "NotFoundPlainText", outputStream);

                var lastProgress = progressList.LastOrDefault();
                Assert.Equal(DownloadStatus.Failed, lastProgress.Status);
                GoogleApiException exception = Assert.IsType <GoogleApiException>(lastProgress.Exception);
                Assert.Equal(HttpStatusCode.NotFound, exception.HttpStatusCode);
                Assert.Equal("The service TestService has thrown an exception. HttpStatusCode is NotFound. No error message was specified.", exception.Message);
                Assert.Contains(
                    $"The service TestService has thrown an exception.{Environment.NewLine}" +
                    $"HttpStatusCode is NotFound.{Environment.NewLine}" +
                    $"No JSON error details were specified.{Environment.NewLine}" +
                    $"Raw error details are: {NotFoundError}", exception.ToString());
                Assert.True(exception.Error.IsOnlyRawContent);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 取回檔案儲存
        /// </summary>
        /// <param name="sourcePathWithFileName">來源路徑與檔案名稱</param>
        /// <param name="targetPathWithFileName">目標路徑與檔案名稱</param>
        /// <returns></returns>
        public bool DownloadToFile(string sourcePathWithFileName, string targetPathWithFileName)
        {
            sourcePathWithFileName = sourcePathWithFileName.Replace("\\", "/");

            var objectToDownload = _storage.Objects.Get(_bucketName, sourcePathWithFileName).Execute();

            var downloader = new MediaDownloader(_storage);

            downloader.ProgressChanged += progress =>
            {
                Console.WriteLine($"{progress.Status} {progress.BytesDownloaded} bytes");
            };

            using (var fileStream = new FileStream(targetPathWithFileName, FileMode.Create))
            {
                var progress = downloader.Download(objectToDownload.MediaLink, fileStream);

                if (progress.Status == DownloadStatus.Completed)
                {
                    Console.WriteLine($"Downloaded {sourcePathWithFileName} to {targetPathWithFileName}");
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 9
0
        private bool RestoreDatabase(DriveService driveservice, string downloadurl)
        {
            bool flag       = false;
            var  downloader = new MediaDownloader(driveservice);

            int last_dot;
            var file_name = "";

            //if database type is sqlite
            if (StaticClass.GeneralClass.flag_database_type_general == false)
            {
                download_file_name      = @"Databses\CheckOut.db";
                last_dot                = download_file_name.LastIndexOf(".");
                download_directory_name = "Databases";
                file_name               = download_directory_name + @"\CheckOut" + (last_dot != -1 ? "." + download_file_name.Substring(last_dot + 1) : "");
            }
            //if database type is sqlserver
            else
            {
                //check exist of folder temp contain database backup
                if (System.IO.Directory.Exists(current_directory + @"\Database_Ser_Temp") == false)
                {
                    System.IO.Directory.CreateDirectory(current_directory + @"\Database_Ser_Temp");
                }

                //check database exist
                if (System.IO.File.Exists(current_directory + @"\Database_Ser_Temp\CheckOut.sql") == true)
                {
                    System.IO.File.Delete(current_directory + @"\Database_Ser_Temp\CheckOut.sql");
                }
                if (!string.IsNullOrEmpty(extDownload) && extDownload == "bak")
                {
                    download_file_name = @"Databses\CheckOut.bak";
                }
                else
                {
                    download_file_name = @"Databses\CheckOut.db";
                }
                last_dot = download_file_name.LastIndexOf(".");
                download_directory_name = "Database_Ser_Temp";
                file_name = download_directory_name + @"\CheckOut" + (last_dot != -1 ? "." + download_file_name.Substring(last_dot + 1) : "");
            }

            var full_path = System.IO.Path.Combine(file_name);

            filestream = new System.IO.FileStream(full_path, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);

            if (downloader.Download(downloadurl, filestream) != null)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }

            filestream.Dispose();
            return(flag);
        }
Ejemplo n.º 10
0
        public void DownloadFileTest()
        {
            MediaDownloader downloader     = new MediaDownloader();
            MediaFile       file           = MediaFile.CreateNew(VboxDownloadVideo);
            string          downloadedFile = downloader.Download(file, Environment.GetFolderPath(Environment.SpecialFolder.Desktop)).DownloadPath;

            Assert.AreEqual(VboxDownloadedVideoPath, downloadedFile);
        }
Ejemplo n.º 11
0
        public void ResumesDownloadProperly()
        {
            MediaDownloader downloader = new MediaDownloader();

            bool canceled   = false;
            bool downloaded = false;

            downloader.MediaFileDownloadProgress += (s, e) =>
            {
                if (((int)e.PercentageComplete >= 25 && (int)e.PercentageComplete <= 30) && !canceled)
                {
                    e.Cancel = canceled = true;
                }

                if (e.PercentageComplete >= 100)
                {
                    downloaded = true;
                }
            };

            MediaFile file = MediaFile.CreateNew(MediaFileTests.VboxTestUrl);
            string    path = Path.Combine(Directory.GetCurrentDirectory(), file.Metadata.FileName + file.Metadata.FileExtension);

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            var firstResult = downloader.Download(file, Directory.GetCurrentDirectory());

            if (!firstResult.IsDownloaded || firstResult.Exceptions.Any())
            {
                throw new Exception();
            }

            var secondDownload = downloader.Download(file, Directory.GetCurrentDirectory(), true);

            if (!secondDownload.IsDownloaded || secondDownload.Exceptions.Any())
            {
                throw new Exception();
            }

            Assert.IsTrue(downloaded);
        }
Ejemplo n.º 12
0
        public void IsDownloadingTest()
        {
            MediaFile file = MediaFile.CreateNew(BasicSoundCloudFileTests.SoundCloudLink);

            MediaDownloader downloader = new MediaDownloader();
            var             result     = downloader.Download(file, Directory.GetCurrentDirectory());

            Assert.IsTrue(result.IsDownloaded);
            Assert.IsTrue(File.Exists(result.DownloadPath));
        }
Ejemplo n.º 13
0
        public void DownloadFileStartingEventTest()
        {
            MediaDownloader downloader = new MediaDownloader();
            bool            fired      = false;
            MediaFile       file       = MediaFile.CreateNew(VboxDownloadVideo);

            downloader.MediaFileDownloadStarting += (s, e) => fired = true;
            string downloadedFile = downloader.Download(file, Environment.GetFolderPath(Environment.SpecialFolder.Desktop)).DownloadPath;

            Assert.AreEqual(fired, true);
        }
Ejemplo n.º 14
0
        public bool DownloadUpdate()
        {
            Global.Logger.Log(ConsoleColor.Cyan, LogType.Updater, null, "Updating..");

            if (Directory.Exists("Update") && Directory.EnumerateFiles("Update").Count() != 0)
            {
                try
                {
                    Directory.Delete("Update", true);
                }
                catch (IOException)
                {
                    Global.Logger.Log(ConsoleColor.Red, LogType.Updater, "Download", "Something is using the Update folder! Please consider removing it manually!");
                    return(false);
                }
            }

            Directory.CreateDirectory("Update");

            var Files = Global.GoogleDrive.Files.List().Execute().Items;

            var Packages = Files.Where(p =>
            {
                return(p.OriginalFilename == "Chino-chan.exe");
            }).OrderBy(s =>
            {
                return(s.CreatedDate);
            });

            if (Packages.Count() == 0)
            {
                Global.Logger.Log(ConsoleColor.Red, LogType.GoogleDrive, null, "Couldn't find Chino-chan.exe! Please upload to update!");
                return(false);
            }

            var ChinoChan = Packages.First();

            var ChinoChanPath = "Update\\Chino-chan.exe";

            Global.Logger.Log(ConsoleColor.Cyan, LogType.Updater, null, "Downloading update..");

            var Progress = Downloader.Download(ChinoChan.DownloadUrl, new FileStream(ChinoChanPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read));

            if (Progress.Status != DownloadStatus.Completed)
            {
                Global.Logger.Log(ConsoleColor.Red, LogType.GoogleDrive, null, "Couldn't download the latest Chino-chan.exe: " + Progress.Exception.ToString());
                return(false);
            }
            Global.Logger.Log(ConsoleColor.Cyan, LogType.Updater, null, "Downloaded!");

            return(true);
        }
        /// <summary>
        /// Uses MediaDownloader to download the contents of a URI.
        /// Asserts that the download succeeded and returns the resulting content as a string.
        /// </summary>
        /// <param name="uri">Uri to download</param>
        /// <returns></returns>
        private string SimpleDownload(string uri)
        {
            using (var service = new MockClientService())
            {
                var downloader   = new MediaDownloader(service);
                var outputStream = new MemoryStream();
                var result       = downloader.Download(uri, outputStream);

                Assert.AreEqual(result.Status, DownloadStatus.Completed);
                Assert.IsNull(result.Exception);
                Assert.AreEqual(result.BytesDownloaded, outputStream.Position);

                return(Encoding.UTF8.GetString(outputStream.GetBuffer(), 0, (int)outputStream.Position));
            }
        }
Ejemplo n.º 16
0
        public void IsConvertingToMp3()
        {
            MediaDownloader downloader   = new MediaDownloader();
            MediaFile       mediaFile    = MediaFile.CreateNew(VboxDownloadConvertTests.VboxDownloadVideo);
            string          existingPath = Path.Combine(Directory.GetCurrentDirectory(), mediaFile.Metadata.FileName + mediaFile.Metadata.FileExtension);

            if (!File.Exists(existingPath))
            {
                downloader.Download(mediaFile, Directory.GetCurrentDirectory());
            }

            MediaConverter converter = new MediaConverter();
            ConvertResult  result    = converter.Convert(mediaFile, existingPath, Directory.GetCurrentDirectory(), new MediaConverterMetadata(Bitrates.Kbps192));

            Assert.IsTrue(result.IsConverted);
            Assert.IsTrue(File.Exists(result.ConvertedPath));
        }
Ejemplo n.º 17
0
        public void DownloadFileCompletedEventTest()
        {
            MediaDownloader downloader = new MediaDownloader();

            bool downloaded = false;

            MediaFile file = MediaFile.CreateNew(VboxDownloadVideo);

            downloader.MediaFileDownloadFinished += (s, e) =>
            {
                downloaded = true;
            };

            string downloadedFile = downloader.Download(file, Environment.GetFolderPath(Environment.SpecialFolder.Desktop)).DownloadPath;

            Assert.AreEqual(true, downloaded);
        }
        public void Download_Error_JsonResponse()
        {
            var downloadUri = "http://www.sample.com";
            var chunkSize   = 100;
            var error       = new RequestError {
                Code = 12345, Message = "Text", Errors = new[] { new SingleError {
                                                                     Message = "Nested error"
                                                                 } }
            };
            var response = new StandardResponse <object> {
                Error = error
            };
            var responseText = new NewtonsoftJsonSerializer().Serialize(response);

            var handler = new MultipleChunksMessageHandler {
                ErrorResponse = responseText
            };

            handler.StatusCode = HttpStatusCode.BadRequest;
            handler.ChunkSize  = chunkSize;
            // The media downloader adds the parameter...
            handler.DownloadUri = new Uri(downloadUri + "?alt=media");

            using (var service = CreateMockClientService(handler))
            {
                var downloader = new MediaDownloader(service);
                downloader.ChunkSize = chunkSize;
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                downloader.Download(downloadUri, outputStream);

                var lastProgress = progressList.LastOrDefault();
                Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed));
                GoogleApiException exception = (GoogleApiException)lastProgress.Exception;
                Assert.That(exception.HttpStatusCode, Is.EqualTo(handler.StatusCode));
                // Just a smattering of checks - if these two pass, it's surely okay.
                Assert.That(exception.Error.Code, Is.EqualTo(error.Code));
                Assert.That(exception.Error.Errors[0].Message, Is.EqualTo(error.Errors[0].Message));
            }
        }
        public void InterceptGzip()
        {
            var compressedData = new MemoryStream();

            using (var gzipStream = new GZipStream(compressedData, CompressionMode.Compress, true))
            {
                gzipStream.Write(MediaContent, 0, MediaContent.Length);
            }

            var interceptedData           = new MemoryStream();
            StreamInterceptor interceptor = interceptedData.Write;
            int interceptorProviderCount  = 0;

            using (var service = new MockClientService())
            {
                var downloader = new MediaDownloader(service)
                {
                    ChunkSize = 10,
                    ResponseStreamInterceptorProvider = _ =>
                    {
                        interceptorProviderCount++;
                        return(interceptor);
                    }
                };

                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                downloader.Download(_httpPrefix + "GzipContent", outputStream);

                // We only had one HTTP response, even though we read it in multiple chunks.
                Assert.Equal(1, interceptorProviderCount);

                // The end result is decompressed content.
                Assert.Equal(MediaContent, outputStream.ToArray());

                // We intercepted the compressed content.
                Assert.Equal(compressedData.ToArray(), interceptedData.ToArray());
            }
        }
Ejemplo n.º 20
0
        public void ConversionEndedEventTest()
        {
            MediaDownloader downloader = new MediaDownloader();

            MediaFile file           = MediaFile.CreateNew(VboxDownloadVideo);
            string    downloadedPath = downloader.Download(file, Environment.GetFolderPath(Environment.SpecialFolder.Desktop)).DownloadPath;
            bool      converting     = false;

            MediaConverter converter = new MediaConverter();

            converter.MediaFileConvertionCompelete += delegate { converting = true; };
            converter.Convert(file, downloadedPath, Environment.GetFolderPath(Environment.SpecialFolder.Desktop), new MediaConverterMetadata
            {
                AudioBitrate = Bitrates.Kbps192,
                Extension    = SupportedConversionFormats.Mp3,
                FileName     = file.Metadata.FileName
            });

            Assert.AreEqual(true, converting);
        }
        public void DownloadRange()
        {
            using (var service = new MockClientService())
            {
                int startInclusive = 5;
                int endInclusive   = 13;
                var downloader     = new MediaDownloader(service);
                downloader.Range = new RangeHeaderValue(startInclusive, endInclusive);
                var outputStream = new MemoryStream();
                var result       = downloader.Download(_httpPrefix + "content", outputStream);

                Assert.Equal(DownloadStatus.Completed, result.Status);
                Assert.Null(result.Exception);
                Assert.Equal(result.BytesDownloaded, outputStream.Position);

                byte[] bytes = outputStream.ToArray();
                Assert.Equal(endInclusive - startInclusive + 1, bytes.Length);
                byte[] expectedBytes = MediaContent.Skip(startInclusive).Take(bytes.Length).ToArray();
                Assert.Equal(expectedBytes, bytes);
            }
        }
Ejemplo n.º 22
0
        public void DownloadFileProgressEventTest()
        {
            MediaDownloader downloader = new MediaDownloader();

            bool hasDownloadSize = false;
            bool hasMaxSize      = false;
            bool hasProgress     = false;

            MediaFile file = MediaFile.CreateNew(VboxDownloadVideo);

            downloader.MediaFileDownloadProgress += (s, e) =>
            {
                hasDownloadSize = e.DownloadedSize > 0;
                hasMaxSize      = e.FileSize > 0;
                hasProgress     = e.PercentageComplete > 0;
            };

            string downloadedFile = downloader.Download(file, Environment.GetFolderPath(Environment.SpecialFolder.Desktop)).DownloadPath;

            Assert.AreEqual(true, hasDownloadSize && hasMaxSize && hasProgress);
        }
        public void Download_Error_PlaintextResponse()
        {
            using (var service = new MockClientService())
            {
                var downloader = new MediaDownloader(service);
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                downloader.Download(_httpPrefix + "NotFoundPlainText", outputStream);

                var lastProgress = progressList.LastOrDefault();
                Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed));
                GoogleApiException exception = (GoogleApiException)lastProgress.Exception;
                Assert.That(exception.HttpStatusCode, Is.EqualTo(HttpStatusCode.NotFound));
                Assert.That(exception.Message, Is.EqualTo(NotFoundError));
                Assert.IsNull(exception.Error);
            }
        }
Ejemplo n.º 24
0
        // [END delete_object]

        // [START media_downloader]
        public void DownloadToFile(string bucketName)
        {
            StorageService storage = CreateStorageClient();

            var objectToDownload = storage.Objects.Get(bucketName, "my-file.txt").Execute();

            var downloader = new MediaDownloader(storage);

            downloader.ProgressChanged += progress =>
            {
                Console.WriteLine($"{progress.Status} {progress.BytesDownloaded} bytes");
            };

            using (var fileStream = new FileStream("downloaded-file.txt", FileMode.Create))
            {
                var progress = downloader.Download(objectToDownload.MediaLink, fileStream);

                if (progress.Status == Google.Apis.Download.DownloadStatus.Completed)
                {
                    Console.WriteLine("Downloaded my-file.txt to downloaded-file.txt");
                }
            }
        }
        public void InterceptPlain()
        {
            var interceptedData           = new MemoryStream();
            StreamInterceptor interceptor = interceptedData.Write;
            int interceptorProviderCount  = 0;

            using (var service = new MockClientService())
            {
                var downloader = new MediaDownloader(service)
                {
                    ChunkSize = 10,
                    ResponseStreamInterceptorProvider = _ =>
                    {
                        interceptorProviderCount++;
                        return(interceptor);
                    }
                };

                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                downloader.Download(_httpPrefix + "PlainText", outputStream);

                // We only had one HTTP response, even though we read it in multiple chunks.
                Assert.Equal(1, interceptorProviderCount);

                // The end result is plain text content.
                Assert.Equal(MediaContent, outputStream.ToArray());

                // We intercepted the plain text content too.
                Assert.Equal(MediaContent, interceptedData.ToArray());
            }
        }
        public void Download_NoContent()
        {
            using (var service = new MockClientService())
            {
                var downloader = new MediaDownloader(service);
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                downloader.Download(_httpPrefix + "NoContent", outputStream);

                // We expect only one event -- "completed".
                Assert.That(progressList.Count, Is.EqualTo(1));

                var progress = progressList[0];

                Assert.That(progress.Status, Is.EqualTo(DownloadStatus.Completed));
                Assert.That(progress.BytesDownloaded, Is.EqualTo(0));
                Assert.That(outputStream.Length, Is.EqualTo(0));
            }
        }
        /// <summary>A helper test to test sync and async downloads.</summary>
        /// <param name="chunkSize">The chunk size for each part.</param>
        /// <param name="sync">Indicates if this download should be synchronously or asynchronously.</param>
        /// <param name="cancelChunk">Defines the chunk at which to cancel the download request.</param>
        /// <param name="target">Last component of the Uri to download</param>
        private void Subtest_Download_Chunks(int chunkSize, bool sync = true, int cancelChunk = 0, string target = "content")
        {
            string downloadUri = _httpPrefix + target;
            var    cts         = new CancellationTokenSource();

            using (var service = new MockClientService())
            {
                var downloader = new MediaDownloader(service);
                downloader.ChunkSize = chunkSize;
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                int  progressUpdates     = 0;
                long lastBytesDownloaded = 0;
                downloader.ProgressChanged += (p) =>
                {
                    if (p.Status != DownloadStatus.Failed)
                    {
                        // We shouldn't receive duplicate notifications for the same range.
                        Assert.That(p.BytesDownloaded, Is.GreaterThan(lastBytesDownloaded));
                    }
                    lastBytesDownloaded = p.BytesDownloaded;

                    progressList.Add(p);
                    if (++progressUpdates == cancelChunk)
                    {
                        cts.Cancel();
                    }
                };

                var outputStream = new MemoryStream();
                if (sync)
                {
                    downloader.Download(downloadUri, outputStream);
                }
                else
                {
                    try
                    {
                        var result = downloader.DownloadAsync(downloadUri, outputStream, cts.Token).Result;
                        if (result.Exception == null)
                        {
                            Assert.AreEqual(0, cancelChunk);
                        }
                        else
                        {
                            Assert.IsInstanceOf <OperationCanceledException>(result.Exception);
                        }
                    }
                    catch (AggregateException ex)
                    {
                        Assert.IsInstanceOf <TaskCanceledException>(ex.InnerException);
                    }
                }

                var lastProgress = progressList.LastOrDefault();
                if (cancelChunk > 0)
                {
                    // last request should fail
                    Assert.NotNull(lastProgress);
                    Assert.NotNull(lastProgress.Exception);
                    Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed));
                    Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(chunkSize * cancelChunk));
                }
                else
                {
                    Assert.NotNull(lastProgress);
                    Assert.Null(lastProgress.Exception);
                    Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Completed));
                    Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(MediaContent.Length));

                    byte[] actual = outputStream.ToArray();
                    CollectionAssert.AreEqual(MediaContent, actual);
                }
            }
        }
Ejemplo n.º 28
0
        /// <summary>A helper test to test sync and async downloads.</summary>
        /// <param name="chunkSize">The chunk size for each part.</param>
        /// <param name="sync">Indicates if this download should be synchronously or asynchronously.</param>
        /// <param name="cancelRequest">Defines the request index to cancel the download request.</param>
        private void Subtest_Download_Chunks(int chunkSize, bool sync = true, int cancelRequest = 0)
        {
            // reset the steam
            StreamContent.Position = 0;

            string downloadUri = "http://www.sample.com";
            var    handler     = new MultipleChunksMessageHandler();

            handler.ChunkSize   = chunkSize;
            handler.DownloadUri = new Uri(downloadUri + "?alt=media");

            // support cancellation
            if (cancelRequest > 0)
            {
                handler.CancelRequestNum = cancelRequest;
            }
            handler.CancellationTokenSource = new CancellationTokenSource();

            int expectedCalls = (int)Math.Ceiling((double)StreamContent.Length / chunkSize);

            using (var service = new MockClientService(new BaseClientService.Initializer()
            {
                HttpClientFactory = new MockHttpClientFactory(handler)
            }))
            {
                var downloader = new MediaDownloader(service);
                downloader.ChunkSize = chunkSize;
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                if (sync)
                {
                    downloader.Download(downloadUri, outputStream);
                    Assert.AreEqual(handler.ThreadId, Thread.CurrentThread.ManagedThreadId);
                }
                else
                {
                    var task = downloader.DownloadAsync(downloadUri, outputStream,
                                                        handler.CancellationTokenSource.Token);
                    try
                    {
                        task.Wait();
                        Assert.AreEqual(cancelRequest, 0);
                    }
                    catch (AggregateException ex)
                    {
                        Assert.That(ex.InnerException, Is.InstanceOf <TaskCanceledException>());
                        Assert.AreNotEqual(cancelRequest, 0);
                    }
                    Assert.AreNotEqual(handler.ThreadId, Thread.CurrentThread.ManagedThreadId);
                }

                var lastProgress = progressList.LastOrDefault();
                if (cancelRequest > 0)
                {
                    // the download was interrupted in the middle
                    Assert.That(handler.Calls, Is.EqualTo(cancelRequest));
                    // last request should be failed
                    Assert.NotNull(lastProgress);
                    Assert.NotNull(lastProgress.Exception);
                    Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed));
                    Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(chunkSize * cancelRequest));
                }
                else
                {
                    // the download succeeded
                    Assert.That(handler.Calls, Is.EqualTo(expectedCalls));
                    Assert.NotNull(lastProgress);
                    Assert.Null(lastProgress.Exception);
                    Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Completed));
                    Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(StreamContent.Length));

                    byte[] read = new byte[1000];
                    outputStream.Position = 0;
                    int length = outputStream.Read(read, 0, 1000);
                    Assert.That(Encoding.UTF8.GetString(read, 0, length), Is.EqualTo(MediaContent));
                }
            }
        }
Ejemplo n.º 29
0
        public void Run(IConfigurableHttpClientInitializer credential,
                        string projectId, string bucketName)
        {
            StorageService service = new StorageService(
                new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = "GCS Sample",
            });

            Console.WriteLine("List of buckets in current project");
            Buckets buckets = service.Buckets.List(projectId).Execute();

            foreach (var bucket in buckets.Items)
            {
                Console.WriteLine(bucket.Name);
            }

            Console.WriteLine("Total number of items in bucket: "
                              + buckets.Items.Count);
            Console.WriteLine("=============================");

            // using Google.Apis.Storage.v1.Data.Object to disambiguate from
            // System.Object
            Google.Apis.Storage.v1.Data.Object fileobj =
                new Google.Apis.Storage.v1.Data.Object()
            {
                Name = "somefile.txt"
            };

            Console.WriteLine("Creating " + fileobj.Name + " in bucket "
                              + bucketName);
            byte[] msgtxt = Encoding.UTF8.GetBytes("Lorem Ipsum");

            service.Objects.Insert(fileobj, bucketName,
                                   new MemoryStream(msgtxt), "text/plain").Upload();

            Console.WriteLine("Object created: " + fileobj.Name);

            Console.WriteLine("=============================");

            Console.WriteLine("Reading object " + fileobj.Name + " in bucket: "
                              + bucketName);
            var req = service.Objects.Get(bucketName, fileobj.Name);

            Google.Apis.Storage.v1.Data.Object readobj = req.Execute();

            Console.WriteLine("Object MediaLink: " + readobj.MediaLink);

            // download using Google.Apis.Download and display the progress
            string pathUser = Environment.GetFolderPath(
                Environment.SpecialFolder.UserProfile);
            var fileName = Path.Combine(pathUser, "Downloads") + "\\"
                           + readobj.Name;

            Console.WriteLine("Starting download to " + fileName);
            var downloader = new MediaDownloader(service)
            {
                ChunkSize = DownloadChunkSize
            };

            // add a delegate for the progress changed event for writing to
            // console on changes
            downloader.ProgressChanged += progress =>
                                          Console.WriteLine(progress.Status + " "
                                                            + progress.BytesDownloaded + " bytes");

            using (var fileStream = new System.IO.FileStream(fileName,
                                                             System.IO.FileMode.Create, System.IO.FileAccess.Write))
            {
                var progress =
                    downloader.Download(readobj.MediaLink, fileStream);
                if (progress.Status == DownloadStatus.Completed)
                {
                    Console.WriteLine(readobj.Name
                                      + " was downloaded successfully");
                }
                else
                {
                    Console.WriteLine("Download {0} was interrupted. Only {1} "
                                      + "were downloaded. ",
                                      readobj.Name, progress.BytesDownloaded);
                }
            }
            Console.WriteLine("=============================");
        }
        /// <summary>A helper test to test sync and async downloads.</summary>
        /// <param name="chunkSize">The chunk size for each part.</param>
        /// <param name="sync">Indicates if this download should be synchronously or asynchronously.</param>
        /// <param name="cancelRequest">Defines the request index to cancel the download request.</param>
        /// <param name="downloadUri">The URI which contains the media to download.</param>
        private void Subtest_Download_Chunks(int chunkSize, bool sync = true, int cancelRequest = 0,
                                             string downloadUri       = "http://www.sample.com")
        {
            var handler = new MultipleChunksMessageHandler(MediaContent);

            handler.StatusCode  = HttpStatusCode.OK;
            handler.ChunkSize   = chunkSize;
            handler.DownloadUri = new Uri(downloadUri +
                                          (downloadUri.Contains("?") ? "&" : "?") + "alt=media");

            // support cancellation
            if (cancelRequest > 0)
            {
                handler.CancelRequestNum = cancelRequest;
            }
            handler.CancellationTokenSource = new CancellationTokenSource();

            int expectedCalls = (int)Math.Ceiling((double)MediaContent.Length / chunkSize);

            using (var service = CreateMockClientService(handler))
            {
                var downloader = new MediaDownloader(service);
                downloader.ChunkSize = chunkSize;
                IList <IDownloadProgress> progressList = new List <IDownloadProgress>();
                downloader.ProgressChanged += (p) =>
                {
                    progressList.Add(p);
                };

                var outputStream = new MemoryStream();
                if (sync)
                {
                    downloader.Download(downloadUri, outputStream);
                }
                else
                {
                    try
                    {
                        var result = downloader.DownloadAsync(downloadUri, outputStream,
                                                              handler.CancellationTokenSource.Token).Result;
                        Assert.AreEqual(0, handler.CancelRequestNum);
                    }
                    catch (AggregateException ex)
                    {
                        Assert.IsInstanceOf <TaskCanceledException>(ex.InnerException);
                    }
                }

                var lastProgress = progressList.LastOrDefault();
                if (cancelRequest > 0)
                {
                    // the download was interrupted in the middle
                    Assert.That(handler.Calls, Is.EqualTo(cancelRequest));
                    // last request should fail
                    Assert.NotNull(lastProgress);
                    Assert.NotNull(lastProgress.Exception);
                    Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Failed));
                    Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(chunkSize * cancelRequest));
                }
                else
                {
                    // the download succeeded
                    Assert.That(handler.Calls, Is.EqualTo(expectedCalls));
                    Assert.NotNull(lastProgress);
                    Assert.Null(lastProgress.Exception);
                    Assert.That(lastProgress.Status, Is.EqualTo(DownloadStatus.Completed));
                    Assert.That(lastProgress.BytesDownloaded, Is.EqualTo(MediaContent.Length));

                    byte[] actual = outputStream.ToArray();
                    CollectionAssert.AreEqual(MediaContent, actual);
                }
            }
        }