extract image delete to storage
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.petshop.backend.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
@@ -21,6 +23,8 @@ import java.util.UUID;
|
||||
@Service
|
||||
public class CatalogImageStorageService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(CatalogImageStorageService.class);
|
||||
|
||||
private static final String PET_PREFIX = "/uploads/pets/";
|
||||
private static final String PRODUCT_PREFIX = "/uploads/products/";
|
||||
private static final String BLOB_PETS = "pets";
|
||||
@@ -106,6 +110,24 @@ public class CatalogImageStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
public void deletePetImageIfPresent(String storedPath) {
|
||||
if (storedPath == null || storedPath.isBlank()) return;
|
||||
try {
|
||||
deletePetImage(storedPath);
|
||||
} catch (IOException | IllegalArgumentException e) {
|
||||
log.warn("Failed to delete pet image {}: {}", storedPath, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteProductImageIfPresent(String storedPath) {
|
||||
if (storedPath == null || storedPath.isBlank()) return;
|
||||
try {
|
||||
deleteProductImage(storedPath);
|
||||
} catch (IOException | IllegalArgumentException e) {
|
||||
log.warn("Failed to delete product image {}: {}", storedPath, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private String extractFilename(String storedPath, String prefix) {
|
||||
if (storedPath == null || !storedPath.startsWith(prefix)) throw new IllegalArgumentException("Image file was not found");
|
||||
String filename = storedPath.substring(prefix.length());
|
||||
|
||||
@@ -136,7 +136,7 @@ public class PetService {
|
||||
appt.setPet(null);
|
||||
}
|
||||
appointmentRepository.saveAll(linkedAppointments);
|
||||
deleteStoredImageIfPresent(pet.getImageUrl());
|
||||
catalogImageStorageService.deletePetImageIfPresent(pet.getImageUrl());
|
||||
petRepository.delete(pet);
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ public class PetService {
|
||||
public MyPetResponse uploadMyPetImage(Long ownerUserId, Long petId, MultipartFile file) throws IOException {
|
||||
validateImageFile(file);
|
||||
Pet pet = findOwnedPet(ownerUserId, petId);
|
||||
deleteStoredImageIfPresent(pet.getImageUrl());
|
||||
catalogImageStorageService.deletePetImageIfPresent(pet.getImageUrl());
|
||||
pet.setImageUrl(catalogImageStorageService.storePetImage(file));
|
||||
return mapToMyPetResponse(petRepository.save(pet));
|
||||
}
|
||||
@@ -185,13 +185,13 @@ public class PetService {
|
||||
public void deletePet(Long id) {
|
||||
Pet pet = petRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Pet not found with id: " + id));
|
||||
deleteStoredImageIfPresent(pet.getImageUrl());
|
||||
catalogImageStorageService.deletePetImageIfPresent(pet.getImageUrl());
|
||||
petRepository.delete(pet);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void bulkDeletePets(BulkDeleteRequest request) {
|
||||
petRepository.findAllById(request.getIds()).forEach(pet -> deleteStoredImageIfPresent(pet.getImageUrl()));
|
||||
petRepository.findAllById(request.getIds()).forEach(pet -> catalogImageStorageService.deletePetImageIfPresent(pet.getImageUrl()));
|
||||
petRepository.deleteAllById(request.getIds());
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ public class PetService {
|
||||
public PetResponse uploadPetImage(Long id, MultipartFile file) throws IOException {
|
||||
validateImageFile(file);
|
||||
Pet pet = findPet(id);
|
||||
deleteStoredImageIfPresent(pet.getImageUrl());
|
||||
catalogImageStorageService.deletePetImageIfPresent(pet.getImageUrl());
|
||||
pet.setImageUrl(catalogImageStorageService.storePetImage(file));
|
||||
return mapToResponse(petRepository.save(pet));
|
||||
}
|
||||
@@ -207,7 +207,7 @@ public class PetService {
|
||||
@Transactional
|
||||
public PetResponse deletePetImage(Long id) {
|
||||
Pet pet = findPet(id);
|
||||
deleteStoredImageIfPresent(pet.getImageUrl());
|
||||
catalogImageStorageService.deletePetImageIfPresent(pet.getImageUrl());
|
||||
pet.setImageUrl(null);
|
||||
return mapToResponse(petRepository.save(pet));
|
||||
}
|
||||
@@ -293,16 +293,6 @@ public class PetService {
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Pet not found with id: " + petId));
|
||||
}
|
||||
|
||||
private void deleteStoredImageIfPresent(String storedImagePath) {
|
||||
if (storedImagePath == null || storedImagePath.isBlank()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
catalogImageStorageService.deletePetImage(storedImagePath);
|
||||
} catch (IOException | IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeStatus(String status) {
|
||||
return status == null ? "" : status.trim();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user