private File CreateFile(IUnitOfWork unitOfWork, string fileName, byte[] data) { var fileRepository = unitOfWork.CreateEntityRepository <File>(); var file = new File { ContentType = JpegContentType, FileName = fileName }; file.Data = data; fileRepository.Create(file); return(file); }
public long CreateFile(IFormFile formFile) { if (formFile == null) { throw new ApiException(x => x.BadRequestResult( (ErrorCode.Parse(ErrorCodeType.PropertyDataNullOrEmpty, OctopostEntityName.File, PropertyName.File.Id), new ErrorDefinition(null, "Invalid file", PropertyName.File.Id)))); } using (var unitOfWork = this.unitOfWorkFactory.CreateUnitOfWork()) { var fileRepository = unitOfWork.CreateEntityRepository <File>(); var file = new File { ContentType = formFile.ContentType, FileName = formFile.FileName, Link = null }; using (var stream = formFile.OpenReadStream()) { using (var memoryStream = new MemoryStream()) { var buffer = new byte[16 * 1024]; var read = 0; while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { memoryStream.Write(buffer, 0, read); } file.Data = memoryStream.ToArray(); } } fileRepository.Create(file); unitOfWork.Save(); return(file.Id); } }