Ejemplo n.º 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 = model.Name.NotNull() ? model.Name : 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 uow = _dbContext.NewUnitOfWork())
            {
                if (await _repository.AddAsync(entity))
                {
                    //如果需要授权访问附件,需要添加拥有者关联信息
                    var ownerEntity = new AttachmentOwnerEntity
                    {
                        AttachmentId = entity.Id,
                        AccountId    = model.AccountId
                    };
                    if (!model.Auth || await _ownerRepository.AddAsync(ownerEntity, uow))
                    {
                        uow.Commit();

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

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

            return(result.Failed("上传失败"));
        }
Ejemplo n.º 2
0
        public async Task <IResultModel> Upload(AttachmentUploadModel model, HttpRequest request)
        {
            var uploadModel = new FileUploadModel
            {
                Request  = request,
                FormFile = model.File,
                RootPath = _moduleCommonOptions.UploadPath,
                Module   = "Common",
                Group    = Path.Combine("Attachment", model.Module, model.Group)
            };

            //附件存储路径/Common/Attachment/{所属模块名称}/{所属分组模块}

            var result = await _fileUploadHelper.Upload(uploadModel);

            if (result.Successful)
            {
                var file   = result.Data;
                var entity = new AttachmentEntity
                {
                    Module   = model.Module,
                    Group    = model.Group,
                    FileName = file.FileName,
                    SaveName = file.SaveName,
                    Ext      = file.Ext,
                    Md5      = file.Md5,
                    Path     = file.Path,
                    FullPath = Path.Combine(file.Path, file.SaveName),
                    Size     = file.Size.Size,
                    SizeCn   = file.Size.ToString()
                };

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

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

                _uow.BeginTransaction();

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

                        var resultModel = _mapper.Map <AttachmentUploadResultModel>(entity);
                        var url         = request.GetHost($"/common/attachment/download/{entity.Id}");
                        resultModel.Url = new Uri(url).ToString();

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

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