Images are working but still there is need to add isImageMain flag to images.

This commit is contained in:
2025-04-28 14:17:38 +02:00
parent 6b5dded7f8
commit 7f8f13b115
12 changed files with 251 additions and 174 deletions

View File

@@ -1,38 +1,84 @@
package _11.asktpk.artisanconnectbackend.service;
import _11.asktpk.artisanconnectbackend.entities.Image;
import _11.asktpk.artisanconnectbackend.repository.ImageRepository;
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 org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@Service
public class ImageService {
private final ImageRepository imageRepository;
public byte[] getImageBytes(String imagePath) throws IOException {
Path path = Paths.get(imagePath);
return Files.readAllBytes(path);
ImageService(ImageRepository imageRepository) {
this.imageRepository = imageRepository;
}
public Resource getImageAsResource(String imagePath) throws IOException {
Path path = Paths.get(imagePath);
Resource resource = new UrlResource(path.toUri());
public String saveImageToStorage(String uploadDirectory, MultipartFile imageFile) throws IOException {
String uniqueFileName = UUID.randomUUID() + imageFile.getOriginalFilename();
if(resource.exists() && resource.isReadable()) {
return resource;
Path uploadPath = Path.of(uploadDirectory);
Path filePath = uploadPath.resolve(uniqueFileName);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath);
}
Files.copy(imageFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
return uniqueFileName;
}
public void addImageUrlToDB(String filename, Long noticeId) {
Image image = new Image();
image.setImageName(filename);
image.setNoticeId(noticeId);
imageRepository.save(image);
}
public Resource getImage(String imageDirectory, String imageName) throws IOException {
Path filePath = Paths.get(imageDirectory).resolve(imageName);
Resource resource = new UrlResource(filePath.toUri());
if(imageName.isEmpty() || imageDirectory.isEmpty()) {
throw new IOException("Filename or folder is empty. Please check your request and try again.");
}
if (!resource.exists()) {
throw new IOException("File not found");
}
return resource;
}
public String deleteImage(String imageDirectory, String imageName) throws IOException {
Path imagePath = Path.of(imageDirectory, imageName);
if (Files.exists(imagePath)) {
Files.delete(imagePath);
return "Success";
} else {
throw new IOException("Nie można odczytać obrazu: " + imagePath);
return "Failed"; // Handle missing images
}
}
public MediaType getMediaTypeForImage(String imagePath) {
return MediaTypeFactory
.getMediaType(imagePath)
.orElse(MediaType.APPLICATION_OCTET_STREAM);
public List<String> getImagesList(Long noticeID) throws Exception {
List<Image> images = imageRepository.findByNoticeId(noticeID);
if (images.isEmpty()) {
throw new Exception("There is no images for this notice");
}
return images.stream()
.map(Image::getImageName)
.collect(Collectors.toList());
}
}