27
27
import java .io .FileNotFoundException ;
28
28
import java .io .FileOutputStream ;
29
29
import java .io .IOException ;
30
+ import java .time .Instant ;
30
31
import java .util .ArrayList ;
31
32
import java .util .List ;
32
33
import java .util .Set ;
@@ -64,50 +65,51 @@ public String unzipImportFile(String filename) {
64
65
ServerConfigurationService serverConfigurationService = ComponentManager .get (ServerConfigurationService .class );
65
66
String repositoryPath = serverConfigurationService .getString ("samigo.answerUploadRepositoryPath" , "${sakai.home}/samigo/answerUploadRepositoryPath/" );
66
67
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
+
73
74
try (FileInputStream fileInputStream = new FileInputStream (new File (filename ))) {
74
75
byte [] data = new byte [fileInputStream .available ()];
75
76
fileInputStream .read (data , 0 , fileInputStream .available ());
76
77
77
78
File dir = new File (unzipLocation .toString ()); // directory where file would be saved
78
79
if (!dir .exists ()) {
79
80
if (!dir .mkdirs ()) {
80
- log .error ( "unable to mkdir " + dir .getPath ());
81
- }
81
+ log .warn ( "Unable to mkdir {}" , dir .getPath ());
82
+ }
82
83
}
83
84
84
- Set dirsMade = new TreeSet ();
85
+ Set < String > dirsMade = new TreeSet <> ();
85
86
try (ZipInputStream zipStream = new ZipInputStream (new ByteArrayInputStream (data ))) {
86
87
ZipEntry entry = (ZipEntry ) zipStream .getNextEntry ();
87
88
// Get the name of the imported zip file name. The value of "filename" has timestamp append to it.
88
89
String tmpName = filename .substring (filename .lastIndexOf ("/" ) + 1 );
89
90
qtiFilename = "exportAssessment.xml" ;
90
91
List <String > xmlFilenames = new ArrayList <>();
91
92
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 )) {
97
99
File d = new File (dir .getPath () + "/" + dirName );
98
100
// If it already exists as a dir, don't do anything
99
101
if (!(d .exists () && d .isDirectory ())) {
100
102
// Try to create the directory, warn if it fails
101
103
if (!d .mkdirs ()) {
102
- log .error ( "unable to mkdir {}/{}" , dir .getPath (), dirName );
103
- }
104
+ log .warn ( "Unable to mkdir {}/{}" , dir .getPath (), dirName );
105
+ }
104
106
dirsMade .add (dirName );
105
107
}
106
108
}
107
109
}
108
110
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 ()) {
111
113
try (FileOutputStream ofile = new FileOutputStream (zipEntryFile )) {
112
114
byte [] buffer = new byte [1024 * 10 ];
113
115
int bytesRead ;
@@ -129,10 +131,10 @@ public String unzipImportFile(String filename) {
129
131
NamedNodeMap namedNodeMap = fstNode .getAttributes ();
130
132
qtiFilename = namedNodeMap .getNamedItem ("href" ).getNodeValue ();
131
133
} catch (Exception e ) {
132
- log .error ( "error parsing imsmanifest.xml" );
134
+ log .warn ( "Could not parse imsmanifest.xml: {}" , e . toString () );
133
135
}
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 );
136
138
}
137
139
138
140
zipStream .closeEntry ();
@@ -149,20 +151,18 @@ public String unzipImportFile(String filename) {
149
151
}
150
152
}
151
153
}
152
- } catch (FileNotFoundException e ) {
153
- log .error (e .getMessage ());
154
154
} catch (IOException e ) {
155
- log .error (e .getMessage ());
155
+ log .warn (e .toString ());
156
156
}
157
157
158
- return unzipLocation .toString ();
158
+ return unzipLocation .toString ();
159
159
}
160
160
161
- public String setQTIFilename () {
161
+ public String getQtiFilename () {
162
162
return qtiFilename ;
163
163
}
164
-
165
- public String getQTIFilename ( ) {
166
- return qtiFilename ;
164
+
165
+ public void setQtiFilename ( String qtiFilename ) {
166
+ this . qtiFilename = qtiFilename ;
167
167
}
168
168
}
0 commit comments