Skip to content

Commit

Permalink
Adding notifier if image conversion is attempted or aborterd due to f…
Browse files Browse the repository at this point in the history
…ile size
  • Loading branch information
CEBergin-Vivid committed Apr 12, 2024
1 parent cedf025 commit 6daa299
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ public static boolean setVirusScanMetadata(String accessToken, String fileId, St
return metaUpdateResponse.getStatus() == 200;
}

public static void setImageConversionMetadata(String accessToken, String fileId, String versionNumber,
JSONObject fileDetails, String conversionStatus) throws UnirestException {
JSONArray metaArray = fileDetails.getJSONArray("metadata");
// Locate any existing scan meta and remove
for (int i = 0; i < metaArray.length(); i++) {
String metadataName = metaArray.getJSONObject(i).getString("metadataName");
if (metadataName.equalsIgnoreCase("WFDMConversionStatus-" + versionNumber)) {
metaArray.remove(i);
break;
}
}

// inject scan meta
JSONObject meta = new JSONObject();
meta.put("@type", "http://resources.wfdm.nrs.gov.bc.ca/fileMetadataResource");
meta.put("metadataName", "WFDMConversionStatus-" + versionNumber);
meta.put("metadataValue", conversionStatus);
metaArray.put(meta);

// PUT the changes
HttpResponse<String> metaUpdateResponse = Unirest.put(System.getenv("WFDM_DOCUMENT_API_URL").trim() + fileId)
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + accessToken)
.body(fileDetails.toString())
.asString();

}

/**
* Fetch the bytes for a WFDM File resource. This will return a
* BufferedInputStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,22 @@ public SQSBatchResponse handleRequest(SQSEvent sqsEvent, Context context) {
fileExtension = "";
}

Integer fileSize;
boolean fileTooLargeToConvert = false;
if (fileDetailsJson.has("fileSize")) {
fileSize = Integer.parseInt(fileDetailsJson.get("fileSize").toString());
if (fileSize > 10000000) {
fileTooLargeToConvert = true;
GetFileFromWFDMAPI.setImageConversionMetadata(wfdmToken, fileId, versionNumber,
fileDetailsJson, "Image Conversion aborted due to file size");
}
}

// if a file has a heic or heif mimetype it needs to be converted by the image
// conversion lambda rather than processed
if (fileExtension.equals("HEIC") || fileExtension.equals("HEIF") ) {
if ((fileExtension.equals("HEIC") || fileExtension.equals("HEIF")) && !fileTooLargeToConvert) {
GetFileFromWFDMAPI.setImageConversionMetadata(wfdmToken, fileId, versionNumber,
fileDetailsJson, "Image Conversion attempted");
logger.log("\nInfo: File with mimeType of " + mimeType + " calling image conversion lambda");
AWSLambda client = AWSLambdaAsyncClient.builder().withRegion(region).build();
InvokeRequest request = new InvokeRequest();
Expand Down

0 comments on commit 6daa299

Please sign in to comment.