Beispiel #1
0
        public async Task <IResultModel <AttachmentUploadResultModel> > Upload(AttachmentUploadModel model, FileInfo fileInfo)
        {
            var result = new ResultModel <AttachmentUploadResultModel>();
            var entity = new AttachmentEntity
            {
                Module   = model.Module,
                Group    = model.Group,
                FileName = fileInfo.FileName,
                SaveName = fileInfo.SaveName,
                Ext      = fileInfo.Ext,
                Md5      = fileInfo.Md5,
                Path     = fileInfo.Path,
                FullPath = Path.Combine(fileInfo.Path, fileInfo.SaveName),
                Size     = fileInfo.Size.Size,
                SizeCn   = fileInfo.Size.ToString()
            };

            var mediaType = await _mediaTypeRepository.GetByExt(fileInfo.Ext);

            if (mediaType != null)
            {
                entity.MediaType = mediaType.Value;
            }

            using (var tran = _repository.BeginTransaction())
            {
                if (await _repository.AddAsync(entity))
                {
                    //如果需要授权访问附件,需要添加拥有者关联信息
                    if (!model.Auth || await _ownerRepository.AddAsync(new AttachmentOwnerEntity {
                        AttachmentId = entity.Id, AccountId = model.AccountId
                    }, tran))
                    {
                        tran.Commit();

                        var resultModel = _mapper.Map <AttachmentUploadResultModel>(entity);

                        return(result.Success(resultModel));
                    }
                }
            }

            return(result.Failed("上传失败"));
        }