Skip to content

Commit

Permalink
Delete target to prevent the obsolete file still existed caused by ht…
Browse files Browse the repository at this point in the history
…tp error
  • Loading branch information
yma96 committed Dec 3, 2024
1 parent 6c61b70 commit 452b14d
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public class ArchiveController
private static final Set<String> CHECKSUMS = Collections.unmodifiableSet( new HashSet<String>()
{
{
add( ".md5" );
add( ".sha1" );
add( ".sha256" );
add( ".md5" );
}
} );

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -374,6 +374,7 @@ private void downloadArtifacts( final Map<String, HistoricalEntryDTO> entryDTOs,
Map<String, List<String>> originalChecksumsMap = new HashMap<>();
if ( originalTracked != null )
{
logger.trace( "originalChecksumsMap generated for {}", content.getBuildConfigId() );
Map<String, HistoricalEntryDTO> originalEntries = reader.readEntries( originalTracked );
originalEntries.forEach( ( key, entry ) -> originalChecksumsMap.put( key, new ArrayList<>(
Arrays.asList( entry.getSha1(), entry.getSha256(), entry.getMd5() ) ) ) );
Expand Down Expand Up @@ -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 ) )
Expand All @@ -554,7 +556,7 @@ private HistoricalContentDTO unpackHistoricalArchive( String contentBuildDir, St

private boolean validateChecksum( final String filePath, final List<String> current, final List<String> 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;
Expand All @@ -563,14 +565,17 @@ private boolean validateChecksum( final String filePath, final List<String> 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 ) );
}

Expand All @@ -584,7 +589,7 @@ private Callable<Boolean> download( final String contentBuildDir, final String p
if ( target.exists() && validateChecksum( filePath, checksums, originalChecksums ) )
{
logger.debug(
"<<<Already existed in historical archive, and checksum equals, skip downloading, path: {}.",
"<<<Already existed in historical archive, and checksum matches, skip downloading, path: {}.",
path );
return true;
}
Expand All @@ -596,6 +601,11 @@ private Callable<Boolean> 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 );
Expand Down

0 comments on commit 452b14d

Please sign in to comment.