public async void ExampleStream_ContentLength_MustBe_9702kbytes()
        {
            using (var streamZip = new HttpZipStream(httpUrl))
            {
                var contentLength = await streamZip.GetContentLengthAsync();

                Assert.Equal(9935427, contentLength);
            }
        }
        public async void ExampleStream_Entries_MustHave_36items()
        {
            using (var streamZip = new HttpZipStream(httpUrl))
            {
                var contentLength = await streamZip.GetContentLengthAsync();

                var entryList = await streamZip.GetEntriesAsync();

                Assert.Equal(36, entryList.Count);
            }
        }
        public async Task <IActionResult> SiaZip(string hash, [FromQuery] string path)
        {
            var zipStream = new System.IO.Compression.HttpZipStream("https://siasky.net/" + hash);

            if (await zipStream.GetContentLengthAsync() == -1)
            {
                return(NotFound("No such Sia hash."));
            }
            using (zipStream)
            {
                await zipStream.GetContentLengthAsync();

                string content   = "";
                var    entryList = await zipStream.GetEntriesAsync();

                bool found = false;
                foreach (var e in entryList)
                {
                    if (e.FileName != path)
                    {
                        continue;
                    }
                    found = true;
                    await zipStream.ExtractAsync(e, async (entryStream) =>
                    {
                        byte[] buffer = new byte[4096];
                        while (true)
                        {
                            int result = await entryStream.ReadAsync(buffer, content.Length, 4096);
                            if (result == 0)
                            {
                                break;
                            }
                            content += buffer.ToString();
                        }
                    });
                }
                if (!found)
                {
                    return(NotFound("No such file in archive."));
                }
                System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
                {
                    FileName = path,
                    Inline   = false,
                };
                Response.Headers.Add("Content-Disposition", cd.ToString());
                return(View(content));
            }
        }
        public async void ExampleStream_LargerEntry_MustBe_0001_With_347kbytes()
        {
            using (var streamZip = new HttpZipStream(httpUrl))
            {
                var contentLength = await streamZip.GetContentLengthAsync();

                var entryList = await streamZip.GetEntriesAsync();

                var largerEntry = entryList
                                  .OrderByDescending(x => x.CompressedSize)
                                  .Take(1)
                                  .FirstOrDefault();
                Assert.Equal("Blue Beetle [1967] #01 - 0001.jpg", largerEntry.FileName);
                Assert.Equal(355736, largerEntry.CompressedSize);
            }
        }
        public async void ExampleStream_SmallerEntryExtraction_MustResult_MemoryStream_With_227kbytes()
        {
            using (var streamZip = new HttpZipStream(httpUrl))
            {
                var contentLength = await streamZip.GetContentLengthAsync();

                var entryList = await streamZip.GetEntriesAsync();

                var smallerEntry = entryList
                                   .OrderBy(x => x.CompressedSize)
                                   .Take(1)
                                   .FirstOrDefault();
                long memoryStreamLength = 0;
                await streamZip.ExtractAsync(smallerEntry, (MemoryStream memoryStream) =>
                {
                    memoryStreamLength = memoryStream.Length;
                });

                Assert.Equal(232660, memoryStreamLength);
            }
        }
Beispiel #6
0
        public async Task <List <ComicPageVM> > ExtractPages(LibraryModel library, ComicFile comicFile)
        {
            try
            {
                // INITIALIZE
                short pageIndex = 0;
                if (!Directory.Exists(comicFile.CachePath))
                {
                    Directory.CreateDirectory(comicFile.CachePath);
                }

                // DOWNLOAD COMIC FILE FROM REMOTE SERVER IF LOCAL CACHE DOESNT EXIST YET
                try
                {
                    if (Directory.GetFiles(comicFile.CachePath).Count() == 0)
                    {
                        if (Xamarin.Essentials.Connectivity.NetworkAccess != Xamarin.Essentials.NetworkAccess.Internet)
                        {
                            return(null);
                        }
                        var downloadUrl = await this.Connector.GetDownloadUrlAsync(new FileData { id = comicFile.Key });

                        if (string.IsNullOrEmpty(downloadUrl))
                        {
                            return(null);
                        }

                        // OPEN REMOTE STREAM
                        var downloadStart = DateTime.Now;
                        using (var zipStream = new System.IO.Compression.HttpZipStream(downloadUrl))
                        {
                            // STREAM SIZE
                            var streamSizeValue = comicFile.GetKeyValue("StreamSize");
                            if (!string.IsNullOrEmpty(streamSizeValue))
                            {
                                long streamSize;
                                if (long.TryParse(streamSizeValue, out streamSize))
                                {
                                    zipStream.SetContentLength(streamSize);
                                }
                            }

                            // FIRST ENTRY
                            var entryList = await zipStream.GetEntriesAsync();

                            entryList.RemoveAll(x =>
                                                !x.FileName.ToLower().EndsWith(".jpg") &&
                                                !x.FileName.ToLower().EndsWith(".jpeg") &&
                                                !x.FileName.ToLower().EndsWith(".png"));
                            var entries = entryList
                                          .OrderBy(x => x.FileName)
                                          .ToList();
                            if (entries == null)
                            {
                                return(null);
                            }

                            // RETRIEVE REMOTE IMAGE CONTENT
                            var tasks = new List <Task>();
                            pageIndex = 0;
                            foreach (var entry in entryList)
                            {
                                tasks.Add(this.ExtractPage(zipStream, entry, comicFile.CachePath, pageIndex++));
                            }
                            await Task.WhenAll(tasks.ToArray());
                        }
                        var downloadFinish = DateTime.Now;
                        Helpers.AppCenter.TrackEvent($"Comic.OneDrive.DownloadingPages", $"ElapsedSeconds:{(downloadFinish - downloadStart).TotalSeconds}");
                    }
                }
                catch (Exception exDownload) { Helpers.AppCenter.TrackEvent(exDownload); throw; }
                if (Directory.GetFiles(comicFile.CachePath).Count() == 0)
                {
                    return(null);
                }

                // LOCATE PAGE IMAGES FROM PATH
                var pages = Directory.GetFiles(comicFile.CachePath)
                            .Where(x =>
                                   x.ToLower().EndsWith(".jpg") ||
                                   x.ToLower().EndsWith(".jpeg") ||
                                   x.ToLower().EndsWith(".png"))
                            .OrderBy(x => x)
                            .Select(pagePath => new ComicPageVM
                {
                    Text      = Path.GetFileNameWithoutExtension(pagePath),
                    Path      = pagePath,
                    IsVisible = false
                })
                            .ToList();

                // LOOP THROUGH PAGES
                pageIndex = 0;
                foreach (var page in pages)
                {
                    page.Index = pageIndex++;
                    page.Text  = page.Text.Substring(1);
                    var pageSize = await this.FileSystem.GetPageSize(page.Path);

                    page.PageSize = new ComicPageSize(pageSize.Width, pageSize.Height);
                }

                return(pages);
            }
            catch (Exception ex) { Helpers.AppCenter.TrackEvent(ex); return(null); }
            finally { GC.Collect(); }
        }