Add file upload routes.

This commit is contained in:
2018-05-18 20:39:49 +02:00
parent c62de5d6ed
commit 5f54a8e592
6 changed files with 170 additions and 1 deletions

View File

@@ -10,13 +10,21 @@ import org.codiki.core.entities.dto.PasswordWrapperDTO;
import org.codiki.core.entities.dto.UserDTO;
import org.codiki.core.entities.persistence.User;
import org.codiki.core.security.TokenService;
import org.codiki.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/api/account")
@@ -82,4 +90,24 @@ public class AccountController {
pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
}
@PostMapping("/uploadAvatar")
public ResponseEntity<String> uploadAvatar(@RequestParam("file") MultipartFile pFile) {
String result;
try {
result = accountService.uploadFile(pFile);
return ResponseEntity.status(HttpStatus.OK).body(result);
} catch(final Exception pEx) {
result = StringUtils.concat("Fail to upload ", pFile.getOriginalFilename() + ".");
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(result);
}
}
@GetMapping("/loadAvatar/{avatarFileName}")
public ResponseEntity<Resource> loadAvatar(@PathVariable("avatarFileName") final String pAvatarFileName) {
final Resource avatarFile = accountService.loadAvatar(pAvatarFileName);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, StringUtils.concat("attachment; filename=\"", avatarFile.getFilename(), "\""))
.body(avatarFile);
}
}