From bd9677f0239c7aadbcae2b4b278d23b64de446c5 Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Thu, 11 Jul 2024 15:48:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E8=AF=BE=E4=BB=B6=E6=97=B6?= =?UTF-8?q?=E5=B0=86=E6=89=80=E6=9C=89=E8=AF=BE=E4=BB=B6=E6=89=93=E5=8C=85?= =?UTF-8?q?=E6=88=90=E4=B8=80=E4=B8=AA=E5=8E=8B=E7=BC=A9=E5=8C=85=EF=BC=8C?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E4=B8=BA=20=E8=AF=BE=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E7=A7=B0-=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Ghre/Ghre_CourseWareController.cs | 14 ++++++ Tiobon.Core.Api/Tiobon.Core.xml | 7 +++ .../Ghre/IGhre_CourseWareServices.cs | 3 ++ .../Ghre/Ghre_CourseWareServices.cs | 50 ++++++++++++++++++- Tiobon.Core/Tiobon.Core.xml | 7 +++ 5 files changed, 80 insertions(+), 1 deletion(-) diff --git a/Tiobon.Core.Api/Controllers/Ghre/Ghre_CourseWareController.cs b/Tiobon.Core.Api/Controllers/Ghre/Ghre_CourseWareController.cs index 514e582c..c67d40c2 100644 --- a/Tiobon.Core.Api/Controllers/Ghre/Ghre_CourseWareController.cs +++ b/Tiobon.Core.Api/Controllers/Ghre/Ghre_CourseWareController.cs @@ -11,4 +11,18 @@ public class Ghre_CourseWareController : BaseController + /// 下载附件压缩包 + /// + /// 课件ID + /// + [HttpPost("DownZip/{id}")] + public async Task> DownZip(long id) + { + return await _service.DownZip(id); + } + #endregion + } \ No newline at end of file diff --git a/Tiobon.Core.Api/Tiobon.Core.xml b/Tiobon.Core.Api/Tiobon.Core.xml index a509bc67..c9bf48da 100644 --- a/Tiobon.Core.Api/Tiobon.Core.xml +++ b/Tiobon.Core.Api/Tiobon.Core.xml @@ -622,6 +622,13 @@ 课件(Controller) + + + 下载附件压缩包 + + 课件ID + + 考试(Controller) diff --git a/Tiobon.Core.IServices/Ghre/IGhre_CourseWareServices.cs b/Tiobon.Core.IServices/Ghre/IGhre_CourseWareServices.cs index 0a55a208..6cc7318d 100644 --- a/Tiobon.Core.IServices/Ghre/IGhre_CourseWareServices.cs +++ b/Tiobon.Core.IServices/Ghre/IGhre_CourseWareServices.cs @@ -1,4 +1,5 @@ using Tiobon.Core.IServices.BASE; +using Tiobon.Core.Model; using Tiobon.Core.Model.Models; namespace Tiobon.Core.IServices @@ -8,5 +9,7 @@ namespace Tiobon.Core.IServices /// public interface IGhre_CourseWareServices :IBaseServices { + + Task> DownZip(long id); } } \ No newline at end of file diff --git a/Tiobon.Core.Services/Ghre/Ghre_CourseWareServices.cs b/Tiobon.Core.Services/Ghre/Ghre_CourseWareServices.cs index 42f93363..11f0216f 100644 --- a/Tiobon.Core.Services/Ghre/Ghre_CourseWareServices.cs +++ b/Tiobon.Core.Services/Ghre/Ghre_CourseWareServices.cs @@ -1,8 +1,10 @@  using System.Data; +using System.IO.Compression; +using Microsoft.AspNetCore.Hosting; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using NPOI.Util.Collections; +using SqlSugar; using Tiobon.Core.Common; using Tiobon.Core.Common.Caches; using Tiobon.Core.IRepository.Base; @@ -20,10 +22,12 @@ public class Ghre_CourseWareServices : BaseServices _dal; private IGhre_CourseServices _ghre_CourseServices; + private readonly IWebHostEnvironment _hostingEnvironment; private IGhre_CourseWareAttachmentServices _ghre_CourseWareAttachmentServices; public Ghre_CourseWareServices(ICaching caching, IGhre_CourseServices ghre_CourseServices, + IWebHostEnvironment hostingEnvironment, IGhre_CourseWareAttachmentServices ghre_CourseWareAttachmentServices, IBaseRepository dal) { @@ -32,6 +36,7 @@ public class Ghre_CourseWareServices : BaseServices Add(InsertGhre_CourseWareInput entity) @@ -136,4 +141,47 @@ public class Ghre_CourseWareServices : BaseServices> DownZip(long id) + { + var result = await base.QueryById(id); + if (result is null) + return ServiceResult.OprateFailed("无效的课件ID!"); + var attachments = await _ghre_CourseWareAttachmentServices.Query(x => x.CourseWareId == id); + var webRootPath = _hostingEnvironment.WebRootPath; + var outPath = $"/files/upload/{result.CourseWareName}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.zip"; + var files = attachments.Select(x => $"{webRootPath}{"\\" + x.RelativePath}").ToArray(); + if (files.Length > 0) + { + CreateZip($"{webRootPath}{outPath}", files); + return ServiceResult.OprateSuccess(null, outPath); + + } + else + return ServiceResult.OprateFailed("该课件无附件!"); + + } + + public static void CreateZip(string zipPath, string[] filesToZip) + { + if (File.Exists(zipPath)) + { + throw new IOException("File already exists."); + } + + using (FileStream zipFile = new FileStream(zipPath, FileMode.Create)) + { + using (ZipArchive zipArchive = new ZipArchive(zipFile, ZipArchiveMode.Create)) + { + foreach (string file in filesToZip) + { + if (File.Exists(file)) + { + ZipArchiveEntry entry = zipArchive.CreateEntryFromFile(file, Path.GetFileName(file)); + } + } + } + } + } } \ No newline at end of file diff --git a/Tiobon.Core/Tiobon.Core.xml b/Tiobon.Core/Tiobon.Core.xml index a509bc67..c9bf48da 100644 --- a/Tiobon.Core/Tiobon.Core.xml +++ b/Tiobon.Core/Tiobon.Core.xml @@ -622,6 +622,13 @@ 课件(Controller) + + + 下载附件压缩包 + + 课件ID + + 考试(Controller)