Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
MultipartFormDataRequest
Browse files Browse the repository at this point in the history
- Remove dependencies httpclient and httpmime
- It is no longer required to create ContentFile instances
  • Loading branch information
NichtStudioCode committed Aug 5, 2020
1 parent 8fab12b commit 3339609
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 73 deletions.
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,9 @@ A java wrapper for the <https://gofile.io> API

## Uploading files

### A quick way to upload files
Create a new instance of GoFile4j and provide the Files you want to upload
```java
GoFile4j.uploadFiles(new File("filename.txt"), new File("filename1.txt"));
```

### Normal way to upload files
Create a ContentFile - a file and the corresponding content type (example: image.png would be ```ContentType.IMAGE_PNG```)
The right content type is not necessary for the upload to work. However, images for example won't have a preview on the download page if they don't have the right content type.
```java
ContentFile file = new ContentFile(new File("filename.txt"), ContentType.TEXT_PLAIN);
```
Create a new instance of GoFile4j and provide the ContentFiles you want to upload
```java
GoFile4j goFile4j = new GoFile4j(file);
GoFile4j goFile4j = new GoFile4j(file, file1);
```
optional: set these values
```java
Expand Down
12 changes: 1 addition & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.studiocode</groupId>
<artifactId>GoFile4j</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1</version>
<build>
<plugins>
<plugin>
Expand All @@ -20,16 +20,6 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/de/studiocode/gofile4j/ContentFile.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
package de.studiocode.gofile4j;

import org.apache.http.entity.ContentType;

import java.io.File;
import java.net.URLConnection;

/**
* A class for a file and the corresponding content type (example: image.png would be ContentType.IMAGE_PNG)
* A class for a file and the corresponding content type
* The right content type is not necessary for the upload to work. However, images for example won't have a preview on the download page if they don't have the right content type.
*/
public class ContentFile {
private ContentType contentType;
private File file;

private final File file;
private final String contentType;

/**
* Create an instance of ContentFile without specifying the content type. Default value: DEFAULT_BINARY (APPLICATION_OCTET_STREAM)
* Create an instance of ContentFile without specifying the content type. The content type will be guessed using URLConnection.guessContentTypeFromName
*
* @param file The file
*/
public ContentFile(File file) {
public ContentFile(File file) {
this.file = file;
this.contentType = ContentType.DEFAULT_BINARY;
this.contentType = URLConnection.guessContentTypeFromName(file.getName());
}

/**
* Create an instance of ContentFile
* @param file The file
*
* @param file The file
* @param contentType The content type
*/
public ContentFile(File file, ContentType contentType) {
this(file);
public ContentFile(File file, String contentType) {
this.file = file;
this.contentType = contentType;
}

public ContentType getContentType() {
return contentType;
}

public File getFile() {
return file;
}

public String getContentType() {
return contentType;
}
}
54 changes: 21 additions & 33 deletions src/main/java/de/studiocode/gofile4j/GoFile4j.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package de.studiocode.gofile4j;

import de.studiocode.gofile4j.request.MultipartFormDataRequest;
import de.studiocode.gofile4j.response.FileUploadResponse;
import de.studiocode.gofile4j.response.FindServerResponse;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
Expand All @@ -31,23 +29,17 @@ public class GoFile4j {

private final ContentFile[] files;
private final String fileServer;
private final HttpPost postReq;
private final MultipartFormDataRequest request;


public GoFile4j(ContentFile... files) throws IOException {
this.files = files;
fileServer = getFileServer();
this.postReq = new HttpPost(UPLOAD_URL_FORMAT.format(fileServer));
this.fileServer = getFileServer();
this.request = new MultipartFormDataRequest(UPLOAD_URL_FORMAT.format(fileServer), StandardCharsets.UTF_8);
}

/**
* A quick method to upload files
*
* @param files The files to upload
* @return The file upload result
*/
public static FileUploadResult uploadFiles(File... files) throws IOException {
return new GoFile4j(Arrays.stream(files).map(ContentFile::new).toArray(ContentFile[]::new)).upload();

public GoFile4j(File... files) throws IOException {
this(Arrays.stream(files).map(ContentFile::new).toArray(ContentFile[]::new));
}

/**
Expand All @@ -56,9 +48,8 @@ public static FileUploadResult uploadFiles(File... files) throws IOException {
* @return The name of the best server available to receive uploads
*/
private String getFileServer() throws IOException {
HttpGet getReq = new HttpGet(FIND_SERVER_URL);
InputStream in = HttpClients.createDefault().execute(getReq).getEntity().getContent();
return new FindServerResponse(in).getServer();
HttpURLConnection findServer = (HttpURLConnection) new URL(FIND_SERVER_URL).openConnection();
return new FindServerResponse(findServer.getInputStream()).getServer();
}

/**
Expand All @@ -67,21 +58,18 @@ private String getFileServer() throws IOException {
* @return The file upload result
*/
public FileUploadResult upload() throws IOException {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
if (email != null) request.addFormField("email", email);
if (description != null) request.addFormField("description", description);
if (password != null) request.addFormField("password", password);
if (tags != null) request.addFormField("tags", tags);
if (expire != null) request.addFormField("expire", expire);

for (ContentFile file : files) {
File f = file.getFile();
builder.addBinaryBody("filesUploaded", f, file.getContentType(), f.getName());
request.addFormFile("filesUploaded", f, f.getName(), file.getContentType());
}
if (email != null) builder.addTextBody("email", email);
if (description != null) builder.addTextBody("description", description);
if (password != null) builder.addTextBody("password", password);
if (tags != null) builder.addTextBody("tags", tags);
if (expire != null) builder.addTextBody("expire", expire);
HttpEntity multipart = builder.build();
postReq.setEntity(multipart);

InputStream content = HttpClients.createDefault().execute(postReq).getEntity().getContent();
FileUploadResponse response = new FileUploadResponse(content);

FileUploadResponse response = new FileUploadResponse(request.complete());
return new FileUploadResult(response);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package de.studiocode.gofile4j.request;

import de.studiocode.gofile4j.utils.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class MultipartFormDataRequest {

private final String boundary = StringUtils.randomString(25, StringUtils.ALPHABET);
private final Charset charset;
private final HttpURLConnection connection;
private final OutputStream out;
private final PrintWriter writer;

public MultipartFormDataRequest(String requestUrl, Charset charset) throws IOException {
this.charset = charset;
connection = (HttpURLConnection) new URL(requestUrl).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

out = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(out, charset));
}

public void addHeaderField(String name, String value) {
writer.println(name + ": " + value);
writer.flush();
}

public void addFormField(String name, String value) {
writer.println("--" + boundary);
writer.println("Content-Disposition: form-data; name=" + name);
writer.println("Content-Type: text/plain; charset=" + charset.toString());
writer.println();
writer.println(value);
writer.flush();
}

public void addFormFile(String fieldName, File file, String fileName, String contentType) throws IOException {
writer.println("--" + boundary);
writer.println("Content-Disposition: form-data; name=" + fieldName + "; filename=" + fileName);
writer.println("Content-Type: " + contentType);
writer.println("Content-Transfer-Encoding: binary");
writer.println();
writer.flush();

InputStream in = new FileInputStream(file);
copy(in, out);
out.flush();
in.close();

writer.println();
writer.flush();
}

public InputStream complete() throws IOException {
writer.flush();
writer.println("--" + boundary + "--");
writer.close();

int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
return connection.getInputStream();
} else throw new IOException("Server responded with non-OK status: " + responseCode);
}

public List<String> completeToStringList() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(complete(), charset));

List<String> lines = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}

return lines;
}

private void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[8192];
int length;
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
}

}
18 changes: 18 additions & 0 deletions src/main/java/de/studiocode/gofile4j/utils/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.studiocode.gofile4j.utils;

import java.util.Random;

public class StringUtils {

public static final String ALPHABET = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final Random RANDOM = new Random();

public static String randomString(int length, String letters) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i != length; i++) {
builder.append(letters.charAt(RANDOM.nextInt(letters.length())));
}
return builder.toString();
}

}

0 comments on commit 3339609

Please sign in to comment.