This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit 03095a91 authored by Michael Breu's avatar Michael Breu :speech_balloon:
Browse files

Correcting Repackaging of zip file

parent dd65d1a6
Branches
1 merge request!17Initial Merge to Prepare Release 1.0.0
package at.ac.uibk.gitsearch.service;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.codeability.sharing.plugins.api.ShoppingBasket;
......@@ -18,6 +26,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
......@@ -242,11 +252,62 @@ public class ShoppingBasketService {
return ex;
}
public InputStream getRepositoryZip(String basketToken, int position) throws GitLabApiException {
public InputStream getRepositoryZip(String basketToken, int position) throws GitLabApiException, IOException {
final @Nullable ExtendedShoppingBasket basketInfo = basketCache.getIfPresent(basketToken);
ShoppingBasket basket = getBasket( basketToken);
final GitLabApi gitLabApi = basketInfo==null?gitLabRepository.getGitLabApi(Optional.empty()):gitLabRepository.getGitLabApi(basketInfo.getGitLabAccessInfo());
return gitLabApi.getRepositoryApi().getRepositoryArchive(basket.exerciseInfo[position].gitLabProjectId, "HEAD", "zip");
return rePackageGitLabProjectZip(new ZipInputStream(gitLabApi.getRepositoryApi().getRepositoryArchive(basket.exerciseInfo[position].gitLabProjectId, "HEAD", "zip")), "from project " + basket.exerciseInfo[position].gitLabProjectId);
}
/**
* chops of the main folder, and brings every file one level up. Also deleting
* all plain files in main folder.
*
* @param zipIn the zip to be repackaged
* @param originalLocation the original location. For debug purposes only.
* @return
* @throws IOException
*/
protected InputStream rePackageGitLabProjectZip(ZipInputStream zipIn, String originalLocation) throws IOException {
final PipedInputStream pis = new PipedInputStream(1024*256);
final PipedOutputStream pos = new PipedOutputStream(pis);
Runnable rePackage = () -> {
try (ZipOutputStream zipOut = new ZipOutputStream(pos)) {
ZipEntry zipInEntry = zipIn.getNextEntry();
String prefix = "";
if (zipInEntry.isDirectory()) { // main directory is prefix to all entries
prefix = zipInEntry.getName();
}
while (zipInEntry != null) {
if (zipInEntry.getName().startsWith(prefix)) {
String newName = zipInEntry.getName().substring(prefix.length());
if (!StringUtils.isEmpty(newName)) {
ZipEntry zipOutEntry = new ZipEntry(newName);
if (zipInEntry.isDirectory()) {
// log.debug("ignoring directory {}", zipInEntry.getName());
zipOut.putNextEntry(zipOutEntry);
} else {
zipOut.putNextEntry(zipOutEntry);
StreamUtils.copy(zipIn, zipOut);
zipOut.closeEntry();
}
}}
zipInEntry = zipIn.getNextEntry();
}
zipIn.close();
zipOut.close();
} catch (IOException e) {
log.error("Cannot rezip file from {}", originalLocation);
}
};
new Thread(rePackage).start();
return pis;
}
public final static Duration BASKET_EXPIRY_INTERVAL = Duration.ofMinutes(10);
......
package at.ac.uibk.gitsearch.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import at.ac.uibk.gitsearch.GitsearchApp;
@SpringBootTest(classes = GitsearchApp.class)
public class ShoppingBasketServiceIT {
private static final String TEST_ZIP_LOCATION = "at/ac/uibk/gitsearch/service/testData/junit-quality-tests-exercise-master.zip";
@Autowired
protected ShoppingBasketService shoppingBasketService;
private final Logger log = LoggerFactory.getLogger(ShoppingBasketServiceIT.class);
@Test
public void testRepackage() throws IOException {
final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream(TEST_ZIP_LOCATION);
final ZipInputStream testZip = new ZipInputStream(resourceStream);
final InputStream testRepackagedZipIS = shoppingBasketService.rePackageGitLabProjectZip(testZip, "from " + TEST_ZIP_LOCATION);
final ZipInputStream testRepackagedZip = new ZipInputStream(testRepackagedZipIS);
ZipEntry entry = testRepackagedZip.getNextEntry();
while(entry!=null) {
log.info("found entry {} (directory: {})", entry.getName(), entry.isDirectory());
Assert.assertTrue("original folder not chopped off?", !entry.getName().startsWith("junit-quality-tests-exercise-master"));
entry = testRepackagedZip.getNextEntry();
}
testRepackagedZip.close();
}
}
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment