This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Remove dependencies httpclient and httpmime - It is no longer required to create ContentFile instances
- Loading branch information
1 parent
8fab12b
commit 3339609
Showing
6 changed files
with
151 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/de/studiocode/gofile4j/request/MultipartFormDataRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/main/java/de/studiocode/gofile4j/utils/StringUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |