Commit 00adbe9f authored by Matija Obreza's avatar Matija Obreza
Browse files

Create missing SHA-1 and MD5 on startup

parent 98abaeb0
......@@ -57,4 +57,12 @@ public interface RepositoryFilePersistence extends JpaRepository<RepositoryFile,
*/
@Query("select distinct(rf.path) from RepositoryFile rf where rf.path like ?1%")
List<String> listDistinctPaths(String prefix, Pageable pageable);
/**
* Find files with missing hashes
*
* @return the list
*/
@Query("select rf from RepositoryFile rf where rf.sha1Sum is null or rf.md5Sum is null")
List<RepositoryFile> findByMissingHashSums();
}
......@@ -39,6 +39,7 @@ import org.genesys2.server.filerepository.service.BytesStorageService;
import org.genesys2.server.filerepository.service.RepositoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
......@@ -49,7 +50,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional(readOnly = true)
public class RepositoryServiceImpl implements RepositoryService {
public class RepositoryServiceImpl implements RepositoryService, InitializingBean {
/** The Constant LOG. */
private final static Logger LOG = LoggerFactory.getLogger(RepositoryServiceImpl.class);
......@@ -66,6 +67,32 @@ public class RepositoryServiceImpl implements RepositoryService {
@Autowired
private BytesStorageService bytesStorageService;
@Override
public void afterPropertiesSet() throws Exception {
addMissingHashSums();
}
/**
* Just in case.
*/
@Transactional
public void addMissingHashSums() {
for (RepositoryFile repositoryFile : repositoryFilePersistence.findByMissingHashSums()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Updating SHA-1 and MD5 for file uuid={}", repositoryFile.getUuid());
}
try {
byte[] bytes = getFileBytes(repositoryFile.getPath(), repositoryFile.getFilename());
repositoryFile.setSha1Sum(DigestUtils.sha1Hex(bytes));
repositoryFile.setMd5Sum(DigestUtils.md5Hex(bytes));
repositoryFilePersistence.save(repositoryFile);
} catch (NoSuchRepositoryFileException e) {
LOG.warn("Failed to generate hash sums: {}", e.getMessage());
}
}
}
/*
* (non-Javadoc)
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment