-
Notifications
You must be signed in to change notification settings - Fork 6
Add file download endpoint for beacons #582
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
Open
oatkins8
wants to merge
2
commits into
main
Choose a base branch
from
feature/file-download-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+235
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| module Api | ||
| module V1 | ||
| module Beacons | ||
| class FilesController < Beacons::BaseController | ||
| include ActiveStorage::Streaming | ||
|
|
||
| def show | ||
| blob = Current.beacon.accessible_blobs.find(params[:id]) | ||
|
|
||
| if request.headers["Range"].present? | ||
| send_blob_byte_range_data(blob, request.headers["Range"]) | ||
| else | ||
| send_blob_stream(blob, disposition: :attachment) | ||
| end | ||
| rescue ActiveRecord::RecordNotFound | ||
| head :not_found | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe "Beacons Files API", type: :request do | ||
| let(:language) { create(:language) } | ||
| let(:region) { create(:region) } | ||
| let(:provider) { create(:provider) } | ||
|
|
||
| let(:beacon) do | ||
| b, @raw_key = create_beacon_with_key(language: language, region: region) | ||
| b.providers << provider | ||
| b | ||
| end | ||
|
|
||
| let(:raw_key) { beacon; @raw_key } | ||
|
|
||
| let(:topic) do | ||
| create(:topic, :with_documents, provider: provider, language: language).tap do |t| | ||
| beacon.topics << t | ||
| end | ||
| end | ||
|
|
||
| let(:blob) { topic.documents.first.blob } | ||
|
|
||
| describe "GET /api/v1/beacons/files/:id" do | ||
| context "with valid authentication and access" do | ||
| it "returns the file with correct headers" do | ||
| get "/api/v1/beacons/files/#{blob.id}", headers: beacon_auth_headers(raw_key) | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| expect(response.headers["Content-Type"]).to eq(blob.content_type) | ||
| expect(response.headers["Content-Disposition"]).to include("attachment") | ||
| expect(response.headers["Content-Length"]).to eq(blob.byte_size.to_s) | ||
| end | ||
|
|
||
| it "streams the file content" do | ||
| get "/api/v1/beacons/files/#{blob.id}", headers: beacon_auth_headers(raw_key) | ||
|
|
||
| expect(response.body).not_to be_empty | ||
| expect(response.body.bytesize).to eq(blob.byte_size) | ||
| end | ||
|
|
||
| it "includes the filename in Content-Disposition" do | ||
| get "/api/v1/beacons/files/#{blob.id}", headers: beacon_auth_headers(raw_key) | ||
|
|
||
| expect(response.headers["Content-Disposition"]).to include(blob.filename.to_s) | ||
| end | ||
| end | ||
|
|
||
| context "with Range header for resumable downloads" do | ||
| it "returns 206 Partial Content" do | ||
| get "/api/v1/beacons/files/#{blob.id}", | ||
| headers: beacon_auth_headers(raw_key).merge("Range" => "bytes=0-1023") | ||
|
|
||
| expect(response).to have_http_status(:partial_content) | ||
| end | ||
|
|
||
| it "includes Content-Range header" do | ||
| get "/api/v1/beacons/files/#{blob.id}", | ||
| headers: beacon_auth_headers(raw_key).merge("Range" => "bytes=0-1023") | ||
|
|
||
| expect(response.headers["Content-Range"]).to be_present | ||
| expect(response.headers["Content-Range"]).to match(/bytes 0-1023\/\d+/) | ||
| end | ||
|
|
||
| it "returns only the requested byte range" do | ||
| get "/api/v1/beacons/files/#{blob.id}", | ||
| headers: beacon_auth_headers(raw_key).merge("Range" => "bytes=0-99") | ||
|
|
||
| expect(response.body.bytesize).to be <= 100 | ||
| end | ||
| end | ||
|
|
||
| context "when file does not exist" do | ||
| it "returns 404" do | ||
| get "/api/v1/beacons/files/99999", headers: beacon_auth_headers(raw_key) | ||
|
|
||
| expect(response).to have_http_status(:not_found) | ||
| end | ||
| end | ||
|
|
||
| context "when beacon does not have access to the file" do | ||
| let(:other_beacon) do | ||
| b, @other_raw_key = create_beacon_with_key(language: language, region: region) | ||
| b.providers << provider | ||
| b | ||
| end | ||
|
|
||
| let(:other_raw_key) { other_beacon; @other_raw_key } | ||
|
|
||
| let(:other_topic) do | ||
| create(:topic, :with_documents, provider: provider, language: language).tap do |t| | ||
| other_beacon.topics << t | ||
| end | ||
| end | ||
|
|
||
| let(:other_blob) { other_topic.documents.first.blob } | ||
|
|
||
| it "returns 404 when requesting another beacon's file" do | ||
| get "/api/v1/beacons/files/#{other_blob.id}", headers: beacon_auth_headers(raw_key) | ||
|
|
||
| expect(response).to have_http_status(:not_found) | ||
| end | ||
| end | ||
|
|
||
| context "without authentication" do | ||
| it "returns 401" do | ||
| get "/api/v1/beacons/files/#{blob.id}" | ||
|
|
||
| expect(response).to have_http_status(:unauthorized) | ||
| end | ||
| end | ||
|
|
||
| context "with invalid authentication" do | ||
| it "returns 401" do | ||
| get "/api/v1/beacons/files/#{blob.id}", | ||
| headers: beacon_auth_headers("invalid-key") | ||
|
|
||
| expect(response).to have_http_status(:unauthorized) | ||
| end | ||
| end | ||
|
|
||
| context "with revoked beacon" do | ||
| before { beacon.revoke! } | ||
|
|
||
| it "returns 401" do | ||
| get "/api/v1/beacons/files/#{blob.id}", headers: beacon_auth_headers(raw_key) | ||
|
|
||
| expect(response).to have_http_status(:unauthorized) | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to use sql in this query? I know it is faster but it is less accessible to everyone. (Not saying don't use sql, asking the question)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I know this is in draft and not finished so ignore this ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good shout - just changed it to ActiveRecord 👍