From 452b14d8d3f0e35b4a744d6dc357e45d7ee5928b Mon Sep 17 00:00:00 2001 From: yma Date: Tue, 3 Dec 2024 11:53:16 +0800 Subject: [PATCH] Delete target to prevent the obsolete file still existed caused by http error --- .../archive/controller/ArchiveController.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java b/src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java index 85ffc69..54ed5c1 100644 --- a/src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java +++ b/src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java @@ -103,9 +103,9 @@ public class ArchiveController private static final Set CHECKSUMS = Collections.unmodifiableSet( new HashSet() { { - add( ".md5" ); add( ".sha1" ); add( ".sha256" ); + add( ".md5" ); } } ); @@ -299,7 +299,7 @@ public void deleteArchiveWithChecksum( final String buildConfigId, final String } String stored = bytesToHex( digest.digest() ); - // only delete the zip once checksum is equaled + // only delete the zip once checksum is matched if ( stored.equals( checksum ) ) { zip.delete(); @@ -374,6 +374,7 @@ private void downloadArtifacts( final Map entryDTOs, Map> originalChecksumsMap = new HashMap<>(); if ( originalTracked != null ) { + logger.trace( "originalChecksumsMap generated for {}", content.getBuildConfigId() ); Map originalEntries = reader.readEntries( originalTracked ); originalEntries.forEach( ( key, entry ) -> originalChecksumsMap.put( key, new ArrayList<>( Arrays.asList( entry.getSha1(), entry.getSha256(), entry.getMd5() ) ) ) ); @@ -530,6 +531,7 @@ private HistoricalContentDTO unpackHistoricalArchive( String contentBuildDir, St ZipEntry entry; while ( ( entry = inputStream.getNextEntry() ) != null ) { + logger.trace( "entry path:" + entry.getName() ); File outputFile = new File( contentBuildDir, entry.getName() ); outputFile.getParentFile().mkdirs(); try ( FileOutputStream outputStream = new FileOutputStream( outputFile ) ) @@ -554,7 +556,7 @@ private HistoricalContentDTO unpackHistoricalArchive( String contentBuildDir, St private boolean validateChecksum( final String filePath, final List current, final List original ) { - if ( CHECKSUMS.stream().anyMatch( suffix -> filePath.toLowerCase().endsWith( "." + suffix ) ) ) + if ( CHECKSUMS.stream().anyMatch( suffix -> filePath.toLowerCase().endsWith( suffix ) ) ) { // skip to validate checksum files return false; @@ -563,14 +565,17 @@ private boolean validateChecksum( final String filePath, final List curr { return false; } + // once sha1 is matched, skip downloading if ( original.get( 0 ) != null && original.get( 0 ).equals( current.get( 0 ) ) ) { return true; } + // once sha256 is matched, skip downloading if ( original.get( 1 ) != null && original.get( 1 ).equals( current.get( 1 ) ) ) { return true; } + // once md5 is matched, skip downloading return original.get( 2 ) != null && original.get( 2 ).equals( current.get( 2 ) ); } @@ -584,7 +589,7 @@ private Callable download( final String contentBuildDir, final String p if ( target.exists() && validateChecksum( filePath, checksums, originalChecksums ) ) { logger.debug( - "<< download( final String contentBuildDir, final String p context.setCookieStore( cookieStore ); final HttpGet request = new HttpGet( path ); InputStream input = null; + if ( target.exists() ) + { + // prevent the obsolete file still existed caused by http error + target.delete(); + } try { CloseableHttpResponse response = client.execute( request, context );