예제 #1
0
 public ActionResult <Diary> UpdateDiary(long dairyId, Diary diary)
 {
     if (dairyId != diary.DiaryId)
     {
         return(BadRequest("The diary cannot be modified."));
     }
     try
     {
         _diaryService.Update(diary);
     }
     catch (Exception e)
     {
         string error = e.Message;
         if (e.InnerException != null)
         {
             error = e.InnerException.Message;
         }
         return(BadRequest(error));
     }
     return(NoContent());
 }
예제 #2
0
        public async Task <IActionResult> Upload(long diaryId)
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                ModelState.AddModelError("File",
                                         $"The request couldn't be processed (Error 1).");
                return(BadRequest(ModelState));
            }
            var fileName = "";
            var diary    = _diaryService.GetById(diaryId);

            if (diary == null)
            {
                return(BadRequest("The diary is not existed."));
            }
            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader  = new MultipartReader(boundary, HttpContext.Request.Body);
            var section = await reader.ReadNextSectionAsync();

            while (section != null)
            {
                var hasContentDispositionHeader =
                    ContentDispositionHeaderValue.TryParse(
                        section.ContentDisposition, out var contentDisposition);

                if (hasContentDispositionHeader)
                {
                    if (!MultipartRequestHelper
                        .HasFileContentDisposition(contentDisposition))
                    {
                        ModelState.AddModelError("File",
                                                 $"The request couldn't be processed (Error 2).");
                        return(BadRequest(ModelState));
                    }
                    else
                    {
                        fileName = Path.GetRandomFileName();
                        var streamedFileContent = await FileHelpers.ProcessStreamedFile(
                            section, contentDisposition, ModelState,
                            _permittedExtensions, _fileSizeLimit);

                        if (!ModelState.IsValid)
                        {
                            return(BadRequest(ModelState));
                        }

                        using (var targetStream = System.IO.File.Create(Path.Combine(_rootPath, fileName)))
                        {
                            await targetStream.WriteAsync(streamedFileContent);
                        }
                    }
                }
                // Drain any remaining section body that hasn't been consumed and
                // read the headers for the next section.
                diary.Photo = String.Join(" ", new string[] { diary.Photo, fileName });
                _diaryService.Update(diary);
                section = await reader.ReadNextSectionAsync();
            }
            return(Ok());
        }
 public async Task <ActionResponse <DiaryDto> > Update([FromBody] DiaryDto request)
 {
     return(await diaryService.Update(request));
 }