diff --git a/src/main/java/model/PkgData.java b/src/main/java/model/PkgData.java index 478754c..340bfba 100644 --- a/src/main/java/model/PkgData.java +++ b/src/main/java/model/PkgData.java @@ -43,8 +43,6 @@ public class PkgData public String md5sum; - public String filePath; - public String fileName; @ElementCollection(fetch = FetchType.EAGER) @@ -70,10 +68,4 @@ public class PkgData @ElementCollection(fetch = FetchType.EAGER) public List backup; - - public String getFileName() - { - return String.format("%s-%s-%s.pkg.tar.xz", name, version, arch); - } - } diff --git a/src/main/java/pacbase/Parser.java b/src/main/java/pacbase/Parser.java index f6f5e30..6280517 100644 --- a/src/main/java/pacbase/Parser.java +++ b/src/main/java/pacbase/Parser.java @@ -139,7 +139,6 @@ public class Parser } } data.md5sum = computeMD5(new FileInputStream(file)); - data.filePath = file.getAbsolutePath(); data.fileName = file.getName(); return data; } diff --git a/src/main/java/service/ApplicationContext.java b/src/main/java/service/ApplicationContext.java index 11d4806..ae02262 100644 --- a/src/main/java/service/ApplicationContext.java +++ b/src/main/java/service/ApplicationContext.java @@ -1,6 +1,9 @@ package service; +import model.PkgData; + import javax.enterprise.context.ApplicationScoped; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -15,6 +18,8 @@ public class ApplicationContext { private Properties systemProperties; + private String repoFolder; + public ApplicationContext() { systemProperties = new Properties(); @@ -26,6 +31,8 @@ public class ApplicationContext // load a properties file systemProperties.load(input); // get the property value and print it out + systemProperties.getProperty("RepoFolder"); + repoFolder = systemProperties.getProperty("RepoFolder"); } catch (IOException ex) { throw new RuntimeException(ex); @@ -48,4 +55,9 @@ public class ApplicationContext { return systemProperties; } + + public File getFile(PkgData pkg) + { + return new File(new File(repoFolder), pkg.fileName); + } } diff --git a/src/main/java/service/PacmanServiceEJB.java b/src/main/java/service/PacmanServiceEJB.java index da18227..8d53ece 100644 --- a/src/main/java/service/PacmanServiceEJB.java +++ b/src/main/java/service/PacmanServiceEJB.java @@ -58,8 +58,8 @@ public class PacmanServiceEJB implements PacmanService List listaDB = em.createQuery("SELECT p FROM PkgData p", PkgData.class).getResultList(); for (PkgData p : listaDB) { - knownPkg.add(p.filePath); - File file = new File(p.filePath); + knownPkg.add(p.fileName); + File file = ctx.getFile(p); if (!file.exists()) { log.log(Level.INFO, String.format("Removing package %s", file.getName())); @@ -76,15 +76,15 @@ public class PacmanServiceEJB implements PacmanService Collection ls = FileUtils.listFiles(new File(ctx.getSystemProperties().getProperty("RepoFolder")), new RegexFileFilter(".*\\.pkg\\.tar\\.xz"), DirectoryFileFilter.DIRECTORY); try { - ut.begin(); for (File file : ls) { if (!knownPkg.contains(file.getAbsolutePath())) { + ut.begin(); parseFile(file); + ut.commit(); } } - ut.commit(); } catch (Exception e) { throw new RuntimeException(e); diff --git a/src/main/java/service/PacmanWebService.java b/src/main/java/service/PacmanWebService.java index dc7e5e7..e98319a 100644 --- a/src/main/java/service/PacmanWebService.java +++ b/src/main/java/service/PacmanWebService.java @@ -2,6 +2,7 @@ package service; import model.PkgData; import model.PkgList; +import model.PkgName; import org.apache.commons.io.IOUtils; import pacbase.Parser; import persistence.QueryEngine; @@ -13,6 +14,8 @@ import javax.ws.rs.*; import javax.ws.rs.core.*; import java.io.*; import java.net.URI; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.List; import java.util.logging.Logger; @@ -105,7 +108,7 @@ public class PacmanWebService PkgData pkg = fnquery.getSingleResult(); StreamingOutput stream = (OutputStream output) -> { - FileInputStream input = new FileInputStream(pkg.filePath); + FileInputStream input = new FileInputStream(ctx.getFile(pkg)); try { int bytes; while ((bytes = input.read()) != -1) { @@ -118,7 +121,7 @@ public class PacmanWebService if (input != null) input.close(); } }; - return Response.ok(stream).header("Content-Length", new File(pkg.filePath).length()).build(); + return Response.ok(stream).header("Content-Length", ctx.getFile(pkg).length()).build(); } catch (NonUniqueResultException e) { @@ -142,7 +145,12 @@ public class PacmanWebService if (filename == null) throw new BadRequestException(); - String hash = Parser.computeMD5(input); + File file = new File("/tmp/"/*ctx.getSystemProperties().getProperty("RepoFolder")*/, filename); + FileOutputStream fos = new FileOutputStream(file); + IOUtils.copy(input, fos); + fos.close(); + FileInputStream fis = new FileInputStream(file); + String hash = Parser.computeMD5(fis); TypedQuery hquery = em.createQuery(hashQuery, PkgData.class); hquery.setParameter("md5sum", hash); @@ -150,18 +158,32 @@ public class PacmanWebService if (savedFiles.size() > 0) { - return null; + file.delete(); + return Response.notModified().build(); } else { - input.reset(); - File file = new File("/tmp/sdf"/*ctx.getSystemProperties().getProperty("RepoFolder")*/, filename); - FileOutputStream fos = new FileOutputStream(file); - IOUtils.copy(input,fos); - fos.close(); PkgData pkg = Parser.parseFile(file); + TypedQuery nquery = em.createQuery(nameQuery, PkgName.class); + nquery.setParameter("name", pkg.name.id); + List savedName = nquery.getResultList(); + if (savedName.size() > 0) + { + pkg.name = savedName.get(0); + } + File destinationFile = new File(ctx.getSystemProperties().getProperty("RepoFolder"),filename); + try + { + Files.move(file.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + catch (Exception e) + { + file.delete(); + throw e; + } + em.persist(pkg); - URI pkgUri = uriInfo.getAbsolutePathBuilder().path(pkg.filePath).build(); + URI pkgUri = uriInfo.getAbsolutePathBuilder().path(pkg.fileName).build(); return Response.created(pkgUri).build(); } }