From c7a6074aedc929e2f4d57e85ff00bec720c21a18 Mon Sep 17 00:00:00 2001 From: Levente Daradics Date: Mon, 17 Oct 2022 21:13:58 +0200 Subject: [PATCH 1/2] #67 pushing persistent URL store in one commit --- .../persistence/TusURLPersistentStore.java | 188 ++++++++++++++++++ .../client/persistence/TusUploadFile.java | 71 +++++++ .../client/persistence/TusUploadFileSet.java | 31 +++ 3 files changed, 290 insertions(+) create mode 100644 src/main/java/io/tus/java/client/persistence/TusURLPersistentStore.java create mode 100644 src/main/java/io/tus/java/client/persistence/TusUploadFile.java create mode 100644 src/main/java/io/tus/java/client/persistence/TusUploadFileSet.java diff --git a/src/main/java/io/tus/java/client/persistence/TusURLPersistentStore.java b/src/main/java/io/tus/java/client/persistence/TusURLPersistentStore.java new file mode 100644 index 0000000..69d281c --- /dev/null +++ b/src/main/java/io/tus/java/client/persistence/TusURLPersistentStore.java @@ -0,0 +1,188 @@ +package io.tus.java.client.persistence; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.apache.commons.io.FileUtils; + +import io.tus.java.client.TusURLStore; + +public class TusURLPersistentStore implements TusURLStore { + + private static volatile TusUploadFileSet filesWithFingerprints; + private String pathOfPersistentXmlFiles = FileUtils.getTempDirectoryPath() + java.io.File.separator + "tus-client-uploads.xml"; + + + + public TusURLPersistentStore() { + + super(); + + + unmarshall(); + marshall(); + } + + public TusURLPersistentStore(String pathOfPersistentXmlFiles) { + super(); + this.pathOfPersistentXmlFiles = pathOfPersistentXmlFiles; + unmarshall(); + } + + public void set(String fingerprint, URL url) { + + unmarshall(); + if(filesWithFingerprints.getFiles().containsKey(fingerprint)) { + + TusUploadFile uf = filesWithFingerprints.getFiles().get(fingerprint); + if(uf != null) { + uf.setUrl(url); + } + + } else { + + TusUploadFile uf = new TusUploadFile(); uf.setUrl(url); + filesWithFingerprints.getFiles().put(fingerprint, uf); + } + marshall(); + + } + + public void set(String fingerprint, URL url, String fileName) { + + unmarshall(); + if(filesWithFingerprints.getFiles().containsKey(fingerprint)) { + + TusUploadFile uf = filesWithFingerprints.getFiles().get(fingerprint); + if(uf != null) { + uf.setUrl(url); + uf.setFileName(fileName); + } + + } else { + + TusUploadFile uf = new TusUploadFile(); + uf.setUrl(url); + filesWithFingerprints.getFiles().put(fingerprint, uf); + } + marshall(); + } + + public URL get(String fingerprint) { + + unmarshall(); + TusUploadFile uf = filesWithFingerprints.getFiles().get(fingerprint); + if(uf != null) { + return uf.getUrl(); + } + return null; + } + + public TusUploadFile getTusUploadFile(String fingerprint) { + + unmarshall(); + return filesWithFingerprints.getFiles().get(fingerprint); + } + + public void remove(String fingerprint) { + + unmarshall(); + filesWithFingerprints.getFiles().remove(fingerprint); + marshall(); + } + + public boolean exists(String fingerprint) { + + unmarshall(); + return filesWithFingerprints.getFiles().containsKey(fingerprint); + } + + public boolean fileExists(String fileName) { + + unmarshall(); + for(Map.Entry entry : filesWithFingerprints.getFiles().entrySet() ) { + + if( entry.getValue().getFileName().equals(fileName) ) { + return true; + } + } + + return false; + } + + public String getFingerprintForFile(String fileName) { + + unmarshall(); + for(Map.Entry entry : filesWithFingerprints.getFiles().entrySet() ) { + + if( entry.getValue().getFileName().equals(fileName) ) { + return entry.getKey(); + } + } + + return null; + } + + public Map getMap() { + return filesWithFingerprints.getFiles(); + } + + private synchronized void unmarshall() { + + try { + + JAXBContext jaxbContext = JAXBContext.newInstance( TusUploadFileSet.class ); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + InputStream inStream = new FileInputStream( pathOfPersistentXmlFiles ); + + filesWithFingerprints = (TusUploadFileSet) jaxbUnmarshaller.unmarshal( inStream ); + } catch( JAXBException e ) { + throw new RuntimeException(e); + + } catch (FileNotFoundException e) { + filesWithFingerprints = new TusUploadFileSet(); + /* + try { + FileUtils.touch(new File(pathOfPersistentXmlFiles)); + } catch (IOException e1) { + throw new RuntimeException(e1); + }*/ + } + } + + private synchronized void marshall() { + + try { + JAXBContext jaxbContext = JAXBContext.newInstance( TusUploadFileSet.class ); + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + + File persistedFileOfFileListXml = new File( pathOfPersistentXmlFiles); + if( !persistedFileOfFileListXml.exists() ) { + + FileUtils.touch( persistedFileOfFileListXml ); + } + jaxbMarshaller.marshal(filesWithFingerprints, persistedFileOfFileListXml); + + } catch( JAXBException | IOException e ) { + throw new RuntimeException(e); + + } + + } +} diff --git a/src/main/java/io/tus/java/client/persistence/TusUploadFile.java b/src/main/java/io/tus/java/client/persistence/TusUploadFile.java new file mode 100644 index 0000000..85053be --- /dev/null +++ b/src/main/java/io/tus/java/client/persistence/TusUploadFile.java @@ -0,0 +1,71 @@ +package io.tus.java.client.persistence; + +import java.net.URL; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class TusUploadFile { + + private String fileName; + private URL url; + + public String getFileName() { + return fileName; + } + public void setFileName(String fileName) { + this.fileName = fileName; + } + public URL getUrl() { + return url; + } + public void setUrl(URL url) { + this.url = url; + } + public TusUploadFile(String fileName, URL url) { + super(); + this.fileName = fileName; + this.url = url; + } + public TusUploadFile() { + super(); + } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((fileName == null) ? 0 : fileName.hashCode()); + result = prime * result + ((url == null) ? 0 : url.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TusUploadFile other = (TusUploadFile) obj; + if (fileName == null) { + if (other.fileName != null) + return false; + } else if (!fileName.equals(other.fileName)) + return false; + if (url == null) { + if (other.url != null) + return false; + } else if (!url.equals(other.url)) + return false; + return true; + } + @Override + public String toString() { + return "TusUploadFile [fileName=" + fileName + ", url=" + url + "]"; + } + + + + +} diff --git a/src/main/java/io/tus/java/client/persistence/TusUploadFileSet.java b/src/main/java/io/tus/java/client/persistence/TusUploadFileSet.java new file mode 100644 index 0000000..d9164ad --- /dev/null +++ b/src/main/java/io/tus/java/client/persistence/TusUploadFileSet.java @@ -0,0 +1,31 @@ +package io.tus.java.client.persistence; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "upload-persistence") +@XmlAccessorType(XmlAccessType.FIELD) +public class TusUploadFileSet { + + @XmlElementWrapper(name="files") + private Map files; + + public TusUploadFileSet() { + super(); + files = new HashMap<>(); + } + + public Map getFiles() { + return files; + } + + +} From 0e21cbca8ff37f6e192d6a68d2f4a9192f070688 Mon Sep 17 00:00:00 2001 From: Levente Daradics Date: Mon, 17 Oct 2022 21:15:26 +0200 Subject: [PATCH 2/2] removed some unused comments --- .../tus/java/client/persistence/TusURLPersistentStore.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/io/tus/java/client/persistence/TusURLPersistentStore.java b/src/main/java/io/tus/java/client/persistence/TusURLPersistentStore.java index 69d281c..312f2b4 100644 --- a/src/main/java/io/tus/java/client/persistence/TusURLPersistentStore.java +++ b/src/main/java/io/tus/java/client/persistence/TusURLPersistentStore.java @@ -156,12 +156,6 @@ private synchronized void unmarshall() { } catch (FileNotFoundException e) { filesWithFingerprints = new TusUploadFileSet(); - /* - try { - FileUtils.touch(new File(pathOfPersistentXmlFiles)); - } catch (IOException e1) { - throw new RuntimeException(e1); - }*/ } }