public async Task <AttachmentMold> Post(SetAttachment request) { var session = Request.ThrowIfUnauthorized(); var file = Request.Files.FirstOrDefault(); if (file == null) { throw HttpError.BadRequest("Error: No file to download"); } var stream = file?.InputStream; var fileId = $"{pref}{Guid.NewGuid()}"; var fileUpload = new Attachment() { Id = fileId, FileName = file?.FileName, SenderId = session.UserAuthId, Hash = SaveFile(fileId.Replace(pref, string.Empty), stream), UploadDateTime = DateTimeOffset.Now, Size = stream.Length }; await RavenSession.StoreAsync(fileUpload); await RavenSession.SaveChangesAsync(); var mapped = Mapper.Map <AttachmentMold>(fileUpload); return(mapped); }
public async Task <EndVisitResponse> Post(EndVisit request) { var visit = await Db.SingleByIdAsync <VisitEntity>(request.Id); if (visit == null) { throw HttpError.NotFound(string.Format(ErrorMessage.VisitDoesNotExist, request.Id)); } if (visit.EndDateTime.HasValue) { throw HttpError.BadRequest(new ArgumentException(ErrorMessage.EndDateExists, nameof(request.VisitEndedDateTime)).Message); } if (!HasValidEndDate(visit.DateTime, request.VisitEndedDateTime)) { throw HttpError.BadRequest(new ArgumentException(ErrorMessage.MaxEndDateExceeded, nameof(request.VisitEndedDateTime)).Message); } await Db.UpdateAddAsync(() => new VisitEntity { EndDateTime = request.VisitEndedDateTime }, x => x.Id == request.Id); return(new EndVisitResponse()); }
public override void Execute(IRequest req, IResponse res, object requestDto) { if (Regex.IsMatch(req.PathInfo, "[0-9]")) { throw HttpError.BadRequest("No numbers"); } }
private void Validate(string operationName, string content, User user) { Validate(operationName, user); if (content.Length > Consts.PostContentLength) { throw HttpError.BadRequest("Content is too long"); } }
public override Nothing Get(Request r) { if (RepoAccount.ValidateEmail(r.ID, r.Code)) { return(Nothing); } throw HttpError.BadRequest("Sorry! Could not validate your email address..."); }
private async Task RegisterUserAsync(UserRegisterDto dto, CredentialLevel credentialLevel) { #region Validate if (!IsValidEmail(dto.Email)) { throw HttpError.BadRequest("Invalid email address"); } if (dto.Name == null) { throw HttpError.BadRequest("Username cannot be null"); } if (dto.Name.Length > Consts.UsernameMaxLength) { throw HttpError.BadRequest("Username is too long"); } if (dto.Description != null && dto.Description.Length > Consts.DescriptionMaxLength) { throw HttpError.BadRequest("Description is too long"); } #endregion #region CheckIfUserExists if ((await _userRepository.Find(x => x.Name == dto.Name)).Any()) { throw HttpError.BadRequest($"There is user with {dto.Name} username"); } if ((await _userRepository.Find(x => x.Email == dto.Email)).Any()) { throw HttpError.BadRequest($"There is user with {dto.Email} email address"); } #endregion var user = new User { Id = Guid.NewGuid().ToString(), CredentialLevel = credentialLevel, Description = dto.Description, Email = dto.Email, Name = dto.Name }; await _userRepository.Create(user); await _hashRepository.CreateOrUpdate(new Hash { Id = user.Id, PasswordHash = HashHelpers.CreateHash(dto.Password) }); }
public async Task <Stream> Get(GetAttachment request) { var filePath = Path.Combine(dirPatch, request.Id); if (!File.Exists(filePath)) { throw HttpError.BadRequest("Error: file does not exist"); } return(File.OpenRead(filePath)); }
public UpdateCarResponse Put(UpdateCar request) { var car = _carService.Get(request.Id); if (car == null) { throw HttpError.BadRequest($"Invalid CarId"); } _carService.Update(request.Id, request.ConvertTo <Car>()); return(new UpdateCarResponse { Car = request.ConvertTo <CarViewModel>() }); }
public async Task <Response> Create(string postId, string commentId, CreateResponseDto dto) { User user = await _sessionService.GetUser(); Validate("create", dto.Content, user); Comment comment = await _commentRepository.GetById(commentId); if (comment == null) { _logger.LogWarning($"There is no comment {postId}"); throw HttpError.NotFound($"There is no comment {postId}"); } if (comment.PostId != postId) { _logger.LogWarning($"Comment {commentId} does not belong to post {postId}"); throw HttpError.BadRequest("Comment does not belong to post"); } var response = new Response { Content = dto.Content, CreationTime = DateTime.Now, LastUpdateTime = DateTime.Now, AuthorId = user.Id, Likes = 0, Dislikes = 0, CommentId = commentId, Id = Guid.NewGuid().ToString() }; await _responseRepository.Create(response); _logger.LogInformation($"Response {response.Id} has been created"); return(response); }
public async Task <object> Any(StoreFileUpload request) { var feature = AssertPlugin <FilesUploadFeature>(); var location = feature.AssertLocation(request.Name, Request); if (Request.Files.Length == 0) { throw HttpError.BadRequest("No files uploaded"); } var session = await Request.GetSessionAsync(); var results = new List <string>(); foreach (var file in Request.Files) { var path = await feature.UploadFileAsync(location, Request, session, file).ConfigAwait(); results.Add(path); } return(new StoreFileUploadResponse { Results = results, }); }
private string SaveFile(string fileId, Stream stream) { var filePath = Path.Combine(dirPatch, fileId); try { if (!Directory.Exists(dirPatch)) { Directory.CreateDirectory(dirPatch); } } catch { throw HttpError.BadRequest("Error on save file"); } using (var fileStream = File.Create(filePath, (int)stream.Length)) { const int bufferSize = 4194304; var data = new byte[bufferSize]; stream.Seek(0, SeekOrigin.Begin); var hashString = string.Empty; while (stream.Position < stream.Length) { var read = stream.Read(data, 0, bufferSize); fileStream.Write(data, 0, read); hashString += data.ToMd5Hash(); } fileStream.Flush(); return(hashString); } }