public async Task <ActionResult> Upload(IFormFile file, UploadType uploadType, int postId = 0) { var author = await _authorProvider.FindByEmail(User.Identity.Name); var post = postId == 0 ? new Post() : await _postProvider.GetPostById(postId); var path = $"{author.Id}/{DateTime.Now.Year}/{DateTime.Now.Month}"; var fileName = $"data/{path}/{file.FileName}"; if (uploadType == UploadType.PostImage) { fileName = Url.Content("~/") + fileName; } if (await _storageProvider.UploadFormFile(file, path)) { var blog = await _blogProvider.GetBlog(); switch (uploadType) { case UploadType.Avatar: author.Avatar = fileName; return((await _authorProvider.Update(author)) ? new JsonResult(fileName) : BadRequest()); case UploadType.AppLogo: blog.Logo = fileName; return((await _blogProvider.Update(blog)) ? new JsonResult(fileName) : BadRequest()); case UploadType.AppCover: blog.Cover = fileName; return((await _blogProvider.Update(blog)) ? new JsonResult(fileName) : BadRequest()); case UploadType.PostCover: post.Cover = fileName; return(new JsonResult(fileName)); case UploadType.PostImage: return(new JsonResult(fileName)); } return(Ok()); } else { return(BadRequest()); } }
public async Task <ActionResult> Upload([FromForm] IFormFile file, UploadType uploadType, int postId = 0) { var author = await _authorProvider.FindByEmail(User.Identity.Name); var post = postId == 0 ? new Post() : await _postProvider.GetPostById(postId); var path = Guid.NewGuid().ToString(); var fileName = path; if (await _storageProvider.UploadFormFile(file, path)) { var blog = await _blogProvider.GetBlog(); switch (uploadType) { case UploadType.Avatar: author.Avatar = fileName; return((await _authorProvider.Update(author)) ? new JsonResult(fileName) : BadRequest()); case UploadType.AppLogo: blog.Logo = fileName; return((await _blogProvider.Update(blog)) ? new JsonResult(fileName) : BadRequest()); case UploadType.AppCover: blog.Cover = fileName; return((await _blogProvider.Update(blog)) ? new JsonResult(fileName) : BadRequest()); case UploadType.PostCover: post.Cover = fileName; return(new JsonResult(fileName)); case UploadType.PostImage: return(new JsonResult(fileName)); } return(Ok()); } else { return(BadRequest("Fail when uploading file")); } }
public async Task <ActionResult <Author> > FindByEmail(string email) { return(await _authorProvider.FindByEmail(email)); }