Compare commits
3 Commits
f0a5f2f878
...
a6c37463c5
| Author | SHA1 | Date |
|---|---|---|
|
|
a6c37463c5 | |
|
|
e212d76822 | |
|
|
86170747b2 |
|
|
@ -1,9 +1,15 @@
|
|||
using Infrastructure;
|
||||
using Infrastructure.Extensions;
|
||||
using Infrastructure.Helpers;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenAuth.App.BaseApp.Base;
|
||||
using OpenAuth.App.Interface;
|
||||
using OpenAuth.App.ServiceApp.GoView.Request;
|
||||
using OpenAuth.App.ServiceApp.GoView.Response;
|
||||
using OpenAuth.Repository;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using OpenAuth.Repository.Domain.GoView;
|
||||
using SqlSugar;
|
||||
|
||||
|
|
@ -11,9 +17,21 @@ namespace OpenAuth.App.ServiceApp.GoView;
|
|||
|
||||
public class GoViewProjectApp : SqlSugarBaseApp<GoviewProject, SugarDbContext>
|
||||
{
|
||||
public GoViewProjectApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<GoviewProject> repository,
|
||||
IAuth auth) : base(unitWork, repository, auth)
|
||||
private ILogger<FileApp> _logger;
|
||||
private string _filePath;
|
||||
private string _dbFilePath; //数据库中的文件路径
|
||||
private string _dbThumbnail; //数据库中的缩略图路径
|
||||
|
||||
public GoViewProjectApp(IOptions<AppSetting> setOptions, ISugarUnitOfWork<SugarDbContext> unitWork,
|
||||
ISimpleClient<GoviewProject> repository,
|
||||
IAuth auth, ILogger<FileApp> logger) : base(unitWork, repository, auth)
|
||||
{
|
||||
_logger = logger;
|
||||
_filePath = setOptions.Value.UploadPath;
|
||||
if (string.IsNullOrEmpty(_filePath))
|
||||
{
|
||||
_filePath = AppContext.BaseDirectory;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Response<bool>> DeleteProject(string ids)
|
||||
|
|
@ -33,7 +51,7 @@ public class GoViewProjectApp : SqlSugarBaseApp<GoviewProject, SugarDbContext>
|
|||
|
||||
var flag = await Repository.AsUpdateable(project).IgnoreNullColumns().ExecuteCommandAsync();
|
||||
return new Response<bool>
|
||||
{ Code = 200, Result = flag > 0, Message = flag > 0 ? "修改成功" : "修改失败" };
|
||||
{ Code = 200, Result = flag > 0, Message = flag > 0 ? "修改成功" : "修改失败" };
|
||||
}
|
||||
|
||||
public async Task<Response<GoviewProject>> Create(GoviewProject project)
|
||||
|
|
@ -45,7 +63,7 @@ public class GoViewProjectApp : SqlSugarBaseApp<GoviewProject, SugarDbContext>
|
|||
project.Id = Guid.NewGuid().ToString();
|
||||
var flag = await Repository.InsertAsync(project);
|
||||
return new Response<GoviewProject>
|
||||
{ Code = 200, Result = flag ? project : null, Message = flag ? "创建成功" : "创建失败" };
|
||||
{ Code = 200, Result = flag ? project : null, Message = flag ? "创建成功" : "创建失败" };
|
||||
}
|
||||
|
||||
public async Task<Response<bool>> Publish(GoViewProjectRequest req)
|
||||
|
|
@ -214,4 +232,146 @@ public class GoViewProjectApp : SqlSugarBaseApp<GoviewProject, SugarDbContext>
|
|||
return new Response<bool> { Code = 200, Result = flag, Message = flag ? "删除成功" : "删除失败" };
|
||||
}
|
||||
#endregion
|
||||
|
||||
public IList<SysUploadFile> Add(IFormFileCollection files)
|
||||
{
|
||||
if (!_auth.CheckLogin())
|
||||
{
|
||||
throw new Exception("必需登录才能上传附件");
|
||||
}
|
||||
|
||||
var result = new List<SysUploadFile>();
|
||||
foreach (var file in files)
|
||||
{
|
||||
result.Add(Add(file));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public SysUploadFile Add(IFormFile file)
|
||||
{
|
||||
if (file != null)
|
||||
{
|
||||
_logger.LogInformation("收到新文件: " + file.FileName);
|
||||
_logger.LogInformation("收到新文件: " + file.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("收到新文件为空");
|
||||
}
|
||||
|
||||
if (file != null && file.Length > 0)
|
||||
{
|
||||
using (var binaryReader = new BinaryReader(file.OpenReadStream()))
|
||||
{
|
||||
var fileName = Path.GetFileName(file.FileName);
|
||||
var data = binaryReader.ReadBytes((int)file.Length);
|
||||
SaveFile(fileName, data);
|
||||
var filedb = new SysUploadFile
|
||||
{
|
||||
Id = SnowFlakeSingle.instance.getID(),
|
||||
FilePath = _dbFilePath,
|
||||
Thumbnail = _dbThumbnail,
|
||||
FileName = fileName,
|
||||
FileSize = file.Length.ToInt(),
|
||||
CreateUserName = _auth.GetUserName(),
|
||||
FileType = Path.GetExtension(fileName),
|
||||
Extension = Path.GetExtension(fileName),
|
||||
CreateTime = DateTime.Now,
|
||||
};
|
||||
Repository.ChangeRepository<SugarRepositiry<SysUploadFile>>().Insert(filedb);
|
||||
var image = new GoviewImage
|
||||
{
|
||||
FileId = filedb.Id,
|
||||
};
|
||||
Repository.ChangeRepository<SugarRepositiry<GoviewImage>>().Insert(image);
|
||||
return filedb;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("文件过大");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 存储文件,如果是图片文件则生成缩略图
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="fileBuffers"></param>
|
||||
/// <exception cref="Exception"></exception>
|
||||
private void SaveFile(string fileName, byte[] fileBuffers)
|
||||
{
|
||||
string folder = DateTime.Now.ToString("yyyyMMdd");
|
||||
|
||||
//判断文件是否为空
|
||||
if (string.IsNullOrEmpty(fileName))
|
||||
{
|
||||
throw new Exception("文件名不能为空");
|
||||
}
|
||||
|
||||
//判断文件是否为空
|
||||
if (fileBuffers.Length < 1)
|
||||
{
|
||||
throw new Exception("文件不能为空");
|
||||
}
|
||||
|
||||
var uploadPath = Path.Combine(_filePath, folder);
|
||||
_logger.LogInformation("文件写入:" + uploadPath);
|
||||
if (!Directory.Exists(uploadPath))
|
||||
{
|
||||
Directory.CreateDirectory(uploadPath);
|
||||
}
|
||||
|
||||
var ext = Path.GetExtension(fileName).ToLower();
|
||||
string newName = GenerateId.GenerateOrderNumber() + ext;
|
||||
|
||||
using (var fs = new FileStream(Path.Combine(uploadPath, newName), FileMode.Create))
|
||||
{
|
||||
fs.Write(fileBuffers, 0, fileBuffers.Length);
|
||||
fs.Close();
|
||||
|
||||
//生成缩略图
|
||||
if (ext.Contains(".jpg") || ext.Contains(".jpeg") || ext.Contains(".png") || ext.Contains(".bmp") ||
|
||||
ext.Contains(".gif"))
|
||||
{
|
||||
string thumbnailName = GenerateId.GenerateOrderNumber() + ext;
|
||||
ImgHelper.MakeThumbnail(Path.Combine(uploadPath, newName), Path.Combine(uploadPath, thumbnailName));
|
||||
_dbThumbnail = Path.Combine(folder, thumbnailName);
|
||||
}
|
||||
|
||||
|
||||
_dbFilePath = Path.Combine(folder, newName);
|
||||
}
|
||||
}
|
||||
|
||||
public IList<SysUploadFile> GetImage(PageReq req)
|
||||
{
|
||||
var list = Repository.ChangeRepository<SugarRepositiry<SysUploadFile>>()
|
||||
.AsQueryable()
|
||||
.InnerJoin<GoviewImage>((a, b) => a.Id.Equals(b.FileId))
|
||||
.Select<SysUploadFile>()
|
||||
.ToList();
|
||||
return list;
|
||||
}
|
||||
|
||||
public bool DeleteImage(string ids)
|
||||
{
|
||||
if (string.IsNullOrEmpty(ids))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var idArray = ids.Split(",").Select(a => a.ToLong());
|
||||
// 向goviewimage中查询
|
||||
var list = Repository.ChangeRepository<SugarRepositiry<GoviewImage>>()
|
||||
.AsQueryable()
|
||||
.Where(a => idArray.Contains(a.FileId))
|
||||
.Select(a => a.FileId.ToString())
|
||||
.ToArray();
|
||||
Repository.ChangeRepository<SugarRepositiry<GoviewImage>>()
|
||||
.DeleteByIds(list);
|
||||
return Repository.ChangeRepository<SugarRepositiry<SysUploadFile>>()
|
||||
.DeleteByIds(list);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using SqlSugar;
|
||||
|
||||
namespace OpenAuth.Repository.Domain
|
||||
{
|
||||
///<summary>
|
||||
///即时消息表
|
||||
///</summary>
|
||||
[SugarTable("t_goview_image")]
|
||||
public partial class GoviewImage
|
||||
{
|
||||
public GoviewImage(){
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Desc:主键
|
||||
/// Default:
|
||||
/// Nullable:False
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey=true,ColumnName = "file_id")]
|
||||
public long FileId {get;set;}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using OpenAuth.App.BaseApp.Base;
|
||||
using OpenAuth.App.ServiceApp.GoView;
|
||||
using OpenAuth.App.ServiceApp.GoView.Request;
|
||||
using OpenAuth.App.ServiceApp.GoView.Response;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using OpenAuth.Repository.Domain.GoView;
|
||||
|
||||
namespace OpenAuth.WebApi.Controllers.ServiceControllers.GoView;
|
||||
|
|
@ -153,4 +155,54 @@ public class GoViewProjectController : ControllerBase
|
|||
return await _app.DeleteComponent(ids);
|
||||
}
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 图片上传接口
|
||||
/// </summary>
|
||||
/// <param name="files"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("image/upload")]
|
||||
[RequestSizeLimit(214748364800)]
|
||||
public Response<IList<SysUploadFile>> UploadImage(IFormFileCollection files)
|
||||
{
|
||||
var result = new Response<IList<SysUploadFile>>();
|
||||
try
|
||||
{
|
||||
result.Result = _app.Add(files);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Code = 500;
|
||||
result.Message = ex.Message;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取组件图片
|
||||
/// </summary>
|
||||
/// <param name="req"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("image/list")]
|
||||
public Response<IList<SysUploadFile>> GetImage([FromQuery] PageReq req )
|
||||
{
|
||||
|
||||
return new Response<IList<SysUploadFile>>()
|
||||
{
|
||||
Result = _app.GetImage(req)
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除图片
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("image/delete")]
|
||||
public Response<bool> DeleteImage(string ids)
|
||||
{
|
||||
return new Response<bool>()
|
||||
{
|
||||
Result = _app.DeleteImage(ids)
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue