文件上传功能添加,注意使用Path.Combine处理通用路径问题(windows||docker)
This commit is contained in:
1 parent
7817c5374c
commit
244b59a3fb
135 files changed
+16937
-75
No files matched your search
@@ -0,0 +1,441 @@
|
||||
|
||||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using CloudBuilder.Core.DatabaseAccessor;
|
||||
using CloudBuilder.Master.Entity;
|
||||
using CloudBuilder.Master.Policy;
|
||||
using CloudBuilder.Master.SearchCriteria;
|
||||
using CloudBuilder.Master.Data;
|
||||
using CloudBuilder.Core.Service;
|
||||
using System.Diagnostics;
|
||||
using CloudBuilder.Security.Service;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using CloudBuilder.Security.Entity;
|
||||
using CloudBuilder.Security.Utility;
|
||||
using CloudBuilder.Security.Policy;
|
||||
using CloudBuilder.Core.Policy;
|
||||
namespace CloudBuilder.Master.Service
|
||||
{
|
||||
public class PublishPhotoService : IPublishPhotoService
|
||||
{
|
||||
private readonly IApplicationService service;
|
||||
|
||||
private IRepository<PublishPhotoEntity> repository;
|
||||
public PublishPhotoService(IApplicationService service)
|
||||
{
|
||||
this.service = service;
|
||||
repository = service.GetRepository<IRepository<PublishPhotoEntity>>();
|
||||
}
|
||||
|
||||
[PermissionOperate(name: MasterPermissionPolicy.PublishPhoto, operate: Operate.Find)]
|
||||
public PublishPhotoData FindByKey(string key)
|
||||
{
|
||||
PublishPhotoData data = new PublishPhotoData();
|
||||
if (string.IsNullOrEmpty(key)) return data;
|
||||
|
||||
PublishPhotoEntity publish = repository.DetachedEntities.Where(x => x.PublishName == key).FirstOrDefault();
|
||||
|
||||
if (publish == null) return data;
|
||||
|
||||
IRepository<PublishPhotoFamilyGroupEntity> repositoryPublishPhotoFamilyGroup = repository.GetRepository<IRepository<PublishPhotoFamilyGroupEntity>>();
|
||||
IRepository<FamilyGroupEntity> repositoryFamilyGroup = repository.GetRepository<IRepository<FamilyGroupEntity>>();
|
||||
|
||||
|
||||
IQueryable<PublishPhotoFamilyGroupDisplayData>? q = from e in repositoryPublishPhotoFamilyGroup.DetachedEntities
|
||||
join fg in repositoryFamilyGroup.DetachedEntities on e.FamilyName equals fg.FamilyName
|
||||
where e.PublishName == key
|
||||
select new PublishPhotoFamilyGroupDisplayData(fg)
|
||||
{
|
||||
CreatedBy = e.CreatedBy,
|
||||
CreatedDatetime = e.CreatedDatetime,
|
||||
UpdatedBy = e.UpdatedBy,
|
||||
UpdatedDatetime = e.UpdatedDatetime,
|
||||
PublishName = e.PublishName,
|
||||
};
|
||||
|
||||
data.FamilyGroups = q.ToArray();
|
||||
if (publish.OwnerName != service.UserId && !YesNoPolicy.IsYes(publish.OpenIndc) && data.FamilyGroups.Length == 0)
|
||||
{
|
||||
throw new ValidatedException("没有权限.");
|
||||
}
|
||||
else if (publish.OwnerName != service.UserId && !YesNoPolicy.IsYes(publish.OpenIndc) && data.FamilyGroups.Length > 0)
|
||||
{
|
||||
IRepository<FamilyGroupManagerEntity> repositoryFamilyGroupManager = repository.GetRepository<IRepository<FamilyGroupManagerEntity>>();
|
||||
|
||||
IQueryable<string>? fgm = from e in repositoryFamilyGroupManager.DetachedEntities
|
||||
where e.UserName == service.UserId && data.FamilyGroups.Where(x => x.FamilyName == e.FamilyName).Count() > 0
|
||||
select e.UserName;
|
||||
|
||||
if (fgm.Count() == 0)
|
||||
throw new ValidatedException("没有权限.");
|
||||
}
|
||||
|
||||
data.PublishPhoto = publish;
|
||||
|
||||
IRepository<PublishPhotoDetailEntity> repositoryPublishPhotoDetail = repository.GetRepository<IRepository<PublishPhotoDetailEntity>>();
|
||||
data.Details = repositoryPublishPhotoDetail.DetachedEntities.Where(x => x.PublishName == publish.PublishName).OrderBy(x => x.OrderBy).ToArray();
|
||||
|
||||
|
||||
bool isAttached = Debugger.IsAttached;
|
||||
if (data.Details != null || data.Details.Length > 0)
|
||||
foreach (var item in data.Details)
|
||||
{
|
||||
item.FileName = (isAttached ? service.Configuration[AppsettingsPolicy.ApplicationUrl] : "") + item.FileName;
|
||||
}
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
[PermissionOperate(name: MasterPermissionPolicy.PublishPhoto, operate: Operate.Find)]
|
||||
public PublishPhotoResultData FindBySearchCriteria(PublishPhotoSearchCriteria criteria)
|
||||
{
|
||||
PublishPhotoResultData data = new PublishPhotoResultData();
|
||||
PublishPhotoEntity[] list = null;
|
||||
|
||||
Int64 count = 0;
|
||||
|
||||
string sqlWhere = null;
|
||||
|
||||
if (criteria.Filter != null && criteria.Filter.Length > 0)
|
||||
{
|
||||
sqlWhere = SearchFilter.ComposeWhereClause(criteria.Filter);
|
||||
}
|
||||
|
||||
sqlWhere = sqlWhere + string.Format(" and ({0} ='{1}' or {2} ='{3}') ",
|
||||
PublishPhotoEntity.DB_OWNER_NAME, service.UserId,
|
||||
PublishPhotoEntity.DB_OPEN_INDC, criteria.IsModal ? service.UserId : YesNoPolicy.YES);
|
||||
|
||||
PublishPhotoEntity entity = PublishPhotoEntity.GetInstance(repository.GetDbContext().Database.ProviderName);
|
||||
string sql = entity.GetSelectTopFrom(criteria.limit, criteria.page, criteria.orderby.StringToDbField(), criteria.orderbytag, sqlWhere);
|
||||
|
||||
string err = null;
|
||||
try
|
||||
{
|
||||
list = repository.FromSql(sql).ToArray();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
err = ex.Message;
|
||||
}
|
||||
|
||||
|
||||
if (list != null && list.Length > 0)
|
||||
{
|
||||
sql = entity.GetSelectCountFrom(sqlWhere);
|
||||
count = Convert.ToInt64(repository.ExecuteScalar(sql));
|
||||
}
|
||||
|
||||
data.Limit = criteria.limit;
|
||||
data.Page = criteria.page;
|
||||
data.Publishs = list;
|
||||
data.Total = count;
|
||||
//data.Sql = sql;
|
||||
data.ErrorMessage = err;
|
||||
return data;
|
||||
}
|
||||
|
||||
[PermissionOperate(name: MasterPermissionPolicy.PublishPhoto, operate: Operate.Print)]
|
||||
public dynamic PrintBySearchCriteria(PublishPhotoSearchCriteria criteria)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[PermissionOperate(name: MasterPermissionPolicy.PublishPhoto, operate: Operate.Insert)]
|
||||
[UnitOfWork]
|
||||
public PublishPhotoData Insert(PublishPhotoData data)
|
||||
{
|
||||
PublishPhotoEntity publishPhoto = repository.DetachedEntities.Where(x => x.PublishLocalName == data.PublishPhoto.PublishLocalName).FirstOrDefault();
|
||||
if (publishPhoto != null)
|
||||
{
|
||||
throw new ValidatedException($"[{publishPhoto.PublishLocalName}]已发布.");
|
||||
}
|
||||
|
||||
Validate(data.PublishPhoto);
|
||||
|
||||
if (data.PublishPhoto.DefaultIndc == YesNoPolicy.YES)
|
||||
{
|
||||
CancelDefaultIndc(null);
|
||||
}
|
||||
|
||||
publishPhoto = new PublishPhotoEntity();
|
||||
ObjectCopyHelper.Copy(data.PublishPhoto, publishPhoto);
|
||||
|
||||
ISecurityGeneratorNumberService securityGeneratorNumberService = service.ServiceProvider.GetService<ISecurityGeneratorNumberService>()!;
|
||||
|
||||
publishPhoto.PublishName = securityGeneratorNumberService.GetNo(MasterPrefixPolicy.PHOTO_PUBLISH, MasterPrefixPolicy.PHOTO_PUBLISH_PUBLISH_NAME, "PP", YesNoPolicy.YES, "yymmdd00000");
|
||||
publishPhoto.OwnerName = service.UserId;
|
||||
|
||||
repository.InsertNow(publishPhoto);
|
||||
|
||||
SavePublishPhotoFamilyGroup(publishPhoto.PublishName, data.FamilyGroups);
|
||||
|
||||
return FindByKey(publishPhoto.PublishName);
|
||||
}
|
||||
|
||||
public void CancelDefaultIndc(string publishLocalName)
|
||||
{
|
||||
PublishPhotoEntity[] orgs = repository.DetachedEntities.Where(x => x.PublishName != publishLocalName && x.OwnerName == service.UserId && x.DefaultIndc == YesNoPolicy.YES).ToArray();
|
||||
foreach (PublishPhotoEntity org in orgs)
|
||||
{
|
||||
org.DefaultIndc = YesNoPolicy.NO;
|
||||
}
|
||||
|
||||
if (orgs != null && orgs.Length > 0)
|
||||
repository.UpdateNow(orgs);
|
||||
}
|
||||
|
||||
private void Validate(PublishPhotoEntity publishPhoto)
|
||||
{
|
||||
List<SysTableSchemaViewEntity> sysTableSchemas = service.ServiceProvider.GetRequiredService<ISysTableSchemaViewService>().FindByTableName(PublishPhotoEntity.DB_NAME_PUBLISH_PHOTO);
|
||||
ValidateHelper.ValidateString(publishPhoto, sysTableSchemas.ToArray());
|
||||
}
|
||||
|
||||
|
||||
[PermissionOperate(name: MasterPermissionPolicy.PublishPhoto, operate: Operate.Update)]
|
||||
[UnitOfWork]
|
||||
public PublishPhotoData Update(PublishPhotoData data)
|
||||
{
|
||||
//只有自己创建的才能修改
|
||||
PublishPhotoEntity publishEntity = repository.DetachedEntities.Where(x => x.OwnerName == service.UserId && x.PublishName == data.PublishPhoto.PublishName).FirstOrDefault();
|
||||
if (publishEntity == null)
|
||||
{
|
||||
throw new ValidatedException("记录不存在.");
|
||||
}
|
||||
|
||||
ValidateHelper.Validate(service, data.PublishPhoto, PublishPhotoEntity.DB_NAME_PUBLISH_PHOTO);
|
||||
|
||||
if (data.PublishPhoto.DefaultIndc == YesNoPolicy.YES)
|
||||
{
|
||||
CancelDefaultIndc(data.PublishPhoto.PublishName);
|
||||
}
|
||||
|
||||
publishEntity = new PublishPhotoEntity();
|
||||
ObjectCopyHelper.Copy(data.PublishPhoto, publishEntity);
|
||||
|
||||
repository.UpdateNow(publishEntity);
|
||||
|
||||
//只有删除和更新,没有插入
|
||||
SavePublishPhotoDetails(publishEntity.PublishName, data.Details);
|
||||
|
||||
SavePublishPhotoFamilyGroup(publishEntity.PublishName, data.FamilyGroups);
|
||||
|
||||
return FindByKey(publishEntity.PublishName);
|
||||
}
|
||||
|
||||
private void SavePublishPhotoFamilyGroup(string publishName, PublishPhotoFamilyGroupEntity[] familyGroups)
|
||||
{
|
||||
IRepository<PublishPhotoFamilyGroupEntity> repositoryPublishPhotoFamilyGroup = repository.GetRepository<IRepository<PublishPhotoFamilyGroupEntity>>();
|
||||
|
||||
PublishPhotoFamilyGroupEntity[] olds = repositoryPublishPhotoFamilyGroup.Entities.Where(x => x.PublishName == publishName).ToArray();
|
||||
if (olds != null && olds.Length > 0)
|
||||
{
|
||||
//存在更新,不存在删除
|
||||
|
||||
foreach (PublishPhotoFamilyGroupEntity old in olds)
|
||||
{
|
||||
if (familyGroups != null && familyGroups.Length > 0 && familyGroups.Where(x => x.FamilyName == old.FamilyName).Count() > 0)
|
||||
{
|
||||
//只有主键,无需更新
|
||||
//repositoryPpf.UpdateNow(old);
|
||||
}
|
||||
else
|
||||
{
|
||||
repositoryPublishPhotoFamilyGroup.DeleteNow(old);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (familyGroups != null && familyGroups.Length > 0)
|
||||
foreach (PublishPhotoFamilyGroupEntity dto in familyGroups)
|
||||
{
|
||||
if (string.IsNullOrEmpty(dto.CreatedBy))
|
||||
repositoryPublishPhotoFamilyGroup.InsertNow(new PublishPhotoFamilyGroupEntity()
|
||||
{
|
||||
PublishName = publishName,
|
||||
FamilyName = dto.FamilyName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void SavePublishPhotoDetails(string publishName, PublishPhotoDetailEntity[] details)
|
||||
{
|
||||
if (details != null && details.Length > 0)
|
||||
{
|
||||
int i = 1;
|
||||
foreach (PublishPhotoDetailEntity dtl in details)
|
||||
{
|
||||
dtl.OrderBy = i++;
|
||||
}
|
||||
}
|
||||
|
||||
IRepository<PublishPhotoDetailEntity> repositoryPublishPhotoDetail = repository.GetRepository<IRepository<PublishPhotoDetailEntity>>();
|
||||
|
||||
PublishPhotoDetailEntity[] olds = repositoryPublishPhotoDetail.Entities.Where(x => x.PublishName == publishName).ToArray();
|
||||
if (olds != null && olds.Length > 0)
|
||||
{
|
||||
//存在更新,不存在删除
|
||||
|
||||
foreach (PublishPhotoDetailEntity old in olds)
|
||||
{
|
||||
if (details != null && details.Length > 0 && details.Where(x => x.DetailId == old.DetailId).Count() > 0)
|
||||
{
|
||||
PublishPhotoDetailEntity dtl = details.Where(x => x.DetailId == old.DetailId).FirstOrDefault();
|
||||
|
||||
old.OrderBy = dtl.OrderBy;
|
||||
old.Notice = dtl.Notice;
|
||||
|
||||
repositoryPublishPhotoDetail.UpdateNow(old);
|
||||
}
|
||||
else
|
||||
{
|
||||
repositoryPublishPhotoDetail.DeleteNow(old);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[PermissionOperate(name: MasterPermissionPolicy.PublishPhoto, operate: Operate.Delete)]
|
||||
[UnitOfWork]
|
||||
public void Delete(string key)
|
||||
{
|
||||
PublishPhotoEntity publishEntity = repository.DetachedEntities.Where(x => x.OwnerName == service.UserId && x.PublishName == key).FirstOrDefault();
|
||||
if (publishEntity == null)
|
||||
{
|
||||
throw new ValidatedException("记录不存在.");
|
||||
}
|
||||
|
||||
repository.DeleteNow(publishEntity);
|
||||
|
||||
IRepository<PublishPhotoDetailEntity> repositoryDetail = repository.GetRepository<IRepository<PublishPhotoDetailEntity>>();
|
||||
|
||||
PublishPhotoDetailEntity[] detail = repositoryDetail.Entities.Where(x => x.PublishName == key).ToArray();
|
||||
if (detail != null && detail.Length > 0)
|
||||
{
|
||||
foreach (PublishPhotoDetailEntity dtl in detail)
|
||||
{
|
||||
repositoryDetail.DeleteNow(dtl);
|
||||
}
|
||||
}
|
||||
|
||||
IRepository<PublishPhotoFamilyGroupEntity> repositoryFamilyGroup = repository.GetRepository<IRepository<PublishPhotoFamilyGroupEntity>>();
|
||||
|
||||
PublishPhotoFamilyGroupEntity[] groups = repositoryFamilyGroup.Entities.Where(x => x.PublishName == key).ToArray();
|
||||
if (groups != null && groups.Length > 0)
|
||||
{
|
||||
foreach (PublishPhotoFamilyGroupEntity gp in groups)
|
||||
{
|
||||
repositoryFamilyGroup.DeleteNow(gp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
public PublishPhotoDetailEntity[] FindPublishPhotoDetailByPublishName(string publishName)
|
||||
{
|
||||
List<PublishPhotoDetailEntity> publishPhotoDetails = new List<PublishPhotoDetailEntity>();
|
||||
if (string.IsNullOrEmpty(publishName)) return publishPhotoDetails.ToArray();
|
||||
|
||||
IRepository<PublishPhotoEntity> repositoryPublishPhoto = repository.GetRepository<IRepository<PublishPhotoEntity>>();
|
||||
PublishPhotoEntity publishPhoto = repositoryPublishPhoto.DetachedEntities.Where(x => x.OwnerName == service.UserId && x.PublishName == publishName).FirstOrDefault();
|
||||
|
||||
if (publishPhoto == null)
|
||||
return publishPhotoDetails.ToArray();
|
||||
|
||||
IRepository<PublishPhotoDetailEntity> repositoryPublishPhotoDetail = repository.GetRepository<IRepository<PublishPhotoDetailEntity>>();
|
||||
PublishPhotoDetailEntity[] dtls = repositoryPublishPhotoDetail.DetachedEntities.Where(x => x.PublishName == publishName).OrderBy(x => x.OrderBy).ToArray();
|
||||
|
||||
if (dtls == null || dtls.Length == 0) return publishPhotoDetails.ToArray();
|
||||
|
||||
bool isAttached = Debugger.IsAttached;
|
||||
foreach (var item in dtls)
|
||||
{
|
||||
if (item.FileName.ToLower().StartsWith("http")) continue;
|
||||
|
||||
item.FileName = (isAttached ? service.Configuration[AppsettingsPolicy.ApplicationUrl] : "") + item.FileName;
|
||||
}
|
||||
|
||||
return dtls;
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
public PublishPhotoDetailEntity[] FindPublishPhotoDetail()
|
||||
{
|
||||
PublishPhotoEntity publishPhoto = repository.DetachedEntities.Where(x => x.OwnerName == service.UserId && x.DefaultIndc == YesNoPolicy.YES).FirstOrDefault();
|
||||
List<PublishPhotoDetailEntity> publishPhotoDetails = new List<PublishPhotoDetailEntity>();
|
||||
if (publishPhoto == null)
|
||||
return publishPhotoDetails.ToArray();
|
||||
|
||||
IRepository<PublishPhotoDetailEntity> repositoryPublishPhotoDetail = repository.GetRepository<IRepository<PublishPhotoDetailEntity>>();
|
||||
|
||||
PublishPhotoDetailEntity[] dtls = repositoryPublishPhotoDetail.DetachedEntities.Where(x => x.PublishName == publishPhoto.PublishName).OrderBy(x => x.OrderBy).ToArray();
|
||||
|
||||
if (dtls == null || dtls.Length == 0) return dtls;
|
||||
|
||||
bool isAttached = Debugger.IsAttached;
|
||||
foreach (var item in dtls)
|
||||
{
|
||||
if (item.FileName.ToLower().StartsWith("http")) continue;
|
||||
|
||||
item.FileName = (isAttached ? service.Configuration[AppsettingsPolicy.ApplicationUrl] : "") + item.FileName;
|
||||
}
|
||||
|
||||
return dtls;
|
||||
}
|
||||
|
||||
[PermissionOperate(name: MasterPermissionPolicy.PublishAudio, operate: Operate.Insert)]
|
||||
public void InsertPublishPhotoDetail(PublishData publishData)
|
||||
{
|
||||
IRepository<DmsFileEntity> repositoryDmsFile = repository.GetRepository<IRepository<DmsFileEntity>>();
|
||||
DmsFileEntity dmsFile = repositoryDmsFile.DetachedEntities.Where(x => x.FileId == publishData.FileId).FirstOrDefault();
|
||||
|
||||
if (dmsFile == null) return;
|
||||
if (!FileTypePolicy.IsPhoto(dmsFile.OrderKey)) return;
|
||||
|
||||
InsertPublishPhotoDetail(dmsFile, publishData.PublishName);
|
||||
}
|
||||
|
||||
[PermissionOperate(name: MasterPermissionPolicy.PublishAudio, operate: Operate.Insert)]
|
||||
public void InsertPublishPhotoDetailByFamilyGroup(PublishData publishData)
|
||||
{
|
||||
IRepository<FamilyDmsFileViewEntity> repositoryDmsFile = repository.GetRepository<IRepository<FamilyDmsFileViewEntity>>();
|
||||
FamilyDmsFileViewEntity dmsFile = repositoryDmsFile.DetachedEntities.Where(x => x.FileId == publishData.FileId).FirstOrDefault();
|
||||
|
||||
if (dmsFile == null) return;
|
||||
if (!(dmsFile.OrderKey.ToLower().Contains(".jpg") || dmsFile.OrderKey.ToLower().Contains(".png") ||
|
||||
dmsFile.OrderKey.ToLower().Contains(".gif") || dmsFile.OrderKey.ToLower().Contains(".jpeg"))) return;
|
||||
|
||||
DmsFileEntity dmsFileEntity = new DmsFileEntity();
|
||||
ObjectCopyHelper.Copy(dmsFile, dmsFileEntity);
|
||||
InsertPublishPhotoDetail(dmsFileEntity, publishData.PublishName);
|
||||
}
|
||||
|
||||
private void InsertPublishPhotoDetail(DmsFileEntity dmsFile, string publishName)
|
||||
{
|
||||
IRepository<PublishPhotoDetailEntity> repositoryPublishPhotoDetail = repository.GetRepository<IRepository<PublishPhotoDetailEntity>>();
|
||||
|
||||
var webRootPath = service.Configuration[ServiceSettingsPolicy.RequestPath];
|
||||
var phy = service.Configuration[ServiceSettingsPolicy.PhysicalFileDirectory];
|
||||
var scale = service.Configuration[ServiceSettingsPolicy.DmsFileScaleDirectory].Replace(phy, "");
|
||||
|
||||
var fileExtension = Path.GetExtension(dmsFile.OrderKey);
|
||||
var filePathRel = Path.Combine(scale, dmsFile.CreatedBy, fileExtension.Replace(".", ""), "");
|
||||
var filePath = filePathRel.Replace(Path.DirectorySeparatorChar, '/');
|
||||
string path = Path.Combine(webRootPath, filePathRel, dmsFile.OrderKey);
|
||||
|
||||
|
||||
PublishPhotoDetailEntity[] dtls = repositoryPublishPhotoDetail.DetachedEntities.Where(x => x.PublishName == publishName).ToArray();
|
||||
|
||||
if (dtls != null && dtls.Where(x => x.FileName == path).Count() > 0) return;
|
||||
|
||||
PublishPhotoDetailEntity dtl = new PublishPhotoDetailEntity();
|
||||
dtl.PublishName = publishName;
|
||||
dtl.OwnerName = dmsFile.OwerId;
|
||||
if (dtls != null && dtls.Length > 0)
|
||||
dtl.OrderBy = dtls.Sum(x => x.OrderBy) + 1;
|
||||
dtl.FileName = path;
|
||||
|
||||
repositoryPublishPhotoDetail.InsertNow(dtl);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user