2026-01-13 22:40:52 +07:00
|
|
|
package ru.soune.nocopy.service;
|
|
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
import com.vrt.fileprotection.image.phash.PHash;
|
|
|
|
|
import com.vrt.fileprotection.image.phash.PerceptualHashHelper;
|
2026-01-13 22:40:52 +07:00
|
|
|
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;
|
2026-03-31 14:43:34 +07:00
|
|
|
import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
2026-01-13 22:40:52 +07:00
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
import java.io.File;
|
2026-01-13 22:40:52 +07:00
|
|
|
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;
|
2026-01-13 22:40:52 +07:00
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class ImageHashService {
|
|
|
|
|
|
|
|
|
|
private final ImageHashRepository repository;
|
|
|
|
|
|
2026-03-31 14:43:34 +07:00
|
|
|
private final CloudStorageService cloudStorageService;
|
|
|
|
|
|
2026-01-23 13:22:02 +07:00
|
|
|
public Map<String, Long> calculateHash(Path imagePath) throws IOException {
|
2026-04-08 21:58:28 +07:00
|
|
|
// File file = cloudStorageService.readFileFromStorageByPath(imagePath.toString());
|
|
|
|
|
File file = imagePath.toFile();
|
|
|
|
|
|
2026-01-30 14:00:21 +07:00
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
PHash pHash = PerceptualHashHelper.INSTANCE.generateDCTPerceptualHash(file);
|
2026-01-30 14:00:21 +07:00
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
Long firstPart = pHash.getFirstPart();
|
|
|
|
|
Long secondPart = pHash.getSecondPart();
|
2026-01-23 13:22:02 +07:00
|
|
|
Map<String, Long> hash = Map.of(
|
|
|
|
|
"hi", firstPart,
|
|
|
|
|
"low", secondPart);
|
2026-01-13 22:40:52 +07:00
|
|
|
|
2026-01-22 19:55:09 +07:00
|
|
|
return hash;
|
2026-01-19 15:49:27 +07:00
|
|
|
}
|
2026-01-13 22:40:52 +07:00
|
|
|
|
2026-01-23 13:22:02 +07:00
|
|
|
public void create(FileEntity file, Map<String, Long> stringIntegerMap) {
|
2026-01-19 15:49:27 +07:00
|
|
|
ImageHashEntity entity = ImageHashEntity.builder()
|
|
|
|
|
.file(file)
|
|
|
|
|
.hash64Hi(stringIntegerMap.get("hi"))
|
|
|
|
|
.hash64Lo(stringIntegerMap.get("low"))
|
|
|
|
|
.hashAlgorithm("PHASH64")
|
|
|
|
|
.createdAt(LocalDateTime.now())
|
|
|
|
|
.build();
|
2026-01-13 22:40:52 +07:00
|
|
|
|
2026-01-19 15:49:27 +07:00
|
|
|
repository.save(entity);
|
2026-01-13 22:40:52 +07:00
|
|
|
}
|
|
|
|
|
}
|