下载课件时将所有课件打包成一个压缩包,名称为 课件名称-时间

master
xiaochanghai 12 months ago
parent 9137862154
commit bd9677f023
  1. 14
      Tiobon.Core.Api/Controllers/Ghre/Ghre_CourseWareController.cs
  2. 7
      Tiobon.Core.Api/Tiobon.Core.xml
  3. 3
      Tiobon.Core.IServices/Ghre/IGhre_CourseWareServices.cs
  4. 50
      Tiobon.Core.Services/Ghre/Ghre_CourseWareServices.cs
  5. 7
      Tiobon.Core/Tiobon.Core.xml

@ -11,4 +11,18 @@ public class Ghre_CourseWareController : BaseController<IGhre_CourseWareServices
public Ghre_CourseWareController(IGhre_CourseWareServices service) : base(service)
{
}
#region 下载附件压缩包
/// <summary>
/// 下载附件压缩包
/// </summary>
/// <param name="id">课件ID</param>
/// <returns></returns>
[HttpPost("DownZip/{id}")]
public async Task<ServiceResult<string>> DownZip(long id)
{
return await _service.DownZip(id);
}
#endregion
}

@ -622,6 +622,13 @@
课件(Controller)
</summary>
</member>
<member name="M:Tiobon.Core.Api.Controllers.Ghre_CourseWareController.DownZip(System.Int64)">
<summary>
下载附件压缩包
</summary>
<param name="id">课件ID</param>
<returns></returns>
</member>
<member name="T:Tiobon.Core.Api.Controllers.Ghre_ExamController">
<summary>
考试(Controller)

@ -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
/// </summary>
public interface IGhre_CourseWareServices :IBaseServices<Ghre_CourseWare, Ghre_CourseWareDto, InsertGhre_CourseWareInput, EditGhre_CourseWareInput>
{
Task<ServiceResult<string>> DownZip(long id);
}
}

@ -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<Ghre_CourseWare, Ghre_Course
{
private readonly IBaseRepository<Ghre_CourseWare> _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<Ghre_CourseWare> dal)
{
@ -32,6 +36,7 @@ public class Ghre_CourseWareServices : BaseServices<Ghre_CourseWare, Ghre_Course
base._caching = caching;
_ghre_CourseServices = ghre_CourseServices;
_ghre_CourseWareAttachmentServices = ghre_CourseWareAttachmentServices;
_hostingEnvironment = hostingEnvironment;
}
public override async Task<long> Add(InsertGhre_CourseWareInput entity)
@ -136,4 +141,47 @@ public class Ghre_CourseWareServices : BaseServices<Ghre_CourseWare, Ghre_Course
return result;
}
public async Task<ServiceResult<string>> DownZip(long id)
{
var result = await base.QueryById(id);
if (result is null)
return ServiceResult<string>.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<string>.OprateSuccess(null, outPath);
}
else
return ServiceResult<string>.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));
}
}
}
}
}
}

@ -622,6 +622,13 @@
课件(Controller)
</summary>
</member>
<member name="M:Tiobon.Core.Api.Controllers.Ghre_CourseWareController.DownZip(System.Int64)">
<summary>
下载附件压缩包
</summary>
<param name="id">课件ID</param>
<returns></returns>
</member>
<member name="T:Tiobon.Core.Api.Controllers.Ghre_ExamController">
<summary>
考试(Controller)

Loading…
Cancel
Save