Skip to content

Commit

Permalink
JAVACLIENT-208: Allow to send non-ASCII character in batch upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinleturc committed Aug 19, 2020
1 parent a485f24 commit 79b902b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,28 @@ public void itCanUploadBatchFileThroughRestMapping() {
assertContentEquals("sample.jpg", blob);
}

/*
* JAVACLIENT-208
*/
@Test
public void itCanUploadFileWithNonAsciiCharacter() {
BatchUploadManager batchUploadManager = nuxeoClient.batchUploadManager();
BatchUpload batchUpload = batchUploadManager.createBatch();
assertNotNull(batchUpload);
File file = FileUtils.getResourceFileFromContext("sample.jpg");
FileBlob fileBlob = new FileBlob(file, "Ümlaut.pdf");
batchUpload = batchUpload.upload("1", fileBlob);
assertNotNull(batchUpload);

// Getting a doc and attaching the batch file
Document doc = Document.createWithName("file", "File");
doc.setPropertyValue("dc:title", "new title");
doc = nuxeoClient.repository().createDocumentByPath("/", doc);
assertNotNull(doc);
doc.setPropertyValue("file:content", batchUpload.getBatchBlob());
doc = doc.updateDocument();
assertEquals("Ümlaut.pdf", doc.getPropertyValue("file:content/name"));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -161,13 +163,15 @@ public BatchUpload upload(String fileIdx, File file, String name, String fileTyp
*/
public BatchUpload upload(String fileIdx, Blob blob) {
String filename = blob.getFilename();
String encodedFilename = encodeToAsciiForHeader(filename);
String mimeType = blob.getMimeType();
long length = blob.getContentLength();
if (chunkSize == 0) {

// Post blob
RequestBody fbody = Requests.create(blob);
BatchUpload response = fetchResponse(
api.upload(filename, length, mimeType, UPLOAD_NORMAL_TYPE, 0, 1, batchId, fileIdx, fbody));
api.upload(encodedFilename, length, mimeType, UPLOAD_NORMAL_TYPE, 0, 1, batchId, fileIdx, fbody));
response.name = filename;
response.batchId = batchId;
response.fileIdx = fileIdx;
Expand All @@ -186,7 +190,7 @@ public BatchUpload upload(String fileIdx, Blob blob) {
// Post chunk as a stream
RequestBody requestBody = RequestBody.create(MediaTypes.APPLICATION_OCTET_STREAM.toOkHttpMediaType(),
buffer, 0, bufferLength);
response = fetchResponse(api.upload(filename, length, mimeType, UPLOAD_CHUNKED_TYPE, chunkIndex,
response = fetchResponse(api.upload(encodedFilename, length, mimeType, UPLOAD_CHUNKED_TYPE, chunkIndex,
chunkNumber, batchId, fileIdx, requestBody));
chunkIndex++;
}
Expand All @@ -204,6 +208,14 @@ public BatchUpload upload(String fileIdx, Blob blob) {
}
}

protected String encodeToAsciiForHeader(String string) {
try {
return URLEncoder.encode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new NuxeoClientException("Unable to encode the header: " + string, e);
}
}

public void cancel() {
fetchResponse(api.cancel(batchId));
}
Expand Down

0 comments on commit 79b902b

Please sign in to comment.