Compare commits

...

2 Commits

Author SHA1 Message Date
Larry L a208b818e7 v1 SCORM Extractor complete - removed logging 1 year ago
Larry L a84b0e3ed5 v1 SCORM Extractor complete 1 year ago
  1. 104
      core/src/main/java/com/mcaps/core/workflows/ScormExtractor.java

104
core/src/main/java/com/mcaps/core/workflows/ScormExtractor.java

@ -12,6 +12,11 @@ import java.util.regex.Pattern;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import javax.activation.MimetypesFileTypeMap;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Constants; import org.osgi.framework.Constants;
@ -38,91 +43,66 @@ public class ScormExtractor implements WorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(ScormExtractor.class); private static final Logger log = LoggerFactory.getLogger(ScormExtractor.class);
ResourceResolver resourceResolver; ResourceResolver resourceResolver;
Session session;
@Reference @Reference
QueryBuilder queryBuilder; QueryBuilder queryBuilder;
@Override @Override
public void execute(WorkItem wi, WorkflowSession ws, MetaDataMap mdm) throws WorkflowException { public void execute(WorkItem wi, WorkflowSession ws, MetaDataMap mdm) throws WorkflowException {
resourceResolver = ws.adaptTo(ResourceResolver.class); resourceResolver = ws.adaptTo(ResourceResolver.class);
session = ws.adaptTo(Session.class);
log.warn("SCORM WORKFLOW: MetaDataMap args: " + mdm.get("PROCESS_ARGS", "string").toString());
String[] params = mdm.get("PROCESS_ARGS", "string").toString().split(",");
/* Initially created for Workflow Launcher
String excludePath = params[0];
String saveToLocation = params[1];
String payloadPath = wi.getContentPath().replace("/jcr:content/renditions/original","");
unzipFile(payloadPath, saveToLocation);
String nodePath = wi.getContentPath().replace(".zip/jcr:content/renditions/original","");
cleanup(nodePath, resourceResolver);
*/
/* Created for manually triggered workflows */ /* Created for manually triggered workflows */
String payloadPath = wi.getContentPath().replace("/jcr:content/renditions/original",""); String payloadPath = wi.getContentPath().replace("/jcr:content/renditions/original","");
String[] params = mdm.get("PROCESS_ARGS", "string").toString().split(",");
String[] fileExtensions = params; // Valid extensions com from the 'Arguments' field in the workflows 'Process' tab String[] fileExtensions = params; // Valid extensions com from the 'Arguments' field in the workflows 'Process' tab
unzipFile(payloadPath, fileExtensions); unzipFile(payloadPath, fileExtensions);
cleanup(payloadPath, resourceResolver);
}
private void cleanup(String path, ResourceResolver resourceResolver) {
log.warn("SCORM WORKFLOW: inside cleanup: path - " + path);
Resource resource = resourceResolver.getResource(path);
log.warn("SCORM WORKFLOW: inside cleanup: resource - " + resource);
/*Node zipNode = resource.adaptTo(Node.class);
try {
resourceResolver.delete(resource);
resourceResolver.commit();
} catch (PersistenceException e) {
e.printStackTrace();
}*/
} }
private void unzipFile(String filePath, String[] validFileExtensions) {//, String outputFolder) { private void unzipFile(String filePath, String[] validFileExtensions) {//, String outputFolder) {
String nodePath = filePath.replaceAll("(?:.[^/]+).zip(/jcr:content/renditions/original)?",""); String nodePath = filePath.replaceAll("(?:.[^/]+).zip(/jcr:content/renditions/original)?",""); // remove filename to get AEM pathing for original zip file
Pattern p = Pattern.compile("(?:.[^/]+).("+String.join("|", validFileExtensions)+")(/jcr:content/renditions/original)?"); // the pattern to search for Pattern p = Pattern.compile("(?:.[^/]+).("+String.join("|", validFileExtensions)+")(/jcr:content/renditions/original)?"); // pattern to search for filename only
log.warn("SCORM WORKFLOW: inside unzipFile: filePath - " + filePath);
log.warn("SCORM WORKFLOW: inside unzipFile: nodePath - " + nodePath);
Resource zipfile = resourceResolver.getResource(filePath); Resource zipfile = resourceResolver.getResource(filePath);
AssetManager am = resourceResolver.adaptTo(AssetManager.class); AssetManager am = resourceResolver.adaptTo(AssetManager.class);
log.warn("SCORM WORKFLOW: Resource - " + zipfile.getResourceType());
Asset a = zipfile.adaptTo(Asset.class); Asset a = zipfile.adaptTo(Asset.class);
InputStream is = a.getOriginal().getStream(); InputStream is = a.getOriginal().getStream();
//NOTE: now working in initial directory level, need to extract to new folders //NOTE: now working in initial directory level, need to extract individual assets from zip
try (ZipInputStream zipIn = new ZipInputStream(is)) { try (ZipInputStream zipIn = new ZipInputStream(is)) {
ZipEntry entry = zipIn.getNextEntry(); ZipEntry entry = zipIn.getNextEntry();
while (entry != null) { while (entry != null) {
Matcher m = p.matcher(entry.getName()); Matcher m = p.matcher(entry.getName()); // set up matcher to parse original filename
String matchedString = m.find()?m.group(0):"no match"; String matchedString = m.find()?m.group(0):"no match"; // extract filename w/o pathing
log.warn("SCORM WORKFLOW: matcher - " + matchedString); String extractedFilename = matchedString.replace("/", ""); // filename w/o leading '/'
String[] fileName = entry.getName().split("\\."); String[] fileName = entry.getName().split("\\."); // filename split to get extension
String zipFilePath = nodePath + matchedString; String fileExtension = fileName.length >= 2 ? fileName[fileName.length-1]:"none"; // file extension
String zipFileExtension = fileName.length >= 2 ? fileName[fileName.length-1]:"none"; boolean extensionIsValid = Arrays.stream(validFileExtensions).anyMatch(fileExtension::equals); // file extension check
boolean extensionIsValid = Arrays.stream(validFileExtensions).anyMatch(zipFileExtension::equals);
// Extract file // Extract files
if (!entry.isDirectory() && extensionIsValid) { if (!entry.isDirectory() && extensionIsValid) {
log.warn("SCORM WORKFLOW: zipFilePath - " + zipFilePath); File f = new File(extractedFilename); //create temporary file
log.warn("SCORM WORKFLOW: zipFileExtension - " + zipFileExtension);
File f = new File(entry.getName());
try (FileOutputStream fos = new FileOutputStream(f)) { try (FileOutputStream fos = new FileOutputStream(f)) {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int length; int length;
while ((length = zipIn.read(buffer)) >= 0) { while ((length = zipIn.read(buffer)) >= 0) {
fos.write(buffer, 0, length); fos.write(buffer, 0, length);
} }
log.warn("SCORM WORKFLOW: saving to AEM: " + zipFilePath);
log.warn("SCORM WORKFLOW: saving to AEM: " + extractedFilename);
InputStream fis = new FileInputStream(f); InputStream fis = new FileInputStream(f);
am.createAsset(zipFilePath, fis, null, true); String aemFilePath = nodePath + matchedString; // AEM DAM path with filename
log.warn("SCORM WORKFLOW: file extracted: " + zipFilePath);
MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
String mimeType = fileTypeMap.getContentType(f.getName()); // get MIME type
am.createAsset(aemFilePath, fis, mimeType, true); // save to AEM DAM
f.delete(); //delete temporary file once it has been saved to AEM.
setAssetMetadata(aemFilePath, a);
} }
} }
@ -131,10 +111,32 @@ public class ScormExtractor implements WorkflowProcess {
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
log.error(e.getMessage()); log.error(e.getMessage());
log.error(e.getStackTrace().toString()); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
log.error(e.getMessage()); log.error(e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
private void setAssetMetadata(String filePath, Asset parentAsset) {
Resource r = resourceResolver.getResource(filePath);
Node assetNode = r.adaptTo(Node.class);
String parentLMSID = parentAsset.getMetadataValue("mcaps:lmsId");
String skills = parentAsset.getMetadataValue("mcaps:skills");
String[] parentSkills = skills.replace(", ", ",").split(",");
String parentCategories = parentAsset.getMetadataValue("mcaps:category");
String parentCourseUrl = parentAsset.getMetadataValue("mcaps:courseUrl");
try {
Node metadataNode = assetNode.getNode("jcr:content/metadata");
metadataNode.setProperty("mcaps:lmsId", parentLMSID);
metadataNode.setProperty("mcaps:skills", parentSkills);
metadataNode.setProperty("mcaps:categories", parentCategories);
metadataNode.setProperty("mcaps:courseUrl", parentCourseUrl);
session.save();
} catch (RepositoryException e) {
e.printStackTrace();
}
}
} }
Loading…
Cancel
Save