Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions src/main/java/io/carbone/CarboneServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ public boolean checkPathIsAbsolute(String path) {
return p.isAbsolute();
}

@Override
public String generateTemplateId(byte[] fileBytes) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}

digest.update(fileBytes);
byte[] hashByte = digest.digest();
StringBuilder hexString = new StringBuilder();
for (byte b : hashByte) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append(0);
hexString.append(hex);
}
return hexString.toString();
}

public String generateTemplateId(String path) {
try {
File file = new File(path);
Expand All @@ -64,23 +85,7 @@ public String generateTemplateId(String path) {
return null;
}

MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}

digest.update(fileBytes);
byte[] hashByte = digest.digest();
StringBuilder hexString = new StringBuilder();
for (byte b : hashByte) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append(0);
hexString.append(hex);
}
return hexString.toString();
return generateTemplateId(fileBytes);
} catch (Exception e) {
e.printStackTrace();
return null;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/carbone/ICarboneServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,12 @@ public interface ICarboneServices {
* @return give a precalculates templateId
*/
String generateTemplateId(String path);

/**
* precalculates the template id
* @param fileBytes byte content of the template file
* @return give a precalculates templateId
*/
String generateTemplateId(byte[] fileBytes);

}
12 changes: 12 additions & 0 deletions src/test/java/io/carbone/CarboneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,16 @@ public void Test_Carbon_Service_Fectory_With_No_Argument() throws CarboneExcepti
String status = carboneService.getStatus();
assertEquals(status.contains("\"success\":true,\"code\":200"), true);
}

@Test
public void Test_Generate_Template_Id_From_Byte_Array() throws CarboneException, IOException
{
String filename = "/src/test/java/io/carbone/template3.odt";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);

String templateId = carboneServices.generateTemplateId(fileBytes);

assertEquals(templateId, "fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987a");
}
}