Пример #1
0
        private HttpResponseMessage ExportZipFile(ProjectSummaryPrintSessionDTO session, Guid id, bool timeline, bool locations, bool piechart)
        {
            if (CurrentOrgUser != null)
            {
                var project    = UnitOfWork.ProjectsRepository.Find(session.ProjectId);
                var assignment = project.Assignments.SingleOrDefault(a => a.OrgUserId == CurrentOrgUser.Id);
                if (assignment == null || !assignment.CanExportZip)
                {
                    return(new HttpResponseMessage(HttpStatusCode.Forbidden));
                }
            }

            var rootIndex = WebHelpers.GetRootIndexPath();
            var authData  = $"{{\"token\":\"{HttpContext.Current.Request.Headers["Authorization"].Substring(7)}\",\"email\":\"{CurrentUser.Email}\"}}";
            var url       = $"{Request.RequestUri.Scheme}://{Request.RequestUri.Authority}/{rootIndex}?authData={authData}#!/projects/summary/print/{id.ToString()}?timeline={timeline}&locations={locations}&piechart={piechart}";

            var surveys = UnitOfWork.FilledFormsRepository.AllAsNoTracking
                          .Where(s => session.SurveyIds.Contains(s.Id)).ToList();

            var rootFolder = HostingEnvironment.MapPath("/ExportTemp");

            if (Directory.Exists(rootFolder))
            {
                Directory.Delete(rootFolder, true);
            }
            Directory.CreateDirectory(rootFolder);

            foreach (var survey in surveys)
            {
                var attachments = UnitOfWork.AttachmentsRepository.AllAsNoTracking
                                  .Where(a => a.FormValue.FilledFormId == survey.Id).ToList();

                foreach (var attch in attachments)
                {
                    string folderPath = string.Empty;
                    if (!string.IsNullOrEmpty(survey.Description))
                    {
                        var sanitizedFileName = WebHelpers.SanitizeFileName(survey.FormTemplate.Title);
                        folderPath = HostingEnvironment.MapPath($"/ExportTemp/{survey.Serial}_{sanitizedFileName}");
                    }
                    else
                    {
                        folderPath = HostingEnvironment.MapPath($"/ExportTemp/{survey.Serial}");
                    }

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

                    try
                    {
                        var filePath = HostingEnvironment.MapPath(attch.Url);
                        var fileInfo = new FileInfo(filePath);
                        var dest     = Path.Combine(folderPath, attch.FileName);
                        fileInfo.CopyTo(dest);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
            }

            var projectName = UnitOfWork.ProjectsRepository.Find(session.ProjectId).Name;
            var pdfFileName = $"{projectName}.pdf";

            var pdfData     = ConvertHtmlToPdf(url);
            var pdfFilePath = Path.Combine(rootFolder, pdfFileName);
            var fs          = new FileStream(pdfFilePath, FileMode.Create);

            fs.Write(pdfData, 0, pdfData.Length);
            fs.Flush();
            fs.Close();

            var zipRootPath = HostingEnvironment.MapPath("/ZipTemp");

            if (Directory.Exists(zipRootPath))
            {
                Directory.Delete(zipRootPath, true);
            }
            Directory.CreateDirectory(zipRootPath);

            var zipFilename = $"{projectName}.zip";
            var zipFilePath = Path.Combine(zipRootPath, zipFilename);

            ZipFile.CreateFromDirectory(rootFolder, zipFilePath);

            var zipData  = System.IO.File.ReadAllBytes(zipFilePath);
            var response = CreateZipResponseMessage(zipData, zipFilename);

            Directory.Delete(rootFolder, true);
            Directory.Delete(zipRootPath, true);

            return(response);
        }