예제 #1
0
        public async Task <IActionResult> Upload([FromForm] IFormCollection form)
        {
            var config      = _configProvider.Get <PathConfig>();
            var formFile    = form.Files.FirstOrDefault();
            var uploadModel = new FileUploadModel
            {
                Request  = Request,
                FormFile = formFile,
                RootPath = config.UploadPath,
                Module   = "Admin",
                Group    = Path.Combine("OSS", "Open"),
                SubPath  = Path.Combine("code", "file")
            };

            var fileUploadResult = await _fileUploadHelper.Upload(uploadModel);

            var fileInfo = new NetModular.Lib.Utils.Core.Files.FileInfo(formFile.FileName)
            {
                SaveName = formFile.FileName,
                Path     = "resource/"
            };
            var fileObj = new FileObject
            {
                PhysicalPath = Path.Combine(config.UploadPath, fileUploadResult.Data.FullPath),
                AccessMode   = FileAccessMode.Private,
                Group        = Path.Combine("OSS", "Open"),
                ModuleCode   = "Admin",
                FileInfo     = fileInfo
            };
            var fileStorageResult = _fileStorageProvider.Upload(fileObj);

            return(Ok(await Task.FromResult(fileStorageResult)));
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="formFile">文件</param>
        /// <param name="relativePath">相对目录</param>
        /// <param name="rootPath">根目录</param>
        /// <param name="cancellationToken">取消token</param>
        /// <returns></returns>
        private async Task <FileInfo> UploadSave(IFormFile formFile, string relativePath, string rootPath, CancellationToken cancellationToken = default)
        {
            var date = DateTime.Now;

            var name     = formFile.FileName;
            var size     = formFile.Length;
            var fileInfo = new FileInfo(name, size)
            {
                Path = Path.Combine(relativePath, date.ToString("yyyy"), date.ToString("MM"), date.ToString("dd")),
            };

            fileInfo.SaveName = $"{Guid.NewGuid().ToString().Replace("-", "")}.{fileInfo.Ext}";

            var fullDir = Path.Combine(rootPath, fileInfo.Path);

            if (!Directory.Exists(fullDir))
            {
                Directory.CreateDirectory(fullDir);
            }

            //写入
            var fullPath = Path.Combine(fullDir, fileInfo.SaveName);

            fileInfo.Md5 = await SaveWidthMd5(formFile, fullPath, cancellationToken);

            return(fileInfo);
        }
예제 #3
0
        public async Task <IResultModel> Add(FileUploadModel model, FileInfo fileInfo)
        {
            var entity = new FileEntity
            {
                ModuleCode = model.ModuleCode,
                Group      = model.Group,
                AccessMode = model.AccessMode,
                FileName   = fileInfo.FileName,
                SaveId     = fileInfo.SaveName.Split('.')[0],
                Path       = fileInfo.Path,
                FullPath   = fileInfo.FullPath,
                Ext        = fileInfo.Ext,
                Size       = fileInfo.Size.Size,
                SizeName   = fileInfo.Size.ToString(),
                Md5        = fileInfo.Md5
            };

            var mime = await _mimeRepository.Get(fileInfo.Ext.ToLower());

            if (mime != null)
            {
                entity.Mime = mime.Value;
            }

            using var uow = _dbContext.NewUnitOfWork();
            var result = await _repository.AddAsync(entity, uow);

            if (result)
            {
                #region ==绑定文件拥有者==

                if (model.AccessMode == FileAccessMode.Auth && model.Accounts != null && model.Accounts.Any())
                {
                    var owners = model.Accounts.Split(',')
                                 .Select(accountId => new FileOwnerEntity
                    {
                        SaveId    = entity.SaveId,
                        AccountId = Guid.Parse(accountId)
                    }).ToList();
                    await _ownerRepository.AddAsync(owners, uow);
                }

                #endregion

                #region ==OSS上传==

                var fileObject = new FileObject
                {
                    ModuleCode   = entity.ModuleCode,
                    Group        = entity.Group,
                    AccessMode   = entity.AccessMode,
                    PhysicalPath = model.PhysicalPath,
                    FileInfo     = fileInfo
                };

                await _fileStorageProvider.Upload(fileObject);

                entity.Url = fileInfo.Url;

                #endregion

                uow.Commit();

                return(ResultModel.Success(entity));
            }
            return(ResultModel.Failed());
        }
        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("上传失败"));
        }