Skip to content

Commit

Permalink
Merge pull request #13 from Patlenlix/issue-11-logger
Browse files Browse the repository at this point in the history
Issue 11 logger
  • Loading branch information
LordRekishi authored May 10, 2022
2 parents 2611112 + b2e35a5 commit 8a09b6f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/main/java/se/iths/imagestorage/beans/BeansConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package se.iths.imagestorage.beans;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeansConfig {

@Bean
public Logger getLogger(InjectionPoint ip) {
return LoggerFactory.getLogger(ip.getMember().getDeclaringClass());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package se.iths.imagestorage.repository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -15,24 +14,33 @@

@Repository
public class FileSystemRepository {
private final Logger log = LoggerFactory.getLogger(FileSystemRepository.class);
private final Logger log;

public FileSystemRepository(Logger log) {
this.log = log;
}

public void uploadImage(MultipartFile file, Image image){
Path path = Paths.get(image.getPath());

try {
log.info("Checking and creating directory...");
Files.createDirectories(path.getParent());
byte[] bytes = file.getBytes();
log.info("Attempting to upload file...");
Files.write(path, bytes);
log.info("Image successfully uploaded to: {}", path);
} catch (IOException e) {
log.error("Error: {0}");
log.error("Error: {1}", e);
}
}

public FileSystemResource findInFileSystem(String path){
try{
try {
log.info("Attempting to download file at: {}", path);
return new FileSystemResource(Paths.get(path));
}catch (InvalidPathException e){
} catch (InvalidPathException e) {
log.error("No file found at: {}", path);
throw new RuntimeException(e);
}
}
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/se/iths/imagestorage/service/ImageService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package se.iths.imagestorage.service;


import org.slf4j.Logger;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand All @@ -12,30 +13,52 @@
import se.iths.imagestorage.repository.ImageDbRepository;

import java.nio.file.Paths;
import java.time.LocalDateTime;

@Service
public class ImageService {

private final ImageDbRepository imageDbRepository;
private final FileSystemRepository fileSystemRepository;
private final Logger log;

public ImageService(ImageDbRepository imageDbRepository, FileSystemRepository fileSystemRepository) {
public ImageService(ImageDbRepository imageDbRepository, FileSystemRepository fileSystemRepository, Logger log) {
this.imageDbRepository = imageDbRepository;
this.fileSystemRepository = fileSystemRepository;
this.log = log;
}

public Long uploadImage(MultipartFile imageAsFile){
log.info("File upload started at: {}", LocalDateTime.now());

Image image = new Image();
image.setName(imageAsFile.getOriginalFilename());

image.setPath(setImagePath() + imageAsFile.getOriginalFilename());
log.info("Setting Image name...");
String imageName = imageAsFile.getOriginalFilename();
image.setName(imageName);
log.info("Image name set to: {}", imageName);

log.info("Setting Image path...");
String imagePath = setImagePath() + imageAsFile.getOriginalFilename();
image.setPath(imagePath);
log.info("Image path set to: {}", imagePath);

fileSystemRepository.uploadImage(imageAsFile,image);
return imageDbRepository.save(image).getId();
}

public FileSystemResource downloadImage(Long id){
log.info("File download started at: {}", LocalDateTime.now());

log.info("Attempting to find image with ID: {}", id);
Image image = imageDbRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
.orElseThrow(() -> {
log.error("No file with ID {} found", id);
return new ResponseStatusException(HttpStatus.NOT_FOUND);
});

log.info("Image with ID {} found", id);

return fileSystemRepository.findInFileSystem(image.getPath());
}
private String setImagePath(){
Expand Down

0 comments on commit 8a09b6f

Please sign in to comment.