11/*
2- * Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
2+ * Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
3030
3131import javax .annotation .Nonnull ;
3232import java .io .File ;
33- import java .nio .file .Paths ;
3433import java .util .List ;
3534
3635
@@ -47,20 +46,20 @@ public DataService(WorkerConfigurationService workerConfigurationService) {
4746 /**
4847 * Download dataset file for the given standard task and save
4948 * it in {@link IexecFileHelper#SLASH_IEXEC_IN}.
50- *
49+ *
5150 * @param taskDescription Task description containing dataset related parameters
5251 * @return downloaded dataset file path
5352 * @throws WorkflowException if download fails or bad checksum.
5453 */
5554 public String downloadStandardDataset (@ Nonnull TaskDescription taskDescription )
5655 throws WorkflowException {
57- String chainTaskId = taskDescription .getChainTaskId ();
58- String uri = taskDescription .getDatasetUri ();
59- String filename = taskDescription .getDatasetAddress ();
60- String parentDirectoryPath = workerConfigurationService .getTaskInputDir (chainTaskId );
56+ final String chainTaskId = taskDescription .getChainTaskId ();
57+ final String uri = taskDescription .getDatasetUri ();
58+ final String filename = taskDescription .getDatasetAddress ();
59+ final String parentDirectoryPath = workerConfigurationService .getTaskInputDir (chainTaskId );
6160 String datasetLocalFilePath = "" ;
6261 if (MultiAddressHelper .isMultiAddress (uri )) {
63- for (String ipfsGateway : MultiAddressHelper .IPFS_GATEWAYS ) {
62+ for (final String ipfsGateway : MultiAddressHelper .IPFS_GATEWAYS ) {
6463 log .debug ("Try to download dataset from {}" , ipfsGateway );
6564 datasetLocalFilePath =
6665 downloadFile (chainTaskId , ipfsGateway + uri , parentDirectoryPath , filename );
@@ -75,17 +74,16 @@ public String downloadStandardDataset(@Nonnull TaskDescription taskDescription)
7574 if (datasetLocalFilePath .isEmpty ()) {
7675 throw new WorkflowException (ReplicateStatusCause .DATASET_FILE_DOWNLOAD_FAILED );
7776 }
78- String expectedSha256 = taskDescription .getDatasetChecksum ();
77+ final String expectedSha256 = taskDescription .getDatasetChecksum ();
7978 if (StringUtils .isEmpty (expectedSha256 )) {
80- log .warn ("INSECURE! Cannot check empty on-chain dataset checksum " +
81- "[chainTaskId:{}]" , chainTaskId );
79+ log .warn ("INSECURE! Cannot check empty on-chain dataset checksum [chainTaskId:{}]" ,
80+ chainTaskId );
8281 return datasetLocalFilePath ;
8382 }
84- String actualSha256 = FileHashUtils .sha256 (new File (datasetLocalFilePath ));
83+ final String actualSha256 = FileHashUtils .sha256 (new File (datasetLocalFilePath ));
8584 if (!expectedSha256 .equals (actualSha256 )) {
86- log .error ("Dataset checksum mismatch [chainTaskId:{}, " +
87- "expected:{}, actual:{}]" , chainTaskId , expectedSha256 ,
88- actualSha256 );
85+ log .error ("Dataset checksum mismatch [chainTaskId:{}, expected:{}, actual:{}]" ,
86+ chainTaskId , expectedSha256 , actualSha256 );
8987 throw new WorkflowException (ReplicateStatusCause .DATASET_FILE_BAD_CHECKSUM );
9088 }
9189 return datasetLocalFilePath ;
@@ -94,18 +92,17 @@ public String downloadStandardDataset(@Nonnull TaskDescription taskDescription)
9492 /**
9593 * Download input files for the given standard task and save them
9694 * in the input folder.
97- *
95+ *
9896 * @param chainTaskId Task ID used to create input files download folder
99- * @param uriList List of input files to download
97+ * @param uriList List of input files to download
10098 * @throws WorkflowException if download fails.
10199 */
102- public void downloadStandardInputFiles (String chainTaskId , @ Nonnull List <String > uriList )
100+ public void downloadStandardInputFiles (final String chainTaskId , @ Nonnull final List <String > uriList )
103101 throws WorkflowException {
104- for (String uri : uriList ) {
105- String filename = !StringUtils .isEmpty (uri )
106- ? Paths .get (uri ).getFileName ().toString ()
107- : "" ;
108- String parenDirectoryPath = workerConfigurationService .getTaskInputDir (chainTaskId );
102+ for (final String uri : uriList ) {
103+ final String filename = FileHashUtils .createFileNameFromUri (uri );
104+ final String parenDirectoryPath = workerConfigurationService .getTaskInputDir (chainTaskId );
105+ log .debug ("Download file [uri:{}, fileName:{}]" , uri , filename );
109106 if (downloadFile (chainTaskId , uri , parenDirectoryPath , filename ).isEmpty ()) {
110107 throw new WorkflowException (ReplicateStatusCause .INPUT_FILES_DOWNLOAD_FAILED );
111108 }
@@ -115,21 +112,21 @@ public void downloadStandardInputFiles(String chainTaskId, @Nonnull List<String>
115112 /**
116113 * Download a file from a URI in the provided parent
117114 * directory and save it with the provided filename.
118- *
119- * @param chainTaskId Task ID, for logging purpose
120- * @param uri URI of single file to download
115+ *
116+ * @param chainTaskId Task ID, for logging purpose
117+ * @param uri URI of single file to download
121118 * @param parentDirectoryPath Destination folder on worker host
122- * @param filename Name of downloaded file in destination folder
119+ * @param filename Name of downloaded file in destination folder
123120 * @return absolute path of the saved file on worker host
124121 */
125- String downloadFile (String chainTaskId , String uri ,
126- String parentDirectoryPath , String filename ) {
122+ String downloadFile (final String chainTaskId , final String uri ,
123+ final String parentDirectoryPath , final String filename ) {
127124 if (StringUtils .isEmpty (chainTaskId ) ||
128125 StringUtils .isEmpty (uri ) ||
129126 StringUtils .isEmpty (parentDirectoryPath ) ||
130127 StringUtils .isEmpty (filename )) {
131128 log .error ("Failed to download, args shouldn't be empty " +
132- "[chainTaskId:{}, datasetUri:{}, parentDir:{}, filename:{}]" ,
129+ "[chainTaskId:{}, datasetUri:{}, parentDir:{}, filename:{}]" ,
133130 chainTaskId , uri , parentDirectoryPath , filename );
134131 return StringUtils .EMPTY ;
135132 }
0 commit comments