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

Document and test streaming upload #84

Merged
merged 7 commits into from
May 11, 2024
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Add support for [Perspectives](https://www.sanity.io/blog/introducing-perspectives-sanity-previews) (https://github.com/balexand/sanity/pull/83).
- Support for [Perspectives](https://www.sanity.io/blog/introducing-perspectives-sanity-previews) (https://github.com/balexand/sanity/pull/83).
- Support for streaming uploads via `Sanity.upload_asset/3`. This feature came for free with the switch to `Req`.

### Removed
- (BREAKING) The `:drafts` option has been removed and passing it will result in an error. The `:perspective` option should be used instead. The default behavior is the same so you will only need to update your code if you are explicitly passing the `:drafts` option.
Expand Down
7 changes: 5 additions & 2 deletions lib/sanity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@ defmodule Sanity do
end

@doc """
Generates a request for the [asset endpoint](https://www.sanity.io/docs/http-api-assets).
Generates a request for the [asset endpoint](https://www.sanity.io/docs/http-api-assets). The
`body` argument can be a binary or iolist to upload content from memory. Or it can be an
`Enumerable`, such as a value returned by `File.stream!/2`, for streaming uploads.

## Options

Expand All @@ -398,7 +400,8 @@ defmodule Sanity do
* `creditLine` - The credit to person(s) and/or organization(s) required by the supplier of
the image to be used when published
"""
@spec upload_asset(iodata(), keyword() | map(), keyword() | map()) :: Request.t()
@spec upload_asset(iodata() | Enumerable.t(), keyword() | map(), keyword() | map()) ::
Request.t()
def upload_asset(body, opts \\ [], query_params \\ []) do
opts = NimbleOptions.validate!(opts, @asset_options_schema)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions test/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,56 @@ defmodule Sanity.MutateIntegrationTest do
end
end

describe "upload_asset" do
test "binary jpg asset", %{config: config} do
binary = File.read!("test/fixtures/danielle-stein-10OL1q7oX6c-unsplash.jpg")

assert %Sanity.Response{
body: %{
"document" => %{
"_type" => "sanity.imageAsset",
"metadata" => %{
"dimensions" => %{
"_type" => "sanity.imageDimensions",
"height" => 960,
"width" => 640
}
},
"mimeType" => "image/jpeg",
"sha1hash" => "41de39c94c974305bdda7d901cfca3102a8e6e77",
"size" => 169_633
}
}
} =
Sanity.upload_asset(binary)
|> Sanity.request!(config)
end

test "stream jpg asset", %{config: config} do
path = "test/fixtures/danielle-stein-10OL1q7oX6c-unsplash.jpg"

stream =
if System.version() |> String.split(".") |> Enum.map(&String.to_integer/1) >= [1, 16, 0] do
File.stream!(path, 2048)
else
File.stream!(path, [:raw, :read_ahead, :binary], 2048)
end

assert %Sanity.Response{
body: %{
"document" => %{
"_type" => "sanity.imageAsset",
"mimeType" => "image/jpeg",
"sha1hash" => "41de39c94c974305bdda7d901cfca3102a8e6e77",
"size" => 169_633
}
}
} =
Sanity.upload_asset(stream)
|> Sanity.request!(config)
end
end

test "timeout error", %{config: config} do
config = Keyword.put(config, :http_options, receive_timeout: 0, retry: false)

Expand Down