Browse Source

Additional unzip code

update pom.xml
master
Larry L 1 year ago
parent
commit
9ea1b56015
  1. 3
      .vscode/settings.json
  2. 66
      core/src/main/java/com/mcaps/core/workflows/ScormExtractor.java
  3. 2
      pom.xml

3
.vscode/settings.json vendored

@ -0,0 +1,3 @@
{
"java.compile.nullAnalysis.mode": "automatic"
}

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

@ -6,14 +6,11 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import javax.jcr.Node; import org.apache.sling.api.resource.Resource;
import javax.jcr.Session; import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Constants; import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.Reference;
@ -25,12 +22,9 @@ import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem; import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess; import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap; import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.dam.api.Asset;
import com.day.cq.search.PredicateGroup; import com.day.cq.dam.api.AssetManager;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder; import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
@Component(property = { @Component(property = {
Constants.SERVICE_DESCRIPTION + "=Extracts a SCORM content package and puts it somewhere", Constants.SERVICE_DESCRIPTION + "=Extracts a SCORM content package and puts it somewhere",
@ -40,51 +34,65 @@ import com.day.cq.search.result.SearchResult;
public class ScormExtractor implements WorkflowProcess { 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;
@Reference @Reference
QueryBuilder queryBuilder; QueryBuilder queryBuilder;
@Override @Override
public void execute(WorkItem arg0, WorkflowSession arg1, MetaDataMap arg2) throws WorkflowException { public void execute(WorkItem wi, WorkflowSession ws, MetaDataMap mdm) throws WorkflowException {
log.warn("SCORM WORKFLOW: MetaDataMap args: " + arg2.get("PROCESS_ARGS", "string").toString()); resourceResolver = ws.adaptTo(ResourceResolver.class);
String[] params = arg2.get("PROCESS_ARGS", "string").toString().split(",");
String attachmentsPath = params[0]; log.warn("SCORM WORKFLOW: MetaDataMap args: " + mdm.get("PROCESS_ARGS", "string").toString());
String[] params = mdm.get("PROCESS_ARGS", "string").toString().split(",");
//String attachmentsPath = params[0];
String saveToLocation = params[1]; String saveToLocation = params[1];
log.warn("SCORM WORKFLOW: payload path: " + arg0.getContentPath());
String payloadPath = arg0.getWorkflowData().getPayload().toString(); log.warn("SCORM WORKFLOW: payload path: " + wi.getContentPath());
Map<String, String> map = new HashMap<String, String> (); String payloadPath = wi.getWorkflowData().getPayload().toString();
map.put("path", payloadPath + "/" + attachmentsPath); unzipFile(payloadPath, saveToLocation);
unzipFile(saveToLocation, payloadPath);
//File saveLocationFolder = new File(saveToLocation);
//if (!saveLocationFolder.exists()) {
// saveLocationFolder.mkdirs();
//}
map.put("type", "nt:file");
//throw new UnsupportedOperationException("Unimplemented method 'execute'");
} }
private void unzipFile(String filePath, String outputFolder) { private void unzipFile(String filePath, String outputFolder) {
try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(filePath))) { log.warn("SCORM WORKFLOW: inside unzipFile: filePath - " + filePath);
log.warn("SCORM WORKFLOW: inside unzipFile: outputFolder - " + outputFolder);
Resource zipfile = resourceResolver.getResource(filePath);
AssetManager am = resourceResolver.adaptTo(AssetManager.class);
Asset a = zipfile.adaptTo(Asset.class);
log.warn("SCORM WORKFLOW: Asset - " + a);
InputStream is = a.getOriginal().getStream();
//NOTE: for some reason 'a' is returning NULL
try (ZipInputStream zipIn = new ZipInputStream(is)) {
ZipEntry entry = zipIn.getNextEntry(); ZipEntry entry = zipIn.getNextEntry();
while (entry != null) { while (entry != null) {
String zipFilePath = outputFolder + File.separator + entry.getName(); String zipFilePath = outputFolder + File.separator + entry.getName();
log.warn("SCORM WORKFLOW: zipFilePath: " + zipFilePath); // Extract file
if (!entry.isDirectory()) { if (!entry.isDirectory()) {
try (FileOutputStream fos = new FileOutputStream(filePath)) { File f = new File(entry.getName());
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);
} }
InputStream fis = new FileInputStream(f);
am.createAsset(zipFilePath, fis, null, true);
log.warn("SCORM WORKFLOW: file extracted: " + zipFilePath);
} }
} }
zipIn.closeEntry(); zipIn.closeEntry();
entry = zipIn.getNextEntry(); entry = zipIn.getNextEntry();
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
log.error(e.getMessage());
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }

2
pom.xml

@ -37,7 +37,7 @@
</modules> </modules>
<properties> <properties>
<aem.host>localhost</aem.host> <aem.host>192.168.248.22</aem.host>
<aem.port>4502</aem.port> <aem.port>4502</aem.port>
<aem.publish.host>localhost</aem.publish.host> <aem.publish.host>localhost</aem.publish.host>
<aem.publish.port>4503</aem.publish.port> <aem.publish.port>4503</aem.publish.port>

Loading…
Cancel
Save