Skip to content

Commit

Permalink
fix: use provided content-type if any (#51) (#52)
Browse files Browse the repository at this point in the history
Co-authored-by: Maksim_Hadalau <[email protected]>
  • Loading branch information
Maxim-Gadalov and Maksim_Hadalau authored Nov 22, 2023
1 parent f30c89b commit 779063c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ public Future<?> upload(String path) {
context.getRequest()
.setExpectMultipart(true)
.uploadHandler(upload -> {
String contentType = upload.contentType();
String filename = upload.filename();
Pipe<Buffer> pipe = new PipeImpl<>(upload).endOnFailure(false);
BlobWriteStream writeStream = new BlobWriteStream(
proxy.getVertx(),
proxy.getStorage(),
filename,
absoluteFilePath);
absoluteFilePath,
contentType);
pipe.to(writeStream, result);

result.future()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static FileMetadataBase buildFileMetadata(StorageMetadata metadata) {

return switch (metadata.getType()) {
case BLOB ->
new FileMetadata(lastElement, path, metadata.getSize(), BlobStorageUtil.getContentType(lastElement));
new FileMetadata(lastElement, path, metadata.getSize(), ((BlobMetadata) metadata).getContentMetadata().getContentType());
case FOLDER, RELATIVE_PATH ->
new FolderMetadata(BlobStorageUtil.removeLeadingAndTrailingPathSeparators(lastElement),
BlobStorageUtil.removeTrailingPathSeparator(path));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ public class BlobWriteStream implements WriteStream<Buffer> {
public BlobWriteStream(Vertx vertx,
BlobStorage storage,
String fileName,
String parentPath) {
String parentPath,
String contentType) {
this.vertx = vertx;
this.storage = storage;
this.fileName = fileName;
this.parentPath = parentPath;
this.contentType = BlobStorageUtil.getContentType(fileName);
this.contentType = contentType != null ? contentType : BlobStorageUtil.getContentType(fileName);
}

@Override
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/com/epam/aidial/core/FileApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testFileUpload(Vertx vertx, VertxTestContext context) {
Checkpoint checkpoint = context.checkpoint(3);
WebClient client = WebClient.create(vertx);

FileMetadata expectedFileMetadata = new FileMetadata("file.txt", "Users/User1/files", 17, "text/plain");
FileMetadata expectedFileMetadata = new FileMetadata("file.txt", "Users/User1/files", 17, "text/custom");

Future.succeededFuture().compose((mapper) -> {
Promise<Void> promise = Promise.promise();
Expand All @@ -136,7 +136,7 @@ public void testFileUpload(Vertx vertx, VertxTestContext context) {
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.json(FileMetadata.class))
.sendMultipartForm(generateMultipartForm("file.txt", TEST_FILE_CONTENT),
.sendMultipartForm(generateMultipartForm("file.txt", TEST_FILE_CONTENT, "text/custom"),
context.succeeding(response -> {
context.verify(() -> {
assertEquals(200, response.statusCode());
Expand Down Expand Up @@ -294,7 +294,11 @@ private static BlobStorage buildFsBlobStorage(Path baseDir) {
}

private static MultipartForm generateMultipartForm(String fileName, String content) {
return MultipartForm.create().textFileUpload("attachment", fileName, Buffer.buffer(content), "text/plain");
return generateMultipartForm(fileName, content, "text/plan");
}

private static MultipartForm generateMultipartForm(String fileName, String content, String contentType) {
return MultipartForm.create().textFileUpload("attachment", fileName, Buffer.buffer(content), contentType);
}

private static String generateJwtToken(String user) {
Expand Down

0 comments on commit 779063c

Please sign in to comment.