lib: add GoFile server type. Note this isn't as useful as a direct file upload because GoFile forces clients to visit a page before accessing the file, meaning e.g. the file won't embed in a chat service.
Also included is "ByteSourceUploadable" which wraps a Guava ByteSource as a jodd-http Uploadable, so that we can implement multipart-upload services as appropriate.
This commit is contained in:
@@ -7,5 +7,8 @@
|
|||||||
<factorypathentry kind="VARJAR" id="M2_REPO/org/checkerframework/checker-compat-qual/2.5.5/checker-compat-qual-2.5.5.jar" enabled="true" runInBatchMode="false"/>
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/checkerframework/checker-compat-qual/2.5.5/checker-compat-qual-2.5.5.jar" enabled="true" runInBatchMode="false"/>
|
||||||
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/errorprone/error_prone_annotations/2.3.4/error_prone_annotations-2.3.4.jar" enabled="true" runInBatchMode="false"/>
|
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/errorprone/error_prone_annotations/2.3.4/error_prone_annotations-2.3.4.jar" enabled="true" runInBatchMode="false"/>
|
||||||
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar" enabled="true" runInBatchMode="false"/>
|
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/jodd/jodd-http/6.0.3/jodd-http-6.0.3.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/jodd/jodd-util/6.0.0/jodd-util-6.0.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
<factorypathentry kind="VARJAR" id="M2_REPO/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar" enabled="true" runInBatchMode="false"/>
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar" enabled="true" runInBatchMode="false"/>
|
||||||
</factorypath>
|
</factorypath>
|
||||||
|
@@ -27,6 +27,11 @@
|
|||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.8.6</version>
|
<version>2.8.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jodd</groupId>
|
||||||
|
<artifactId>jodd-http</artifactId>
|
||||||
|
<version>6.0.3</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
@@ -0,0 +1,46 @@
|
|||||||
|
package net.monarchpass.piecannon.impl;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.google.common.io.ByteSource;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
import jodd.http.HttpRequest;
|
||||||
|
import lombok.Data;
|
||||||
|
import net.monarchpass.piecannon.Server;
|
||||||
|
import net.monarchpass.piecannon.util.ByteSourceUploadable;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class GoFileServer implements Server {
|
||||||
|
public static final String DEFAULT_HOST = "gofile.io";
|
||||||
|
public static final String DEFAULT_API_HOST = "apiv2." + DEFAULT_HOST;
|
||||||
|
|
||||||
|
private final Gson gson = new Gson();
|
||||||
|
private final String label;
|
||||||
|
private String host = DEFAULT_HOST;
|
||||||
|
private String apiHost = DEFAULT_API_HOST;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URI upload (String name, ByteSource source) {
|
||||||
|
Response response = request(HttpRequest.get(String.format("https://%s/getServer", apiHost)));
|
||||||
|
|
||||||
|
final String serverId = (String)response.getData().get("server");
|
||||||
|
response = request(HttpRequest.post(String.format("https://%s.%s/uploadFile", serverId, host))
|
||||||
|
.form("file", new ByteSourceUploadable(name, source)));
|
||||||
|
|
||||||
|
final String code = (String)response.getData().get("code");
|
||||||
|
final URI uri = URI.create(String.format("https://%s/d/%s", host, code));
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Response request (final HttpRequest request) {
|
||||||
|
return gson.fromJson(request.send().bodyText(), Response.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Response {
|
||||||
|
public final String status;
|
||||||
|
public final Map<String, Object> data;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,54 @@
|
|||||||
|
package net.monarchpass.piecannon.util;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import com.google.common.io.ByteSource;
|
||||||
|
|
||||||
|
import jodd.http.HttpException;
|
||||||
|
import jodd.http.upload.Uploadable;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ByteSourceUploadable implements Uploadable<ByteSource> {
|
||||||
|
private final String fileName;
|
||||||
|
private final ByteSource source;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ByteSource getContent () {
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getBytes () {
|
||||||
|
try {
|
||||||
|
return source.read();
|
||||||
|
} catch (final IOException exception) {
|
||||||
|
throw new HttpException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getFileName () {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMimeType () {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSize () {
|
||||||
|
try {
|
||||||
|
return (int) source.size();
|
||||||
|
} catch (final IOException exception) {
|
||||||
|
throw new HttpException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream openInputStream () throws IOException {
|
||||||
|
return source.openStream();
|
||||||
|
}
|
||||||
|
}
|
@@ -1,18 +1,23 @@
|
|||||||
package net.monarchpass.piecannon.util;
|
package net.monarchpass.piecannon.util;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonPrimitive;
|
||||||
|
|
||||||
import net.monarchpass.piecannon.Server;
|
import net.monarchpass.piecannon.Server;
|
||||||
import net.monarchpass.piecannon.impl.FtpServer;
|
import net.monarchpass.piecannon.impl.FtpServer;
|
||||||
|
import net.monarchpass.piecannon.impl.GoFileServer;
|
||||||
import net.monarchpass.piecannon.impl.SftpServer;
|
import net.monarchpass.piecannon.impl.SftpServer;
|
||||||
|
|
||||||
public class ServerFactory implements Function<JsonObject, Server> {
|
public class ServerFactory implements Function<JsonObject, Server> {
|
||||||
public Server apply (final JsonObject object) {
|
public Server apply (final JsonObject object) {
|
||||||
final String type = object.getAsJsonPrimitive("type").getAsString();
|
final String type = object.getAsJsonPrimitive("type").getAsString();
|
||||||
if (type.equalsIgnoreCase("ftp")) {
|
if (type.equalsIgnoreCase("gofile")) {
|
||||||
|
return new GoFileServer(Optional.ofNullable(object.getAsJsonPrimitive("label")).map(JsonPrimitive::getAsString).orElse("GoFile"));
|
||||||
|
} else if (type.equalsIgnoreCase("ftp")) {
|
||||||
return makeFtpServer(object);
|
return makeFtpServer(object);
|
||||||
} else if (type.equalsIgnoreCase("sftp")) {
|
} else if (type.equalsIgnoreCase("sftp")) {
|
||||||
return makeSftpServer(object);
|
return makeSftpServer(object);
|
||||||
|
27
lib/src/test/java/net/monarchpass/piecannon/GoFileTest.java
Normal file
27
lib/src/test/java/net/monarchpass/piecannon/GoFileTest.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package net.monarchpass.piecannon;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import lombok.extern.java.Log;
|
||||||
|
import net.monarchpass.piecannon.impl.GoFileServer;
|
||||||
|
|
||||||
|
@Log
|
||||||
|
public class GoFileTest {
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void test () throws Exception {
|
||||||
|
try {
|
||||||
|
System.setProperty("https.protocols", "TLSv1.2");
|
||||||
|
final GoFileServer server = new GoFileServer("GoFileTest");
|
||||||
|
assertThat(PieCannon.testServer(server)).isTrue();
|
||||||
|
} catch (final Exception exception) {
|
||||||
|
log.log(Level.SEVERE, "", exception);
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user