Klasse ImageService
java.lang.Object
com.spaghetticodegang.trylater.image.ImageService
Service layer for handling business logic related to image upload and deletion.
This service provides methods for uploading, scaling, and deleting images,
as well as creating response DTOs for image uploads.
-
Konstruktorübersicht
Konstruktoren -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungCreates anImageUploadResponseDto
from a givenImage
entity.boolean
deleteImageByImgPath
(String imgPath) Deletes an image file from the file system based on its unique ID.uploadImage
(org.springframework.web.multipart.MultipartFile imageFile) Uploads a single image file.
-
Konstruktordetails
-
ImageService
public ImageService()
-
-
Methodendetails
-
createImageUploadResponseDto
Creates anImageUploadResponseDto
from a givenImage
entity. This DTO contains essential information about the uploaded image, such as its unique ID and the full path where it is stored.- Parameter:
image
- TheImage
entity representing the uploaded image. Must not benull
.- Gibt zurück:
- An
ImageUploadResponseDto
containing the image ID and its storage path.
-
uploadImage
public ImageUploadResponseDto uploadImage(org.springframework.web.multipart.MultipartFile imageFile) Uploads a single image file. This method performs the following steps:- Validates the provided
MultipartFile
to ensure it is not empty and has a supported image format (png, jpeg, jpg, webp). - Generates a unique filename for the image using UUID and the original file extension.
- Creates the necessary directory structure on the server if it doesn't exist.
- Transfers the uploaded image file to the designated storage location.
- Creates a new
Image
entity with the generated filename and saves it to the database using theImageRepository
. - Returns an
ImageUploadResponseDto
containing the ID and path of the newly uploaded image.
- Parameter:
imageFile
- TheMultipartFile
representing the image to be uploaded. Must not benull
or empty.- Gibt zurück:
- An
ImageUploadResponseDto
with the ID and path of the uploaded image. - Löst aus:
ImageHandleException
- If the provided file is empty, has an unsupported format, or if anIOException
occurs during file handling.
- Validates the provided
-
deleteImageByImgPath
Deletes an image file from the file system based on its unique ID. This method attempts to delete the file located at the path constructed using the configured upload directory and the providedimgPath
.- Parameter:
imgPath
- The unique identifier of the image to be deleted. This should match the filename (including extension) of the image file.- Gibt zurück:
true
if the image file was successfully deleted;false
if the file does not exist.- Löst aus:
ImageHandleException
- If anIOException
occurs during the deletion process.
-