Skip to content

Commit 12c3894

Browse files
committed
Support decoding legacy files that don't contain filenames
JAVA-2546
1 parent 238d4a1 commit 12c3894

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

driver-async/src/test/functional/com/mongodb/async/client/gridfs/GridFSBucketSmokeTestSpecification.groovy

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import static com.mongodb.async.client.TestHelper.runSlow
4444
import static com.mongodb.async.client.gridfs.helpers.AsyncStreamHelper.toAsyncInputStream
4545
import static com.mongodb.async.client.gridfs.helpers.AsyncStreamHelper.toAsyncOutputStream
4646
import static com.mongodb.client.model.Filters.eq
47+
import static com.mongodb.client.model.Updates.unset
4748
import static org.bson.codecs.configuration.CodecRegistries.fromCodecs
4849
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries
4950

@@ -511,5 +512,47 @@ class GridFSBucketSmokeTestSpecification extends FunctionalSpecification {
511512
then:
512513
fileAsDocument.getDocument('metadata').getBinary('uuid').getType() == 4 as byte
513514
}
515+
516+
@Unroll
517+
def 'should handle missing file name data when downloading #description'() {
518+
given:
519+
def content = multiChunkString
520+
def contentBytes = content as byte[]
521+
ObjectId fileId
522+
523+
when:
524+
if (direct) {
525+
fileId = run(gridFSBucket.&uploadFromStream, 'myFile', toAsyncInputStream(content.getBytes()));
526+
} else {
527+
def outputStream = gridFSBucket.openUploadStream('myFile')
528+
run(outputStream.&write, ByteBuffer.wrap(contentBytes))
529+
run(outputStream.&close)
530+
fileId = outputStream.getObjectId()
531+
}
532+
533+
then:
534+
run(filesCollection.&count) == 1
535+
536+
when:
537+
// Remove filename
538+
run(filesCollection.&updateOne, eq('_id', fileId), unset('filename'))
539+
540+
def byteBuffer = ByteBuffer.allocate(contentBytes.length)
541+
if (direct) {
542+
run(gridFSBucket.openDownloadStream(fileId).&read, byteBuffer)
543+
} else {
544+
def outputStream = toAsyncOutputStream(byteBuffer)
545+
run(gridFSBucket.&downloadToStream, fileId, outputStream)
546+
run(outputStream.&close)
547+
}
548+
549+
then:
550+
byteBuffer.array() == contentBytes
551+
552+
where:
553+
description | direct
554+
'directly' | true
555+
'a stream' | false
556+
}
514557
}
515558

driver-core/src/main/com/mongodb/client/gridfs/codecs/GridFSFileCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public GridFSFile decode(final BsonReader reader, final DecoderContext decoderCo
6565
BsonDocument bsonDocument = bsonDocumentCodec.decode(reader, decoderContext);
6666

6767
BsonValue id = bsonDocument.get("_id");
68-
String filename = bsonDocument.getString("filename").getValue();
68+
String filename = bsonDocument.get("filename", new BsonString("")).asString().getValue();
6969
long length = bsonDocument.getNumber("length").longValue();
7070
int chunkSize = bsonDocument.getNumber("chunkSize").intValue();
7171
Date uploadDate = new Date(bsonDocument.getDateTime("uploadDate").getValue());

driver/src/test/functional/com/mongodb/client/gridfs/GridFSBucketSmokeTestSpecification.groovy

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import java.security.MessageDigest
3535

3636
import static com.mongodb.Fixture.getDefaultDatabaseName
3737
import static com.mongodb.Fixture.getMongoClient
38+
import static com.mongodb.client.model.Filters.eq
39+
import static com.mongodb.client.model.Updates.unset
3840
import static org.bson.codecs.configuration.CodecRegistries.fromCodecs
3941
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries
4042

@@ -557,4 +559,47 @@ class GridFSBucketSmokeTestSpecification extends FunctionalSpecification {
557559
then:
558560
gridFSContentBytes == contentBytes
559561
}
562+
563+
@Unroll
564+
def 'should handle missing file name data when downloading #description'() {
565+
given:
566+
def content = multiChunkString
567+
def contentBytes = content as byte[]
568+
ObjectId fileId
569+
byte[] gridFSContentBytes
570+
571+
when:
572+
if (direct) {
573+
fileId = gridFSBucket.uploadFromStream('myFile', new ByteArrayInputStream(contentBytes));
574+
} else {
575+
def outputStream = gridFSBucket.openUploadStream('myFile')
576+
outputStream.write(contentBytes)
577+
outputStream.close()
578+
fileId = outputStream.getObjectId()
579+
}
580+
581+
then:
582+
filesCollection.count() == 1
583+
584+
when:
585+
// Remove filename
586+
filesCollection.updateOne(eq('_id', fileId), unset('filename'))
587+
588+
if (direct) {
589+
gridFSContentBytes = gridFSBucket.openDownloadStream(fileId).getBytes()
590+
} else {
591+
def outputStream = new ByteArrayOutputStream(contentBytes.length)
592+
gridFSBucket.downloadToStream(fileId, outputStream)
593+
outputStream.close()
594+
gridFSContentBytes = outputStream.toByteArray()
595+
}
596+
597+
then:
598+
gridFSContentBytes == contentBytes
599+
600+
where:
601+
description | direct
602+
'directly' | true
603+
'a stream' | false
604+
}
560605
}

0 commit comments

Comments
 (0)