removed filepath from database, fixed various bugs with REST POST method

This commit is contained in:
2015-04-04 10:10:27 +02:00
parent 96d18a1359
commit ee2071c5fa
5 changed files with 48 additions and 23 deletions

View File

@@ -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<String> backup;
public String getFileName()
{
return String.format("%s-%s-%s.pkg.tar.xz", name, version, arch);
}
}

View File

@@ -139,7 +139,6 @@ public class Parser
}
}
data.md5sum = computeMD5(new FileInputStream(file));
data.filePath = file.getAbsolutePath();
data.fileName = file.getName();
return data;
}

View File

@@ -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);
}
}

View File

@@ -58,8 +58,8 @@ public class PacmanServiceEJB implements PacmanService
List<PkgData> 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<File> 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);

View File

@@ -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<PkgData> 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<PkgName> nquery = em.createQuery(nameQuery, PkgName.class);
nquery.setParameter("name", pkg.name.id);
List<PkgName> 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();
}
}