WIP for images

This commit is contained in:
2025-04-24 07:34:53 +02:00
parent a3d3a01d3a
commit 6b5dded7f8
6 changed files with 119 additions and 35 deletions

View File

@@ -0,0 +1,38 @@
package _11.asktpk.artisanconnectbackend.service;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Service
public class ImageService {
public byte[] getImageBytes(String imagePath) throws IOException {
Path path = Paths.get(imagePath);
return Files.readAllBytes(path);
}
public Resource getImageAsResource(String imagePath) throws IOException {
Path path = Paths.get(imagePath);
Resource resource = new UrlResource(path.toUri());
if(resource.exists() && resource.isReadable()) {
return resource;
} else {
throw new IOException("Nie można odczytać obrazu: " + imagePath);
}
}
public MediaType getMediaTypeForImage(String imagePath) {
return MediaTypeFactory
.getMediaType(imagePath)
.orElse(MediaType.APPLICATION_OCTET_STREAM);
}
}