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)