Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tus#67 persistent url store #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this property static? Does this mean that the user can only effectively use one TusURLPersistentStore?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I don't remember why is it static. However I didn't recognize any use case for more than one TusUploadFileSet.

private String pathOfPersistentXmlFiles = FileUtils.getTempDirectoryPath() + java.io.File.separator + "tus-client-uploads.xml";



public TusURLPersistentStore() {

super();


unmarshall();
marshall();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why this marshal/unmarshall step is necessary here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I wrote it because unmarshall()/marshall() takes care of initialization too. It's probably a shortcutty way to do it.

}

public TusURLPersistentStore(String pathOfPersistentXmlFiles) {
super();
this.pathOfPersistentXmlFiles = pathOfPersistentXmlFiles;
unmarshall();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is here no marshall() compared to the other constructor?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed a custom path for a store is already initialized. Therefore no marshall(). Non existing file on custom path will generate RuntimeException.

}

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you are implementing additional methods for storing and retrieving additional information about the uploads, such as the filename. We can implement such a more capable URL store for tus-js-client as well. I think we should do the same for tus-java-client in its next major release: #78


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

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


}