Override PUT so it doesn't change the file extension

Fixes #282 and HER-1907
This commit is contained in:
Alex Osborne
2019-08-28 12:46:29 +09:00
parent 5612fa249b
commit eeddfd763f
@@ -21,6 +21,8 @@
package org.archive.crawler.restlet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.LinkedList;
@@ -153,4 +155,28 @@ public class EnhDirectoryResource extends DirectoryServerResource {
getResponse().redirectSeeOther(ref);
return new EmptyRepresentation();
}
/*
* XXX: We override Restlet's default PUT behaviour (see FileClientHelper.handleFilePut) as it unhelpfully changes
* the file extension based on the content-type and there's no apparent way to disable that.
*/
@Override
public Representation put(Representation entity) throws ResourceException {
File file = new File(URI.create(getTargetUri()));
if (getTargetUri().endsWith("/") || file.isDirectory()) {
return super.put(entity);
}
boolean created = !file.exists();
try (FileOutputStream out = new FileOutputStream(file)) {
entity.write(out);
} catch (FileNotFoundException e) {
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, e);
} catch (IOException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
}
if (created) {
getResponse().setStatus(Status.SUCCESS_CREATED);
}
return new EmptyRepresentation();
}
}