Ejemplo n.º 1
0
 public async Task <ResultResponse <DebtorFileDto> > AddDebtorFile([FromForm] DebtorFileCreateDto dto)
 {
     if (await IsInRole(Roles.Admin))
     {
         if (dto != null && ModelState.IsValid)
         {
             if (dto.File == null)
             {
                 if (Request?.HasFormContentType == true && Request?.Form?.Files?.Count != 0)
                 {
                     dto.File = Request.Form.Files[0];
                 }
             }
             if (dto.File == null)
             {
                 return(ResultResponse <DebtorFileDto> .GetBadResponse(BussinesLogic.Models.StatusCode.BadRequest, "Необходимо передать файл"));
             }
             else
             {
                 return(await _debtorFilesService.AddDebtorFileAsync(dto, UserId.Value));
             }
         }
         return(ResultResponse <DebtorFileDto> .GetBadResponse(BussinesLogic.Models.StatusCode.BadRequest));
     }
     return(ResultResponse <DebtorFileDto> .GetBadResponse(BussinesLogic.Models.StatusCode.Forbidden));
 }
Ejemplo n.º 2
0
        public async Task <ResultResponse <DebtorFileDto> > AddDebtorFileAsync(DebtorFileCreateDto dto, int uploaderId)
        {
            try
            {
                var uploader = await _usersRepository.GetAsync(uploaderId);

                if (uploader == null)
                {
                    return(ResultResponse <DebtorFileDto> .GetBadResponse(StatusCode.NotFound, "Не найден пользователь"));
                }

                byte[] bytes = new byte[dto.File.Length];
                using (var stream = dto.File.OpenReadStream())
                {
                    await stream.ReadAsync(bytes, 0, (int)dto.File.Length);
                }
                var fileManager = new FileManager.Infrasructure.FileManager(
                    baseAddress: _configurationService.WebAppSettings.BaseAddress,
                    folder: _configurationService.UploadedFilesSettings.DebtorFilesFolderRelativePath,
                    progressLogger: _progressLogger);
                var uploadingResult = await fileManager.UploadFileAsync(new FileDto(dto.File.FileName, bytes),
                                                                        new string[] { "png", "jpg", "gif", "jpeg", "bmp" });

                if (uploadingResult.IsSuccess)
                {
                    var debtorFile = await _debtorFilesRepository.AddAsync(new DebtorFile
                    {
                        length      = (int)dto.File.Length,
                        name        = dto.Name,
                        native_name = dto.File.FileName,
                        path        = uploadingResult.FilePath,
                        uploaded    = DateTime.Now,
                        uploader_id = uploaderId,
                    });

                    if (debtorFile != null)
                    {
                        var documentDto = _mapper.Map <DebtorFileDto>(debtorFile);
                        return(ResultResponse <DebtorFileDto> .GetSuccessResponse(documentDto));
                    }
                    else
                    {
                        return(ResultResponse <DebtorFileDto> .GetInternalServerErrorResponse());
                    }
                }
                else
                {
                    return(ResultResponse <DebtorFileDto> .GetInternalServerErrorResponse(uploadingResult.ErrorDescription));
                }
            }
            catch (Exception ex)
            {
                _progressLogger.Error(ex, new { dto, uploaderId }, GetType().Name, nameof(AddDebtorFileAsync));
                return(ResultResponse <DebtorFileDto> .GetInternalServerErrorResponse());
            }
        }