Skip to content

Commit 5e05743

Browse files
authored
SAK-44901 Samigo importing a zip with an xml file (sakaiproject#9004)
1 parent 3595b1b commit 5e05743

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

samigo/samigo-app/src/java/org/sakaiproject/tool/assessment/ui/bean/qti/XMLImportBean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void importAssessment(String uploadFile, boolean isCP, boolean isRespondu
169169
if (isCP) {
170170
ImportService importService = new ImportService();
171171
unzipLocation = importService.unzipImportFile(uploadFile);
172-
filename = unzipLocation + "/" + importService.getQTIFilename();
172+
filename = unzipLocation + "/" + importService.getQtiFilename();
173173
}
174174
try
175175
{

samigo/samigo-cp/src/java/org/sakaiproject/tool/assessment/contentpackaging/ImportService.java

+29-29
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.FileNotFoundException;
2828
import java.io.FileOutputStream;
2929
import java.io.IOException;
30+
import java.time.Instant;
3031
import java.util.ArrayList;
3132
import java.util.List;
3233
import java.util.Set;
@@ -64,50 +65,51 @@ public String unzipImportFile(String filename) {
6465
ServerConfigurationService serverConfigurationService = ComponentManager.get(ServerConfigurationService.class);
6566
String repositoryPath = serverConfigurationService.getString("samigo.answerUploadRepositoryPath", "${sakai.home}/samigo/answerUploadRepositoryPath/");
6667
StringBuilder unzipLocation = new StringBuilder(repositoryPath);
67-
log.debug("****"+unzipLocation);
68-
unzipLocation.append("/jsf/upload_tmp/qti_imports/");
69-
unzipLocation.append(AgentFacade.getAgentString());
70-
unzipLocation.append("/unzip_files/");
71-
unzipLocation.append(Long.toString(new java.util.Date().getTime()));
72-
68+
log.debug("**** {}", unzipLocation);
69+
unzipLocation.append("/jsf/upload_tmp/qti_imports/");
70+
unzipLocation.append(AgentFacade.getAgentString());
71+
unzipLocation.append("/unzip_files/");
72+
unzipLocation.append(Instant.now().toEpochMilli());
73+
7374
try (FileInputStream fileInputStream = new FileInputStream(new File(filename))) {
7475
byte[] data = new byte[fileInputStream.available()];
7576
fileInputStream.read(data, 0, fileInputStream.available());
7677

7778
File dir = new File(unzipLocation.toString()); // directory where file would be saved
7879
if (!dir.exists()) {
7980
if (!dir.mkdirs()) {
80-
log.error("unable to mkdir " + dir.getPath());
81-
}
81+
log.warn("Unable to mkdir {}", dir.getPath());
82+
}
8283
}
8384

84-
Set dirsMade = new TreeSet();
85+
Set<String> dirsMade = new TreeSet<>();
8586
try (ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(data))) {
8687
ZipEntry entry = (ZipEntry) zipStream.getNextEntry();
8788
// Get the name of the imported zip file name. The value of "filename" has timestamp append to it.
8889
String tmpName = filename.substring(filename.lastIndexOf("/") + 1);
8990
qtiFilename = "exportAssessment.xml";
9091
List<String> xmlFilenames = new ArrayList<>();
9192
while (entry != null) {
92-
String zipName = entry.getName();
93-
int ix = zipName.lastIndexOf('/');
94-
if (ix > 0) {
95-
String dirName = zipName.substring(0, ix);
96-
if (!dirsMade.contains(dirName)) {
93+
String entryName = entry.getName();
94+
String entryNameTrimmed = entryName.trim();
95+
int ix = entryName.lastIndexOf('/');
96+
if (ix > 0) {
97+
String dirName = entryName.substring(0, ix);
98+
if (!dirsMade.contains(dirName)) {
9799
File d = new File(dir.getPath() + "/" + dirName);
98100
// If it already exists as a dir, don't do anything
99101
if (!(d.exists() && d.isDirectory())) {
100102
// Try to create the directory, warn if it fails
101103
if (!d.mkdirs()) {
102-
log.error("unable to mkdir {}/{}", dir.getPath(), dirName);
103-
}
104+
log.warn("Unable to mkdir {}/{}", dir.getPath(), dirName);
105+
}
104106
dirsMade.add(dirName);
105107
}
106108
}
107109
}
108110

109-
File zipEntryFile = new File(dir.getPath() + "/" + entry.getName());
110-
if (!zipEntryFile.isDirectory()) {
111+
File zipEntryFile = new File(dir.getPath() + "/" + entryName);
112+
if (!zipEntryFile.isDirectory()) {
111113
try (FileOutputStream ofile = new FileOutputStream(zipEntryFile)) {
112114
byte[] buffer = new byte[1024 * 10];
113115
int bytesRead;
@@ -129,10 +131,10 @@ public String unzipImportFile(String filename) {
129131
NamedNodeMap namedNodeMap = fstNode.getAttributes();
130132
qtiFilename = namedNodeMap.getNamedItem("href").getNodeValue();
131133
} catch (Exception e) {
132-
log.error("error parsing imsmanifest.xml");
134+
log.warn("Could not parse imsmanifest.xml: {}", e.toString());
133135
}
134-
} else if (entry.getName() != null && entry.getName().trim().endsWith(".xml")) {
135-
xmlFilenames.add(entry.getName().trim());
136+
} else if (entryNameTrimmed.endsWith(".xml")) {
137+
xmlFilenames.add(entryNameTrimmed);
136138
}
137139

138140
zipStream.closeEntry();
@@ -149,20 +151,18 @@ public String unzipImportFile(String filename) {
149151
}
150152
}
151153
}
152-
} catch (FileNotFoundException e) {
153-
log.error(e.getMessage());
154154
} catch (IOException e) {
155-
log.error(e.getMessage());
155+
log.warn(e.toString());
156156
}
157157

158-
return unzipLocation.toString();
158+
return unzipLocation.toString();
159159
}
160160

161-
public String setQTIFilename() {
161+
public String getQtiFilename() {
162162
return qtiFilename;
163163
}
164-
165-
public String getQTIFilename() {
166-
return qtiFilename;
164+
165+
public void setQtiFilename(String qtiFilename) {
166+
this.qtiFilename = qtiFilename;
167167
}
168168
}

0 commit comments

Comments
 (0)