use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class PayloadWriter method writePayloadFiles.
/**
* Write the payload <b>file(s)</b> to the output directory
*
* @param payloadManifests the set of objects representing the payload manifests
* @param fetchItems the list of items to exclude from writing in the output directory because they will be fetched
* @param outputDir the data directory of the bag
* @param bagDataDir the data directory of the bag
*
* @throws IOException if there was a problem writing a file
*/
public static void writePayloadFiles(final Set<Manifest> payloadManifests, final List<FetchItem> fetchItems, final Path outputDir, final Path bagDataDir) throws IOException {
logger.info(messages.getString("writing_payload_files"));
final Set<Path> fetchPaths = getFetchPaths(fetchItems, bagDataDir);
for (final Manifest payloadManifest : payloadManifests) {
for (final Path payloadFile : payloadManifest.getFileToChecksumMap().keySet()) {
final Path relativePayloadPath = bagDataDir.relativize(payloadFile);
if (fetchPaths.contains(relativePayloadPath.normalize())) {
logger.info(messages.getString("skip_fetch_item_when_writing_payload"), payloadFile);
} else {
final Path writeToPath = outputDir.resolve(relativePayloadPath);
logger.debug(messages.getString("writing_payload_file_to_path"), payloadFile, writeToPath);
final Path parent = writeToPath.getParent();
if (parent != null) {
Files.createDirectories(parent);
}
Files.copy(payloadFile, writeToPath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class AddPayloadToBagManifestVistorTest method includeDotKeepFilesInManifest.
@Test
public void includeDotKeepFilesInManifest() throws Exception {
Manifest manifest = new Manifest(StandardSupportedAlgorithms.MD5);
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
Map<Manifest, MessageDigest> map = new HashMap<>();
map.put(manifest, messageDigest);
boolean includeHiddenFiles = false;
Path start = Paths.get(new File("src/test/resources/dotKeepExampleBag").toURI()).resolve("data");
CreatePayloadManifestsVistor sut = new CreatePayloadManifestsVistor(map, includeHiddenFiles);
Files.walkFileTree(start, sut);
assertEquals(1, manifest.getFileToChecksumMap().size());
assertTrue(manifest.getFileToChecksumMap().containsKey(start.resolve("fooDir/.keep")));
}
use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class ManifestReader method readManifest.
/**
* Reads a manifest file and converts it to a {@link Manifest} object.
*
* @param nameMapping a map between BagIt algorithm names and {@link MessageDigest} names
* @param manifestFile a specific manifest file
* @param bagRootDir the root directory of the bag
* @param charset the encoding to use when reading the manifest file
* @return the converted manifest object from the file
*
* @throws IOException if there is a problem reading a file
* @throws MaliciousPathException if there is path that is referenced in the manifest that is outside the bag root directory
* @throws UnsupportedAlgorithmException if the manifest uses a algorithm that isn't supported
* @throws InvalidBagitFileFormatException if the manifest is not formatted properly
*/
public static Manifest readManifest(final BagitAlgorithmNameToSupportedAlgorithmMapping nameMapping, final Path manifestFile, final Path bagRootDir, final Charset charset) throws IOException, MaliciousPathException, UnsupportedAlgorithmException, InvalidBagitFileFormatException {
logger.debug(messages.getString("reading_manifest"), manifestFile);
final String alg = PathUtils.getFilename(manifestFile).split("[-\\.]")[1];
final SupportedAlgorithm algorithm = nameMapping.getSupportedAlgorithm(alg);
final Manifest manifest = new Manifest(algorithm);
final Map<Path, String> filetToChecksumMap = readChecksumFileMap(manifestFile, bagRootDir, charset);
manifest.setFileToChecksumMap(filetToChecksumMap);
return manifest;
}
use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class ManifestWriterTest method testManifestsDontContainWindowsFilePathSeparator.
@Test
public void testManifestsDontContainWindowsFilePathSeparator() throws IOException {
Set<Manifest> tagManifests = new HashSet<>();
Manifest manifest = new Manifest(StandardSupportedAlgorithms.MD5);
manifest.getFileToChecksumMap().put(Paths.get("/foo/bar/ham/data/one/two/buckleMyShoe.txt"), "someHashValue");
tagManifests.add(manifest);
File outputDir = folder.newFolder();
File tagManifest = new File(outputDir, "tagmanifest-md5.txt");
assertFalse(tagManifest.exists());
ManifestWriter.writeTagManifests(tagManifests, Paths.get(outputDir.toURI()), Paths.get("/foo/bar/ham"), StandardCharsets.UTF_8);
List<String> lines = Files.readAllLines(Paths.get(tagManifest.toURI()));
for (String line : lines) {
assertFalse("Line [" + line + "] contains \\ which is not allowed by the bagit specification", line.contains("\\"));
}
}
use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.
the class ManifestWriterTest method testWriteTagManifests.
@Test
public void testWriteTagManifests() throws IOException {
Set<Manifest> tagManifests = new HashSet<>();
Manifest manifest = new Manifest(StandardSupportedAlgorithms.MD5);
manifest.getFileToChecksumMap().put(Paths.get("/foo/bar/ham/data/one/two/buckleMyShoe.txt"), "someHashValue");
tagManifests.add(manifest);
File outputDir = folder.newFolder();
File tagManifest = new File(outputDir, "tagmanifest-md5.txt");
assertFalse(tagManifest.exists());
ManifestWriter.writeTagManifests(tagManifests, Paths.get(outputDir.toURI()), Paths.get("/foo/bar/ham"), StandardCharsets.UTF_8);
assertTrue(tagManifest.exists());
}
Aggregations