Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX-216: Allow passing optional _id while uploading to gridfs files #217

Merged
merged 1 commit into from
Oct 23, 2023
Merged
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
8 changes: 4 additions & 4 deletions lib/mongo/grid_fs/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ defmodule Mongo.GridFs.Upload do

@doc """
Opens a stream that the application can write the contents of the file to.
The driver generates the file id.
The driver generates the file id if not provided.

User data for the 'metadata' field of the files collection document.
"""
@spec open_upload_stream(Mongo.GridFs.Bucket.t(), String.t(), BSON.document() | nil) :: UploadStream.t()
def open_upload_stream(bucket, filename, meta \\ nil) do
UploadStream.new(bucket, filename, meta)
@spec open_upload_stream(Mongo.GridFs.Bucket.t(), String.t(), BSON.document() | nil, UploadStream.file_id() | nil) :: UploadStream.t()
def open_upload_stream(bucket, filename, meta \\ nil, file_id \\ nil) do
UploadStream.new(bucket, filename, meta, file_id)
end
end
9 changes: 5 additions & 4 deletions lib/mongo/grid_fs/upload_stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ defmodule Mongo.GridFs.UploadStream do
alias Mongo.GridFs.Bucket
alias Mongo.GridFs.UploadStream

@type file_id :: BSON.ObjectId.t() | binary()
@type t :: %__MODULE__{
bucket: Bucket.t(),
id: BSON.ObjectId.t(),
id: file_id(),
filename: String.t(),
metadata: {BSON.document() | nil}
}
Expand All @@ -31,9 +32,9 @@ defmodule Mongo.GridFs.UploadStream do
@doc """
Creates a new upload stream to insert a file into the grid-fs.
"""
@spec new(Bucket.t(), String.t(), BSON.document() | nil) :: UploadStream.t()
def new(bucket, filename, metadata \\ nil) do
%UploadStream{bucket: bucket, filename: filename, id: Mongo.object_id(), metadata: metadata}
@spec new(Bucket.t(), String.t(), BSON.document() | nil, file_id() | nil) :: UploadStream.t()
def new(bucket, filename, metadata \\ nil, file_id \\ nil) do
%UploadStream{bucket: bucket, filename: filename, id: file_id || Mongo.object_id(), metadata: metadata}
end

defimpl Collectable, for: UploadStream do
Expand Down
16 changes: 16 additions & 0 deletions test/mongo/grid_fs/upload_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ defmodule Mongo.GridFs.UploadTest do
assert x == chksum
end

test "upload a text file with custom id, check download, length, meta-data and checksum", c do
src_filename = "./test/data/test.txt"
bucket = Bucket.new(c.pid, j: true, w: :majority)
chksum = calc_checksum(src_filename)
file_id = Mongo.object_id()

upload_stream = Upload.open_upload_stream(bucket, "my-example-file.txt", %{tag: "checked", chk_sum: chksum}, file_id)

File.stream!(src_filename, [], 512) |> Stream.into(upload_stream) |> Stream.run()

assert file_id == upload_stream.id

%{"metadata" => %{"tag" => "checked", "chk_sum" => x}} = Mongo.find_one(c.pid, Bucket.files_collection_name(bucket), %{_id: file_id})
assert x == chksum
end

@tag :mongo_4_2
@tag :rs_required
test "upload a text file, check download, length, meta-data and checksum transaction", c do
Expand Down
Loading