Skip to content

Commit

Permalink
tus#67 pushing persistent URL store in one commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-chase-g3ka1 committed Oct 17, 2022
1 parent 2638656 commit c7a6074
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, TusUploadFile> entry : filesWithFingerprints.getFiles().entrySet() ) {

if( entry.getValue().getFileName().equals(fileName) ) {
return true;
}
}

return false;
}

public String getFingerprintForFile(String fileName) {

unmarshall();
for(Map.Entry<String, TusUploadFile> entry : filesWithFingerprints.getFiles().entrySet() ) {

if( entry.getValue().getFileName().equals(fileName) ) {
return entry.getKey();
}
}

return null;
}

public Map<String, TusUploadFile> 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);

}

}
}
71 changes: 71 additions & 0 deletions src/main/java/io/tus/java/client/persistence/TusUploadFile.java
Original file line number Diff line number Diff line change
@@ -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 + "]";
}




}
31 changes: 31 additions & 0 deletions src/main/java/io/tus/java/client/persistence/TusUploadFileSet.java
Original file line number Diff line number Diff line change
@@ -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<String, TusUploadFile> files;

public TusUploadFileSet() {
super();
files = new HashMap<>();
}

public Map<String, TusUploadFile> getFiles() {
return files;
}


}

0 comments on commit c7a6074

Please sign in to comment.