Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

feat: upload file with medatada #265

Open
wants to merge 1 commit 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
Expand Up @@ -96,7 +96,7 @@ private void appendFieldToWriter(String name, String value) {
* @param uploadFile a File to be uploaded
* @throws IOException Thrown if unable to parse the OutputStream of the connection
*/
public void addFilePart(String fieldName, File uploadFile, JSObject data) throws IOException {
public void addFilePart(String fieldName, File uploadFile, JSObject data, JSObject metadata) throws IOException {
String fileName = uploadFile.getName();
prWriter
.append(LINE_FEED)
Expand All @@ -108,9 +108,25 @@ public void addFilePart(String fieldName, File uploadFile, JSObject data) throws
.append("\"; filename=\"")
.append(fileName)
.append("\"")
.append(LINE_FEED)
.append(LINE_FEED);

if (metadata != null) {
prWriter
.append("Content-Type: application/json; charset=UTF-8")
.append(LINE_FEED)
.append(LINE_FEED)
.append(metadata.toString())
.append(LINE_FEED)
.append("--")
.append(boundary)
.append(LINE_FEED);
}

String contentType = URLConnection.guessContentTypeFromName(fileName);

prWriter
.append("Content-Type: ")
.append(URLConnection.guessContentTypeFromName(fileName))
.append(contentType != null ? contentType : "application/octet-stream")
.append(LINE_FEED)
.append(LINE_FEED);
prWriter.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ public static JSObject uploadFile(PluginCall call, Context context) throws IOExc
JSObject headers = call.getObject("headers");
JSObject params = call.getObject("params");
JSObject data = call.getObject("data");
JSObject metadata = call.getObject("metadata");
ResponseType responseType = ResponseType.parse(call.getString("responseType"));

URL url = new URL(urlString);
Expand All @@ -505,7 +506,7 @@ public static JSObject uploadFile(PluginCall call, Context context) throws IOExc
connection.setDoOutput(true);

FormUploader builder = new FormUploader(connection.getHttpConnection());
builder.addFilePart(name, file, data);
builder.addFilePart(name, file, data, metadata);
builder.finish();

return buildResponse(connection, responseType);
Expand Down
14 changes: 12 additions & 2 deletions ios/Plugin/HttpRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class HttpRequestHandler {
return output
}

private static func generateMultipartForm(_ url: URL, _ name: String, _ boundary: String, _ body: [String:Any]) throws -> Data {
private static func generateMultipartForm(_ url: URL, _ name: String, _ boundary: String, _ body: [String: Any], _ metadata: [String: Any]) throws -> Data {
let strings: [String: String] = body.compactMapValues { any in
any as? String
}
Expand All @@ -137,6 +137,15 @@ class HttpRequestHandler {
data.append(
"Content-Disposition: form-data; name=\"\(name)\"; filename=\"\(fname)\"\r\n".data(
using: .utf8)!)

if (metadata.count != 0) {
let jsonData = try JSONSerialization.data(withJSONObject: metadata, options: .prettyPrinted)
let jsonString = String(data: jsonData, encoding: String.Encoding.utf8) ?? ""
data.append("Content-Type: application/json; charset=UTF-8\r\n\r\n".data(using: .utf8)!)
data.append(jsonString.data(using: .utf8)!)
data.append("\r\n--\(boundary)\r\n".data(using: .utf8)!)
}

data.append("Content-Type: \(mimeType)\r\n\r\n".data(using: .utf8)!)
data.append(fileData)
strings.forEach { key, value in
Expand Down Expand Up @@ -206,6 +215,7 @@ class HttpRequestHandler {
let headers = (call.getObject("headers") ?? [:]) as! [String: String]
let params = (call.getObject("params") ?? [:]) as! [String: Any]
let body = (call.getObject("data") ?? [:]) as [String: Any]
let metadata = (call.getObject("metadata") ?? [:]) as [String: Any]
let responseType = call.getString("responseType") ?? "text";
let connectTimeout = call.getDouble("connectTimeout");
let readTimeout = call.getDouble("readTimeout");
Expand All @@ -230,7 +240,7 @@ class HttpRequestHandler {
let boundary = UUID().uuidString
request.setContentType("multipart/form-data; boundary=\(boundary)");

guard let form = try? generateMultipartForm(fileUrl, name, boundary, body) else { throw URLError(.cannotCreateFile) }
guard let form = try? generateMultipartForm(fileUrl, name, boundary, body, metadata) else { throw URLError(.cannotCreateFile) }

let urlRequest = request.getUrlRequest();
let task = URLSession.shared.uploadTask(with: urlRequest, from: form) { (data, response, error) in
Expand Down
4 changes: 4 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export interface HttpUploadFileOptions extends HttpOptions {
* If this option is used, filePath can be a relative path rather than absolute
*/
fileDirectory?: Directory;
/**
* Optionally, the metadata that come along with the file
*/
metadata?: any;
}

export interface HttpCookie {
Expand Down