44import java .nio .file .Files ;
55import java .nio .file .Path ;
66import java .nio .file .StandardCopyOption ;
7+ import java .util .HashSet ;
8+ import java .util .List ;
79import java .util .ResourceBundle ;
810import java .util .Set ;
911
1012import org .slf4j .Logger ;
1113import org .slf4j .LoggerFactory ;
1214
1315import gov .loc .repository .bagit .domain .Bag ;
16+ import gov .loc .repository .bagit .domain .FetchItem ;
1417import gov .loc .repository .bagit .domain .Manifest ;
1518import gov .loc .repository .bagit .domain .Version ;
1619
@@ -35,12 +38,12 @@ static Path writeVersionDependentPayloadFiles(final Bag bag, final Path outputDi
3538 if (bag .getVersion ().isSameOrNewer (VERSION_2_0 )){
3639 bagitDir = outputDir .resolve (".bagit" );
3740 Files .createDirectories (bagitDir );
38- writePayloadFiles (bag .getPayLoadManifests (), outputDir , bag .getRootDir ());
41+ writePayloadFiles (bag .getPayLoadManifests (), bag . getItemsToFetch (), outputDir , bag .getRootDir ());
3942 }
4043 else {
4144 final Path dataDir = outputDir .resolve ("data" );
4245 Files .createDirectories (dataDir );
43- writePayloadFiles (bag .getPayLoadManifests (), dataDir , bag .getRootDir ().resolve ("data" ));
46+ writePayloadFiles (bag .getPayLoadManifests (), bag . getItemsToFetch (), dataDir , bag .getRootDir ().resolve ("data" ));
4447 }
4548
4649 return bagitDir ;
@@ -50,25 +53,41 @@ static Path writeVersionDependentPayloadFiles(final Bag bag, final Path outputDi
5053 * Write the payload <b>file(s)</b> to the output directory
5154 *
5255 * @param payloadManifests the set of objects representing the payload manifests
56+ * @param fetchItems the list of items to exclude from writing in the output directory because they will be fetched
5357 * @param outputDir the data directory of the bag
5458 * @param bagDataDir the data directory of the bag
5559 *
5660 * @throws IOException if there was a problem writing a file
5761 */
58- public static void writePayloadFiles (final Set <Manifest > payloadManifests , final Path outputDir , final Path bagDataDir ) throws IOException {
62+ public static void writePayloadFiles (final Set <Manifest > payloadManifests , final List < FetchItem > fetchItems , final Path outputDir , final Path bagDataDir ) throws IOException {
5963 logger .info (messages .getString ("writing_payload_files" ));
64+ final Set <Path > fetchPaths = getFetchPaths (fetchItems );
65+
6066 for (final Manifest payloadManifest : payloadManifests ){
6167 for (final Path payloadFile : payloadManifest .getFileToChecksumMap ().keySet ()){
62- final Path relativePayloadPath = bagDataDir .relativize (payloadFile );
63-
64- final Path writeToPath = outputDir .resolve (relativePayloadPath );
65- logger .debug (messages .getString ("writing_payload_file_to_path" ), payloadFile , writeToPath );
66- final Path parent = writeToPath .getParent ();
67- if (parent != null ){
68- Files .createDirectories (parent );
68+ final Path relativePayloadPath = bagDataDir .relativize (payloadFile );
69+
70+ if (fetchPaths .contains (relativePayloadPath .normalize ())) {
71+ logger .info (messages .getString ("skip_fetch_item_when_writing_payload" ), payloadFile );
72+ }
73+ else {
74+ final Path writeToPath = outputDir .resolve (relativePayloadPath );
75+ logger .debug (messages .getString ("writing_payload_file_to_path" ), payloadFile , writeToPath );
76+ final Path parent = writeToPath .getParent ();
77+ if (parent != null ){
78+ Files .createDirectories (parent );
79+ }
80+ Files .copy (payloadFile , writeToPath , StandardCopyOption .COPY_ATTRIBUTES , StandardCopyOption .REPLACE_EXISTING );
6981 }
70- Files .copy (payloadFile , writeToPath , StandardCopyOption .COPY_ATTRIBUTES , StandardCopyOption .REPLACE_EXISTING );
7182 }
7283 }
7384 }
85+
86+ private static Set <Path > getFetchPaths (final List <FetchItem > fetchItems ){
87+ final Set <Path > fetchPaths = new HashSet <>();
88+ for (final FetchItem fetchItem : fetchItems ) {
89+ fetchPaths .add (fetchItem .getPath ());
90+ }
91+ return fetchPaths ;
92+ }
7493}
0 commit comments