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