|
|
@ -1,8 +1,10 @@ |
|
|
|
|
|
|
|
|
|
|
|
using System.Data; |
|
|
|
using System.Data; |
|
|
|
|
|
|
|
using System.IO.Compression; |
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using Newtonsoft.Json.Linq; |
|
|
|
using Newtonsoft.Json.Linq; |
|
|
|
using NPOI.Util.Collections; |
|
|
|
using SqlSugar; |
|
|
|
using Tiobon.Core.Common; |
|
|
|
using Tiobon.Core.Common; |
|
|
|
using Tiobon.Core.Common.Caches; |
|
|
|
using Tiobon.Core.Common.Caches; |
|
|
|
using Tiobon.Core.IRepository.Base; |
|
|
|
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 readonly IBaseRepository<Ghre_CourseWare> _dal; |
|
|
|
private IGhre_CourseServices _ghre_CourseServices; |
|
|
|
private IGhre_CourseServices _ghre_CourseServices; |
|
|
|
|
|
|
|
private readonly IWebHostEnvironment _hostingEnvironment; |
|
|
|
private IGhre_CourseWareAttachmentServices _ghre_CourseWareAttachmentServices; |
|
|
|
private IGhre_CourseWareAttachmentServices _ghre_CourseWareAttachmentServices; |
|
|
|
|
|
|
|
|
|
|
|
public Ghre_CourseWareServices(ICaching caching, |
|
|
|
public Ghre_CourseWareServices(ICaching caching, |
|
|
|
IGhre_CourseServices ghre_CourseServices, |
|
|
|
IGhre_CourseServices ghre_CourseServices, |
|
|
|
|
|
|
|
IWebHostEnvironment hostingEnvironment, |
|
|
|
IGhre_CourseWareAttachmentServices ghre_CourseWareAttachmentServices, |
|
|
|
IGhre_CourseWareAttachmentServices ghre_CourseWareAttachmentServices, |
|
|
|
IBaseRepository<Ghre_CourseWare> dal) |
|
|
|
IBaseRepository<Ghre_CourseWare> dal) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -32,6 +36,7 @@ public class Ghre_CourseWareServices : BaseServices<Ghre_CourseWare, Ghre_Course |
|
|
|
base._caching = caching; |
|
|
|
base._caching = caching; |
|
|
|
_ghre_CourseServices = ghre_CourseServices; |
|
|
|
_ghre_CourseServices = ghre_CourseServices; |
|
|
|
_ghre_CourseWareAttachmentServices = ghre_CourseWareAttachmentServices; |
|
|
|
_ghre_CourseWareAttachmentServices = ghre_CourseWareAttachmentServices; |
|
|
|
|
|
|
|
_hostingEnvironment = hostingEnvironment; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override async Task<long> Add(InsertGhre_CourseWareInput entity) |
|
|
|
public override async Task<long> Add(InsertGhre_CourseWareInput entity) |
|
|
@ -136,4 +141,47 @@ public class Ghre_CourseWareServices : BaseServices<Ghre_CourseWare, Ghre_Course |
|
|
|
return result; |
|
|
|
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)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |