Example #1
0
        public async Task ExampleUsage1()
        {
            DirectoryEntry dir = EnvironmentV2.instance.GetOrAddTempFolder("Download_ExampleUsage1").CreateV2();
            IFileRef       f   = new FileRef()
            {
                url = "https://raw.githubusercontent.com/cs-util-com/cscore/master/LICENSE"
            };
            bool wasDownloadNeeded = await f.DownloadTo(dir, (float progress) => {
                Log.d($"Download {progress}% done");
            }, useAutoCachedFileRef : true);

            string licenseText = f.GetFileEntry(dir.FileSystem).LoadAs <string>();

            Assert.Equal(11344, licenseText.Length);
        }
        public async Task TestImageFileRef()
        {
            var dir = EnvironmentV2.instance.GetOrAddTempFolder("TestImageFileWithThumbnail");

            var imgRef = new FileRef()
            {
                url = "https://picsum.photos/1024/512"
            };
            await imgRef.DownloadTo(dir);

            Log.d("FileRef: " + JsonWriter.AsPrettyString(imgRef));
            Assert.NotNull(imgRef.url);
            Assert.NotNull(imgRef.fileName);
            Assert.NotNull(imgRef.dir);

            FileEntry imgEntry = imgRef.GetFileEntry(dir.FileSystem);
            var       img      = await ImageLoader.LoadImageInBackground(imgEntry);

            Assert.Equal(1024, img.Width);
            Assert.Equal(512, img.Height);
        }
        public async Task TestLargeFileDownloadWithProgress()
        {
            var dir = EnvironmentV2.instance.GetOrAddTempFolder("TestLargeFileDownloadWithProgress");

            // Url from https://gist.github.com/jsturgis/3b19447b304616f18657
            var f = new FileRef()
            {
                url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
            };
            await Assert.ThrowsAsync <TaskCanceledException>(async() => {
                await f.DownloadTo(dir, downloadProgress => {
                    if (downloadProgress > 5)
                    {
                        throw new TaskCanceledException("Download canceled after 5%");
                    }
                });
            });

            Assert.NotNull(f.url);
            // Local values should only be set after successful download to have a more atomic predicatable behavior:
            Assert.Null(f.fileName);
            Assert.Null(f.dir);
            Log.d("FileRef: " + JsonWriter.AsPrettyString(f));
        }