Files
no-copy/src/main/java/ru/soune/nocopy/service/ImageHashService.java
T

49 lines
1.6 KiB
Java
Raw Normal View History

package ru.soune.nocopy.service;
import com.vrt.fileprotection.image.phash.PHash;
import com.vrt.fileprotection.image.phash.PerceptualHashHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ru.soune.nocopy.entity.file.FileEntity;
import ru.soune.nocopy.entity.file.ImageHashEntity;
import ru.soune.nocopy.repository.ImageHashRepository;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.time.LocalDateTime;
2026-01-19 15:49:27 +07:00
import java.util.Map;
@Service
@RequiredArgsConstructor
@Slf4j
public class ImageHashService {
private final ImageHashRepository repository;
2026-01-19 15:49:27 +07:00
public Map<String, Integer> calculateHash(Path imagePath) throws IOException {
File file = imagePath.toFile();
PHash pHash = PerceptualHashHelper.INSTANCE.generateDCTPerceptualHash(file);
Long firstPart = pHash.getFirstPart();
Long secondPart = pHash.getSecondPart();
Map<String, Integer> hash = Map.of(
"hi", Math.toIntExact(firstPart),
"low", Math.toIntExact(secondPart));
return hash;
2026-01-19 15:49:27 +07:00
}
2026-01-19 15:49:27 +07:00
public void create(FileEntity file, Map<String, Integer> stringIntegerMap) {
ImageHashEntity entity = ImageHashEntity.builder()
.file(file)
.hash64Hi(stringIntegerMap.get("hi"))
.hash64Lo(stringIntegerMap.get("low"))
.hashAlgorithm("PHASH64")
.createdAt(LocalDateTime.now())
.build();
2026-01-19 15:49:27 +07:00
repository.save(entity);
}
}