|
1 | 1 | package de.unirostock.sems.cbarchive.web.importer; |
2 | 2 |
|
3 | 3 | import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileOutputStream; |
4 | 6 | import java.io.IOException; |
5 | 7 | import java.io.InputStream; |
| 8 | +import java.io.OutputStream; |
6 | 9 | import java.net.URL; |
7 | 10 | import java.nio.file.Path; |
8 | 11 | import java.text.ParseException; |
|
13 | 16 | import java.util.List; |
14 | 17 | import java.util.regex.Matcher; |
15 | 18 | import java.util.regex.Pattern; |
| 19 | +import java.util.zip.ZipEntry; |
| 20 | +import java.util.zip.ZipOutputStream; |
16 | 21 |
|
17 | 22 | import javax.xml.transform.TransformerException; |
18 | 23 |
|
@@ -69,7 +74,19 @@ public GitImporter importRepo() throws ImporterException { |
69 | 74 | remoteUrl = processNzRepoLink( remoteUrl ); |
70 | 75 |
|
71 | 76 | cloneGit(); |
72 | | - buildArchive(); |
| 77 | + |
| 78 | + // check for existing manifest.xml |
| 79 | + File manifest = new File(tempDir, "manifest.xml"); |
| 80 | + if( manifest.exists() && manifest.length() > 0 ) { |
| 81 | + // there is a manifest.xml |
| 82 | + // -> zip it and check if valid CombineArchive |
| 83 | + zipArchive(); |
| 84 | + } |
| 85 | + else { |
| 86 | + // no manifest.xml found |
| 87 | + // -> build Archive from scratch |
| 88 | + buildArchive(); |
| 89 | + } |
73 | 90 |
|
74 | 91 | return this; |
75 | 92 | } |
@@ -198,6 +215,101 @@ private OmexDescription getOmexForFile( Path relativePath ) throws ImporterExcep |
198 | 215 | ); |
199 | 216 | } |
200 | 217 |
|
| 218 | + private void zipArchive() throws ImporterException { |
| 219 | + |
| 220 | + try { |
| 221 | + tempFile = File.createTempFile(Fields.TEMP_FILE_PREFIX, ".omex"); |
| 222 | + tempFile.delete(); // delete tmp file, if it exists already |
| 223 | + |
| 224 | + // do it! |
| 225 | + zipIt(tempFile, tempDir); |
| 226 | + |
| 227 | + // try to open it as CA |
| 228 | + CombineArchive archive = null; |
| 229 | + try { |
| 230 | + archive = new CombineArchive(tempFile, true); |
| 231 | + // check for errors |
| 232 | + if( archive.hasErrors() ) { |
| 233 | + List<String> errors = archive.getErrors(); |
| 234 | + StringBuilder errorString = new StringBuilder("Errors while parsing the manifest.xml:\n"); |
| 235 | + for( String error : errors ) { |
| 236 | + errorString.append("- "); |
| 237 | + errorString.append(error); |
| 238 | + errorString.append("\n"); |
| 239 | + } |
| 240 | + LOGGER.warn( errorString.toString() ); |
| 241 | + throw new ImporterException( errorString.toString() ); |
| 242 | + } |
| 243 | + |
| 244 | + } catch (IOException | JDOMException | ParseException | CombineArchiveException e) { |
| 245 | + LOGGER.error(e, "Cannot open zipped CombineArchive!"); |
| 246 | + throw new ImporterException("Cannot open zipped CombineArchive!", e); |
| 247 | + } finally { |
| 248 | + if( archive != null ) |
| 249 | + archive.close(); |
| 250 | + } |
| 251 | + |
| 252 | + } catch(IOException e) { |
| 253 | + LOGGER.error(e, "IOException while build CombineArchive from Git Repository"); |
| 254 | + throw new ImporterException(e); |
| 255 | + } |
| 256 | + |
| 257 | + } |
| 258 | + |
| 259 | + private void zipIt(File output, File baseDir) throws ImporterException { |
| 260 | + |
| 261 | + Path basePath = baseDir.toPath(); |
| 262 | + List<File> fileList = new LinkedList<File>(); |
| 263 | + OutputStream fileOutput = null; |
| 264 | + ZipOutputStream zipOutput = null; |
| 265 | + |
| 266 | + try { |
| 267 | + // list directory content |
| 268 | + generateFileList(baseDir, fileList); |
| 269 | + |
| 270 | + // prepare output streams |
| 271 | + fileOutput = new FileOutputStream(output); |
| 272 | + zipOutput = new ZipOutputStream(fileOutput); |
| 273 | + |
| 274 | + for( File file : fileList ) { |
| 275 | + // add entry |
| 276 | + ZipEntry entry = new ZipEntry( file.toPath().relativize(basePath).toString() ); |
| 277 | + zipOutput.putNextEntry(entry); |
| 278 | + |
| 279 | + // copy data |
| 280 | + InputStream entryInput = new FileInputStream(file); |
| 281 | + IOUtils.copy(entryInput, zipOutput); |
| 282 | + |
| 283 | + entryInput.close(); |
| 284 | + zipOutput.closeEntry(); |
| 285 | + } |
| 286 | + |
| 287 | + } catch (IOException e) { |
| 288 | + LOGGER.error(e, "Cannot zip folder"); |
| 289 | + throw new ImporterException("Cannot zip folder", e); |
| 290 | + } finally { |
| 291 | + IOUtils.closeQuietly(zipOutput); |
| 292 | + IOUtils.closeQuietly(fileOutput); |
| 293 | + } |
| 294 | + |
| 295 | + } |
| 296 | + |
| 297 | + private void generateFileList(File node, List<File> fileList) { |
| 298 | + |
| 299 | + // node is a file -> just add it |
| 300 | + if( node.isFile() ) { |
| 301 | + fileList.add(node); |
| 302 | + } |
| 303 | + else if( node.isDirectory() ) { |
| 304 | + // node is a directory -> dive into |
| 305 | + String[] subNode = node.list(); |
| 306 | + for( String name : subNode ) { |
| 307 | + generateFileList( new File(node, name), fileList ); |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + } |
| 312 | + |
201 | 313 | private String processNzRepoLink (String link) throws ImporterException { |
202 | 314 |
|
203 | 315 | /* |
|
0 commit comments