/** * Get the digest for a given input stream using the default algorithm */ public static String getDigest(InputStream input) throws IOException, NoSuchAlgorithmException { return getDigest(input, DEFAULT_DIGEST_ALGORITHM); }
private URL addResourceContent(InputStream input, Map<String, Object> atts) throws IOException { synchronized (storageDir) { // Copy the input stream to temporary storage File tempFile = new File(storageDir.getAbsolutePath() + File.separator + "temp-content"); Long size = copyResourceContent(input, tempFile); atts.put(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE, size); // Calculate the SHA-256 String sha256; String algorithm = RepositoryContentHelper.DEFAULT_DIGEST_ALGORITHM; try { sha256 = RepositoryContentHelper.getDigest(new FileInputStream(tempFile), algorithm); atts.put(ContentNamespace.CONTENT_NAMESPACE, sha256); } catch (NoSuchAlgorithmException ex) { throw MESSAGES.noSuchAlgorithm(ex, algorithm); } // Move the content to storage location String contentPath = sha256.substring(0, 2) + File.separator + sha256.substring(2) + File.separator + "content"; File targetFile = new File(storageDir.getAbsolutePath() + File.separator + contentPath); targetFile.getParentFile().mkdirs(); tempFile.renameTo(targetFile); URL url = targetFile.toURI().toURL(); atts.put(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, url.toExternalForm()); return url; } }