コード例 #1
0
        public IActionResult Index(string path)
        {
            const string root = @"C:\Users\Michal Piatkowski\Downloads\";

            if (path == null)
            {
                path = root;
            }
            else
            {
                path = Path.Combine(root, path);
            }
            path = path.Replace('\\', '/');

            //var archiveResult = BuildArchiveView(path);
            //if (archiveResult.IsValid)
            //{
            //    if (archiveResult.HasFile)
            //    {
            //        // serve file from inside the zip
            //        string fullName = archiveResult.File.FullName;
            //        var entry = archiveResult.Archive[fullName];
            //        HttpContext.Response.RegisterForDispose(archiveResult.Archive);
            //
            //        if (archiveResult.File.Length <= HIGHLIGHT_SIZE_THRESHOLD &&
            //            HighlightTypeMap.TryGetLanguage(Path.GetExtension(fullName), out var lang))
            //        {
            //            var highlightStream = entry.OpenReader();
            //            HttpContext.Response.RegisterForDispose(highlightStream);
            //
            //            var highlight = new HighlightModel(highlightStream, lang);
            //            return View("HighlightIndex", highlight);
            //        }
            //
            //        string mime = MimeTypeMap.GetMime(fullName);
            //        if (IsTextMime(mime))
            //        {
            //            using (var stream = entry.OpenReader())
            //                mime = UpdateCharset(stream, mime);
            //        }
            //
            //        var mainStream = entry.OpenReader();
            //        HttpContext.Response.RegisterForDispose(mainStream);
            //        return File(mainStream, mime, enableRangeProcessing: false);
            //    }
            //
            //    if(archiveResult.HasModel)
            //        // serve directory from inside the zip
            //        return View(archiveResult.Model);
            //
            //    return NotFound();
            //}

            // serve file
            if (FileIO.Exists(path))
            {
                // try to enter a zip archive
                string zipPath = "testzip.zip"; //root + @"FTB+Presents+Direwolf20+1.12-1.12.2-2.3.0-Server.zip";
                if (ZipFile.IsZipFile(zipPath))
                {
                    var builder     = new ArchiveTreeBuilder();
                    var archive     = ZipFile.Read(zipPath).ToArchive();
                    var archiveView = builder.BuildTree(archive, null); // (p) => Console.WriteLine("Progress: " + Math.Round(p * 100, 1))
                    archive.Dispose();

                    Console.WriteLine(Log(archiveView));
                }

                Stream stream    = FileIO.OpenRead(path);
                string extension = Path.GetExtension(path);

                if (stream.Length <= HIGHLIGHT_SIZE_THRESHOLD &&
                    HighlightTypeMap.TryGetLanguage(extension, out var lang))
                {
                    var highlight = new HighlightModel(stream, lang);
                    return(View("HighlightIndex", highlight));
                }

                string mime = MimeTypeMap.GetMime(extension);
                if (IsTextMime(mime))
                {
                    mime = UpdateCharset(stream, mime);
                }

                stream.Seek(0, SeekOrigin.Begin);
                return(File(stream, mime, enableRangeProcessing: true));
            }

            // serve directory
            var dir = new DirectoryInfo(path);

            if (!dir.Exists)
            {
                return(NotFound());
            }

            var dirModel = BuildDirectoryModel(dir);

            return(View(dirModel));
        }