Archived
Add logging configuration.
This commit is contained in:
@@ -6,6 +6,8 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.apache.commons.lang.RandomStringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
@@ -14,14 +16,16 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Service
|
||||
public class FileUploadService {
|
||||
/** Logger. */
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FileUploadService.class);
|
||||
/** Length of uploaded file names. */
|
||||
private static final int DESTINATION_IMAGE_NAME_LENGTH = 30;
|
||||
|
||||
@Value("codiki.files.upload")
|
||||
@Value("${codiki.files.upload}")
|
||||
private String folderUpload;
|
||||
@Value("codiki.files.profile-images")
|
||||
@Value("${codiki.files.profile-images}")
|
||||
private String folderProfileImages;
|
||||
@Value("codiki.files.images")
|
||||
@Value("${codiki.files.images}")
|
||||
private String folderImages;
|
||||
|
||||
/**
|
||||
@@ -46,9 +50,12 @@ public class FileUploadService {
|
||||
String result;
|
||||
try {
|
||||
result = buildDestinationFileName();
|
||||
Files.copy(pFile.getInputStream(), Paths.get(pPath).resolve(result));
|
||||
final Path destinationFile = Paths.get(pPath).resolve(result);
|
||||
LOG.debug("Upload file saved as {}", destinationFile.toString());
|
||||
Files.copy(pFile.getInputStream(), destinationFile);
|
||||
return result;
|
||||
} catch (final Exception pEx) {
|
||||
LOG.error("Error during file upload.", pEx);
|
||||
// TODO : Refactor exception
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.codiki.core.entities.dto.ImageDTO;
|
||||
import org.codiki.core.utils.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -25,6 +27,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
@RestController
|
||||
@RequestMapping("/api/images")
|
||||
public class ImageController {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ImageController.class);
|
||||
|
||||
@Autowired
|
||||
private ImageService imageService;
|
||||
@@ -32,11 +35,13 @@ public class ImageController {
|
||||
@PostMapping("/uploadAvatar")
|
||||
public ResponseEntity<String> uploadAvatar(@RequestParam("file") MultipartFile pFile,
|
||||
final HttpServletRequest pRequest, final HttpServletResponse pResponse, final Principal pPrincipal) {
|
||||
LOG.debug("Upload avatar.");
|
||||
ResponseEntity<String> result;
|
||||
try {
|
||||
result = ResponseEntity.status(HttpStatus.OK)
|
||||
.body(imageService.uploadAvatar(pFile, pRequest, pResponse, pPrincipal));
|
||||
} catch(final Exception pEx) {
|
||||
LOG.error("Error during avatar upload.", pEx);
|
||||
result = ResponseEntity.status(HttpStatus.EXPECTATION_FAILED)
|
||||
.body(StringUtils.concat("Fail to upload ", pFile.getOriginalFilename() + "."));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user