-
Notifications
You must be signed in to change notification settings - Fork 1
Cloudinary File Upload Integration #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8424bc1
refactor: remove FileStorageService and its associated tests
Iyedchaabane 76a1125
feat: add Cloudinary dependency for cloud storage integration
Iyedchaabane a6544bd
feat: add Cloudinary configuration for file upload integration
Iyedchaabane 420dc76
feat: implement CloudinaryService for file upload and deletion
Iyedchaabane 99eab97
feat: integrate CloudinaryService for book cover upload and deletion
Iyedchaabane 98725d1
feat: update BookResponse cover field type to String and adjust BookMβ¦
Iyedchaabane 416a07b
feat: enhance GlobalExceptionHandler with logging for unexpected errors
Iyedchaabane ca94665
feat: add Cloudinary configuration for file storage in production
Iyedchaabane 26e3ed6
feat: update application-dev.yml for Cloudinary file storage configurβ¦
Iyedchaabane 7481a21
feat: add comprehensive tests for CloudinaryService including upload β¦
Iyedchaabane 6e9179b
feat: refactor BookServiceTest to use CloudinaryService for file uploads
Iyedchaabane 3e0b63b
feat: update version to 1.1.0 in package.json and pom.xml
Iyedchaabane 870ddf8
fix: specify generic type for uploadResult and remove unnecessary varβ¦
Iyedchaabane 28ad57d
feat: update Cloudinary dependencies and improve cover upload logic iβ¦
Iyedchaabane 298d5d2
refactor: simplify public ID generation in uploadFile method
Iyedchaabane f58ed1f
test: remove redundant original filename mock in CloudinaryServiceTest
Iyedchaabane ea2a262
feat: enhance version token handling in Cloudinary file upload logic
Iyedchaabane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...twork/src/main/java/com/ichaabane/book_network/application/service/CloudinaryService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package com.ichaabane.book_network.application.service; | ||
|
|
||
| import com.cloudinary.Cloudinary; | ||
| import com.cloudinary.utils.ObjectUtils; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| @Service | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public class CloudinaryService { | ||
|
|
||
| private final Cloudinary cloudinary; | ||
|
|
||
| /** | ||
| * Upload file to Cloudinary | ||
| * | ||
| * @param file The file to upload | ||
| * @param folder The folder name in Cloudinary (e.g., "book-covers") | ||
| * @return The secure URL of the uploaded file | ||
| */ | ||
| public String uploadFile(MultipartFile file, String folder) { | ||
| try { | ||
| // Generate unique public ID | ||
| String publicId = generatePublicId(); | ||
|
|
||
| // Upload to Cloudinary | ||
| Map<String, Object> uploadResult = cloudinary.uploader().upload( | ||
| file.getBytes(), | ||
| ObjectUtils.asMap( | ||
| "folder", folder, | ||
| "public_id", publicId, | ||
| "resource_type", "auto", | ||
| "transformation", new com.cloudinary.Transformation() | ||
| .width(500) | ||
| .height(700) | ||
| .crop("limit") | ||
| .quality("auto:good") | ||
| .fetchFormat("auto") | ||
| ) | ||
| ); | ||
|
|
||
| String secureUrl = (String) uploadResult.get("secure_url"); | ||
| log.info("File uploaded successfully to Cloudinary: {}", secureUrl); | ||
| return secureUrl; | ||
|
|
||
| } catch (IOException e) { | ||
| log.error("Failed to upload file to Cloudinary: {}", e.getMessage()); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Upload file for a specific user | ||
| * | ||
| * @param file The file to upload | ||
| * @param userId The user ID | ||
| * @return The secure URL of the uploaded file | ||
| */ | ||
| public String uploadUserFile( | ||
| MultipartFile file, | ||
| Integer userId) { | ||
| String folder = "book-network/users/" + userId; | ||
| return uploadFile(file, folder); | ||
| } | ||
|
|
||
| /** | ||
| * Delete file from Cloudinary | ||
| * | ||
| * @param imageUrl The full URL of the image to delete | ||
| */ | ||
| public void deleteFile(String imageUrl) { | ||
| if (imageUrl == null || imageUrl.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // Extract public ID from URL | ||
| String publicId = extractPublicId(imageUrl); | ||
| if (publicId != null) { | ||
| cloudinary.uploader().destroy(publicId, ObjectUtils.emptyMap()); | ||
| log.info("File deleted from Cloudinary: {}", publicId); | ||
| } | ||
| } catch (IOException e) { | ||
| log.error("Failed to delete file from Cloudinary: {}", e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generate unique public ID for file | ||
| * Note: No extension is appended as Cloudinary handles file types automatically with "resource_type":"auto" | ||
| */ | ||
| private String generatePublicId() { | ||
| return UUID.randomUUID().toString() + "-" + System.currentTimeMillis(); | ||
| } | ||
|
|
||
| /** | ||
| * Extract public ID from Cloudinary URL | ||
| * Example URL: https://res.cloudinary.com/demo/image/upload/v1234567890/folder/file.jpg | ||
| * Public ID: folder/file | ||
| */ | ||
| private String extractPublicId(String imageUrl) { | ||
| try { | ||
| // Split by "upload/" | ||
| String[] parts = imageUrl.split("/upload/"); | ||
| if (parts.length < 2) { | ||
| return null; | ||
| } | ||
|
|
||
| // Get the part after "upload/" | ||
| String afterUpload = parts[1]; | ||
|
|
||
| // Find version token pattern: v followed by digits (e.g., v1234567890) | ||
| java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("v\\d+"); | ||
| java.util.regex.Matcher matcher = pattern.matcher(afterUpload); | ||
|
|
||
| if (matcher.find()) { | ||
| // Extract everything after the version token | ||
| int versionEnd = matcher.end(); | ||
| if (versionEnd < afterUpload.length()) { | ||
| afterUpload = afterUpload.substring(versionEnd); | ||
|
|
||
| // Strip leading "/" | ||
| if (afterUpload.startsWith("/")) { | ||
| afterUpload = afterUpload.substring(1); | ||
| } | ||
| } else { | ||
| return null; // No content after version token | ||
| } | ||
| } else { | ||
| return null; // No version token found | ||
| } | ||
|
|
||
| // Remove file extension | ||
| int lastDot = afterUpload.lastIndexOf("."); | ||
| if (lastDot > 0) { | ||
| afterUpload = afterUpload.substring(0, lastDot); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| return afterUpload; | ||
| } catch (Exception e) { | ||
| log.error("Failed to extract public ID from URL: {}", imageUrl); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
72 changes: 0 additions & 72 deletions
72
...work/src/main/java/com/ichaabane/book_network/application/service/FileStorageService.java
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
...work/src/main/java/com/ichaabane/book_network/infrastructure/config/CloudinaryConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.ichaabane.book_network.infrastructure.config; | ||
|
|
||
| import com.cloudinary.Cloudinary; | ||
| import com.cloudinary.utils.ObjectUtils; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class CloudinaryConfig { | ||
|
|
||
| @Value("${cloudinary.cloud-name}") | ||
| private String cloudName; | ||
|
|
||
| @Value("${cloudinary.api-key}") | ||
| private String apiKey; | ||
|
|
||
| @Value("${cloudinary.api-secret}") | ||
| private String apiSecret; | ||
|
|
||
| @Bean | ||
| public Cloudinary cloudinary() { | ||
| return new Cloudinary(ObjectUtils.asMap( | ||
| "cloud_name", cloudName, | ||
| "api_key", apiKey, | ||
| "api_secret", apiSecret, | ||
| "secure", true | ||
| )); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π§© Analysis chain
π Web query:
Spring Boot 3.5.3 BOM dependency management cloudinary commons-lang3 httpclient versionsπ‘ Result:
For Spring Boot
3.5.3(org.springframework.boot:spring-boot-dependencies:3.5.3) the BOM manages:org.apache.commons:commons-lang3):3.17.0[1]org.apache.httpcomponents.client5:httpclient5):5.5[1]Cloudinary: there is no Cloudinary (
com.cloudinary:*) entry/version managed by the Spring Boot 3.5.3 BOM (nocloudinaryproperty and no managed dependency coordinates for it), so you must specify Cloudinaryβs version yourself (or use Cloudinaryβs own BOM, if they provide one). [1]Sources
[1] Maven Central (Sonatype) β
spring-boot-dependencies:3.5.3POM/properties and managed dependencies list.π Web query:
CVE cloudinary-http44 1.39.0 security vulnerabilitiesπ‘ Result:
For
com.cloudinary:cloudinary-http44:1.39.0, there donβt appear to be direct CVEs filed against the Cloudinary artifact itself, but it pulls in vulnerable transitive dependencies. [5][1]CVEs impacting
cloudinary-http441.39.0 via dependenciescloudinary-http44:1.39.0depends onorg.apache.httpcomponents:httpclient:4.4, and HttpClient versions < 4.5.13 are affected (wrong target host selection via malformed URI authority). Mitigation: upgrade HttpClient to β₯ 4.5.13. [1][4]cloudinary-http44:1.39.0depends onorg.apache.commons:commons-lang3:3.1, and Commons Lang 3 versions < 3.18.0 are affected (uncontrolled recursion/StackOverflowError DoS). Mitigation: upgrade commons-lang3 to β₯ 3.18.0. [1][3]What to do
cloudinary-http44, use Maven/Gradle dependency overrides to pin:org.apache.httpcomponents:httpclientto 4.5.13+org.apache.commons:commons-lang3to 3.18.0+ (or newer)then run tests to ensure compatibility. [1][3][4]
π Web query:
CVE commons-lang3 3.18.0 Apache Commons Lang security issuesπ‘ Result:
For Apache Commons Lang (
org.apache.commons:commons-lang3), the main published security issue relevant to 3.18.0 is:ClassUtils.getClass(...), very long crafted inputs can trigger aStackOverflowError, potentially causing an application crash/denial of service. Affected versions:commons-lang33.0 up to (but not including) 3.18.0. Fixed in: 3.18.0. [1][2]So, Commons Lang 3.18.0 is the patched (non-vulnerable) version for CVE-2025-48924, and upgrading to 3.18.0 or later is the recommended remediation. [1][3]
Sources:
[1] NVD CVE record (CVE-2025-48924)
[2] GitHub Advisory (GHSA-j288-q9x7-2f5v / CVE-2025-48924)
[3] SUSE security update noting update to 3.18.0 to fix CVE-2025-48924
π Web query:
CVE httpclient 4.5.13 Apache HttpComponents vulnerabilities 2024 2025π‘ Result:
For Apache HttpComponents HttpClient 4.5.13 (the 4.x line), I could not find any new CVEs from 2024 or 2025 that list 4.5.13 as an affected version.
What you will commonly see in 2024/2025 scanners is older items:
A 2025 CVE people sometimes confuse with 4.x is:
If you want the safest 4.x baseline anyway, the latest 4.5 line commonly recommended by vulnerability databases is 4.5.14. [5]
Sources: [1] [2] [3] [4] [5]
Version-convergence conflicts confirmed; explicit pins are necessary for CVE mitigation but require integration testing.
The pinned versions mitigate real CVEs in Cloudinary's transitive dependencies:
commons-lang3 3.18.0patches CVE-2025-48924 (DoS via uncontrolled recursion)httpclient 4.5.13patches CVE-2020-13956 (malformed URI handling)However, these pins diverge from Spring Boot 3.5.3 BOM expectations:
commons-lang3 3.17.0(PR pins 3.18.0βnewer, acceptable but needs testing)httpclient5 5.5(PR pinshttpclient 4.xβdifferent artifact line due to Cloudinary's dependency)Confirm your build and integration tests validate cross-module compatibility with the 4.x HttpClient line alongside Spring Boot 3.5.3's 5.x expectations.
π€ Prompt for AI Agents