Beispiel #1
0
        public IActionResult DownloadReports([FromBody] DownloadReportsRequest request)
        {
            string downloadDirectory = Path.Combine(_hostingEnvironment.ContentRootPath, "Reports");

            if (!Directory.Exists(downloadDirectory))
            {
                Directory.CreateDirectory(downloadDirectory);
            }

            string dirName = "Reports";

            if (!string.IsNullOrEmpty(request.DownloadFromSubDirectory))
            {
                var destDirPath = Path.GetFullPath(Path.Combine(downloadDirectory + Path.DirectorySeparatorChar));
                downloadDirectory = Path.GetFullPath(Path.Combine(downloadDirectory, request.DownloadFromSubDirectory));

                if (!downloadDirectory.StartsWith(destDirPath))
                {
                    throw new HttpError(HttpStatusCode.Forbidden);
                }

                dirName = request.DownloadFromSubDirectory;
            }

            var zipDirectory = Path.Combine(downloadDirectory, "output");

            var task = _taskFactory.Create <PackageTask>(zipDirectory);

            string zipFilename = string.IsNullOrEmpty(request.DownloadFromSubDirectory)
                ? "Reports.zip"
                : $"{request.DownloadFromSubDirectory}.zip";

            if (Directory.GetFiles(downloadDirectory).Length == 0)
            {
                throw new HttpError(HttpStatusCode.NotFound, "NoReportsFound");
            }

            task.AddDirectoryToPackage(downloadDirectory, dirName, false).ZipPackage(zipFilename, false).Execute(_taskSession);
            string zipPath = Path.Combine(zipDirectory, zipFilename);

            Stream fs = System.IO.File.OpenRead(zipPath);

            return(File(fs, "application/zip", zipFilename));
        }
Beispiel #2
0
 public async Task <Stream> DownloadReportsAsync(DownloadReportsRequest request)
 {
     return(await GetStreamAsync(request));
 }