Skip to content

Commit

Permalink
SAK-32218 More fixes to import from archive. (sakaiproject#4006)
Browse files Browse the repository at this point in the history
* SAK-32218 Import the expanded zip file.

The zip file import was looking in the wrong place for the files it had just expanded and so wasn’t importing the correct files.

* SAK-32218 Warn if archive folders don’t exist.

Warn at startup if a directory doesn’t exist for the archive and archive-unzip folders doesn’t exist.
  • Loading branch information
buckett authored Mar 3, 2017
1 parent 846fe87 commit b7d2569
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package org.sakaiproject.archive.impl;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -128,6 +130,13 @@ public void init() {
}

M_log.info("init(): storage path: " + m_storagePath + ", unzip path: " + m_unzipPath + ", merge filter{services="+m_filterSakaiServices+", roles="+m_filterSakaiRoles+"}");
if (!new File(m_storagePath).isDirectory()) {
M_log.warn("Failed to find directory {} please create or configure {}.", m_storagePath, "archive.storage.path");
}
if (!new File(m_unzipPath).isDirectory()) {
M_log.warn("Failed to find directory {} please create or configure {}.", m_unzipPath, "archive.unzip.path");
}

}

public void destroy() {
Expand Down Expand Up @@ -160,9 +169,9 @@ public String merge(String fileName, String siteId, String creatorId)
@Override
public String mergeFromZip(String zipFilePath, String siteId, String creatorId) {
try {
String fileName = m_siteZipper.unzipArchive(zipFilePath, m_unzipPath);
String folderName = m_siteZipper.unzipArchive(zipFilePath, m_unzipPath);
//not a lot we can do with the return value here since it always returns a string. would need a reimplementation/wrapper method to return a better value (boolean or some status)
return m_siteMerger.merge(fileName, siteId, creatorId, m_storagePath, m_filterSakaiServices, m_filteredSakaiServices, m_filterSakaiRoles, m_filteredSakaiRoles);
return m_siteMerger.merge(folderName, siteId, creatorId, m_unzipPath, m_filterSakaiServices, m_filteredSakaiServices, m_filterSakaiRoles, m_filteredSakaiRoles);
} catch (IOException e) {
M_log.error("Error merging from zip: " + e.getClass() + ":" + e.getMessage());
return "Error merging from zip: " + e.getClass() + ":" + e.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,52 @@ public void setServerConfigurationService(ServerConfigurationService service) {
}

/**
* Unzip a zip file into the unzip directory. Return the NAME of the archive so we can then merge from it. The merger knows the base dir.
* @param zipFilePath path to ZIP file
* @param m_unzipPath unzip dir for zips
* @return
* Unzip a zip file into the unzip directory. Only unzips the files that are found within the first folder
* contained in the zip archive.
* @param zipFilePath Path to ZIP file
* @param m_unzipPath Path to directory to unzip file into.
* @return The toplevel directory which the zipfile created.
* @throws IOException
*/
public String unzipArchive(String zipFilePath, String m_unzipPath) throws IOException {

log.debug("zipFilePath: " + zipFilePath);

ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();

//destination file from zip. Straight into the normal archive directory
File dest = new File(m_unzipPath, entry.getName());
log.debug("Dest: " + dest.getAbsolutePath());
// Default path from the filename.
String unzippedArchivePath = null;
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();

//destination file from zip. Straight into the normal archive directory
File dest = new File(m_unzipPath, entry.getName());
log.debug("Dest: " + dest.getAbsolutePath());

if(entry.isDirectory()) {
//create dir
if(!dest.mkdir()) {
throw new IOException("Failed to create directory "+ dest);
}
if (unzippedArchivePath == null) {
unzippedArchivePath = entry.getName();
}

if(entry.isDirectory()) {
//create dir
dest.mkdir();
} else {
//extract contents
InputStream in = zipFile.getInputStream(entry);
OutputStream out = new FileOutputStream(dest);
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
} else if (unzippedArchivePath != null && entry.getName().startsWith(unzippedArchivePath)){
//extract contents
try (InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(dest)) {
IOUtils.copy(in, out);
}
} else {
log.info("Ignoring entry: {}", entry.getName());
}
}

//get original filename, remove timestamp, add -archive
String unzippedArchivePath = StringUtils.substringAfterLast(StringUtils.substringBeforeLast(zipFile.getName(), "-") + "-archive", File.separator);

log.debug("unzippedArchivePath: " + unzippedArchivePath);

//get original filename, remove timestamp, add -archive

log.debug("unzippedArchivePath: " + unzippedArchivePath);

return unzippedArchivePath;

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.sakaiproject.archive.impl;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

public class SiteZipperTest {

private SiteZipper siteZipper;

@Before
public void setUp() {
siteZipper = new SiteZipper();
}

@Test
public void testUnzip() throws IOException, URISyntaxException {
String unzipFolder = Files.createTempDirectory("testUnzip").toString();
URL resource = getClass().getResource("/!admin-20170303050657.zip");

String zip = Paths.get(resource.toURI()).toFile().getAbsolutePath();
String folder = siteZipper.unzipArchive(zip, unzipFolder);

Assert.assertEquals("!admin-archive/", folder);
}

@Test
public void testUnzipNoFolder() throws IOException, URISyntaxException {
String unzipFolder = Files.createTempDirectory("testUnzip").toString();
URL resource = getClass().getResource("/!admin-no-folder.zip");

String zip = Paths.get(resource.toURI()).toFile().getAbsolutePath();
String folder = siteZipper.unzipArchive(zip, unzipFolder);

Assert.assertEquals(null, folder);
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit b7d2569

Please sign in to comment.