Browse Source

v1 SCORM Extractor complete

- removed logging
- removed cleanup function
- determine MIME
master
Larry L 1 year ago
parent
commit
a84b0e3ed5
  1. 76
      core/src/main/java/com/mcaps/core/workflows/ScormExtractor.java

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

@ -12,6 +12,9 @@ import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.activation.MimetypesFileTypeMap;
import javax.jcr.ValueFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Constants;
@ -38,91 +41,64 @@ public class ScormExtractor implements WorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(ScormExtractor.class);
ResourceResolver resourceResolver;
ValueFactory valueFactory;
@Reference
QueryBuilder queryBuilder;
@Override
public void execute(WorkItem wi, WorkflowSession ws, MetaDataMap mdm) throws WorkflowException {
resourceResolver = ws.adaptTo(ResourceResolver.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 */
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
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) {
String nodePath = filePath.replaceAll("(?:.[^/]+).zip(/jcr:content/renditions/original)?","");
Pattern p = Pattern.compile("(?:.[^/]+).("+String.join("|", validFileExtensions)+")(/jcr:content/renditions/original)?"); // the pattern to search for
log.warn("SCORM WORKFLOW: inside unzipFile: filePath - " + filePath);
log.warn("SCORM WORKFLOW: inside unzipFile: nodePath - " + nodePath);
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)?"); // pattern to search for filename only
Resource zipfile = resourceResolver.getResource(filePath);
AssetManager am = resourceResolver.adaptTo(AssetManager.class);
log.warn("SCORM WORKFLOW: Resource - " + zipfile.getResourceType());
Asset a = zipfile.adaptTo(Asset.class);
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)) {
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
Matcher m = p.matcher(entry.getName());
String matchedString = m.find()?m.group(0):"no match";
log.warn("SCORM WORKFLOW: matcher - " + matchedString);
Matcher m = p.matcher(entry.getName()); // set up matcher to parse original filename
String matchedString = m.find()?m.group(0):"no match"; // extract filename w/o pathing
String extractedFilename = matchedString.replace("/", ""); // filename w/o leading '/'
String[] fileName = entry.getName().split("\\.");
String zipFilePath = nodePath + matchedString;
String zipFileExtension = fileName.length >= 2 ? fileName[fileName.length-1]:"none";
boolean extensionIsValid = Arrays.stream(validFileExtensions).anyMatch(zipFileExtension::equals);
String[] fileName = entry.getName().split("\\."); // filename split to get extension
String fileExtension = fileName.length >= 2 ? fileName[fileName.length-1]:"none"; // file extension
boolean extensionIsValid = Arrays.stream(validFileExtensions).anyMatch(fileExtension::equals); // file extension check
// Extract file
if (!entry.isDirectory() && extensionIsValid) {
log.warn("SCORM WORKFLOW: zipFilePath - " + zipFilePath);
log.warn("SCORM WORKFLOW: zipFileExtension - " + zipFileExtension);
File f = new File(entry.getName());
File f = new File(extractedFilename); //create temporary file
try (FileOutputStream fos = new FileOutputStream(f)) {
byte[] buffer = new byte[1024];
int length;
while ((length = zipIn.read(buffer)) >= 0) {
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);
am.createAsset(zipFilePath, fis, null, true);
log.warn("SCORM WORKFLOW: file extracted: " + zipFilePath);
String aemFilePath = nodePath + matchedString; // AEM DAM path with filename
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.
}
}
@ -131,7 +107,7 @@ public class ScormExtractor implements WorkflowProcess {
}
} catch (FileNotFoundException e) {
log.error(e.getMessage());
log.error(e.getStackTrace().toString());
e.printStackTrace();
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();

Loading…
Cancel
Save