refactor(image): extract shared SHA-256 hash utility

Move computeHash from ImageFetchService into reusable ImageHashUtil
to avoid duplication when other services need image hashing.
This commit is contained in:
Rodrigo Verdiani 2026-07-14 14:11:44 -03:00
parent cc4c8105ef
commit 9938d4649b
2 changed files with 28 additions and 20 deletions

View File

@ -4,12 +4,12 @@ import static java.util.Objects.nonNull;
import com.google.common.util.concurrent.RateLimiter; import com.google.common.util.concurrent.RateLimiter;
import com.magamochi.common.model.enumeration.ContentType; import com.magamochi.common.model.enumeration.ContentType;
import com.magamochi.image.util.ImageHashUtil;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.UUID; import java.util.UUID;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -54,7 +54,7 @@ public class ImageFetchService {
throws NoSuchAlgorithmException { throws NoSuchAlgorithmException {
var fileContentType = resolveContentType(httpResponse, imageBytes); var fileContentType = resolveContentType(httpResponse, imageBytes);
var fileHash = computeHash(imageBytes); var fileHash = ImageHashUtil.computeSha256(imageBytes);
return imageManagerService.upload( return imageManagerService.upload(
imageBytes, fileContentType, contentType.name().toLowerCase(), fileHash); imageBytes, fileContentType, contentType.name().toLowerCase(), fileHash);
@ -76,22 +76,4 @@ public class ImageFetchService {
return tika.detect(fileBytes); return tika.detect(fileBytes);
} }
private String computeHash(byte[] content) throws NoSuchAlgorithmException {
var digest = MessageDigest.getInstance("SHA-256");
var hashBytes = digest.digest(content);
var hexString = new StringBuilder(2 * hashBytes.length);
for (byte b : hashBytes) {
var hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
} }

View File

@ -0,0 +1,26 @@
package com.magamochi.image.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import lombok.NoArgsConstructor;
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
public class ImageHashUtil {
public static String computeSha256(byte[] content) throws NoSuchAlgorithmException {
var digest = MessageDigest.getInstance("SHA-256");
var hashBytes = digest.digest(content);
var hexString = new StringBuilder(2 * hashBytes.length);
for (byte b : hashBytes) {
var hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}