-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally use redirects and signed cookies for blobs
- Loading branch information
Showing
21 changed files
with
293 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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
52 changes: 52 additions & 0 deletions
52
engines/bops_uploads/app/controllers/bops_uploads/blobs_controller.rb
This file contains 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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsUploads | ||
class BlobsController < ApplicationController | ||
def show | ||
serve_file(blob_path, content_type:, disposition:) | ||
rescue Errno::ENOENT | ||
head :not_found | ||
end | ||
|
||
private | ||
|
||
def blob_path | ||
@service.path_for(@blob.key) | ||
end | ||
|
||
def forcibly_serve_as_binary? | ||
ActiveStorage.content_types_to_serve_as_binary.include?(@blob.content_type) | ||
end | ||
|
||
def allowed_inline? | ||
ActiveStorage.content_types_allowed_inline.include?(@blob.content_type) | ||
end | ||
|
||
def content_type | ||
forcibly_serve_as_binary? ? ActiveStorage.binary_content_type : @blob.content_type | ||
end | ||
|
||
def disposition | ||
if forcibly_serve_as_binary? || !allowed_inline? | ||
:attachment | ||
else | ||
:inline | ||
end | ||
end | ||
|
||
def serve_file(path, content_type:, disposition:) | ||
::Rack::Files.new(nil).serving(request, path).tap do |(status, headers, body)| | ||
self.status = status | ||
self.response_body = body | ||
|
||
headers.each do |name, value| | ||
response.headers[name] = value | ||
end | ||
|
||
response.headers.except!("X-Cascade", "x-cascade") if status == 416 | ||
response.headers["Content-Type"] = content_type || DEFAULT_SEND_FILE_TYPE | ||
response.headers["Content-Disposition"] = disposition || DEFAULT_SEND_FILE_DISPOSITION | ||
end | ||
end | ||
end | ||
end |
This file contains 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
19 changes: 0 additions & 19 deletions
19
engines/bops_uploads/app/controllers/bops_uploads/redirects_controller.rb
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -1,7 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require "aws-sdk-cloudfront" | ||
|
||
module BopsUploads | ||
class Engine < ::Rails::Engine | ||
isolate_namespace BopsUploads | ||
|
||
initializer "bops_uploads.cookie_signer" do |app| | ||
config.after_initialize do | ||
key_pair_id = ENV.fetch("UPLOADS_PUBLIC_KEY_ID") do | ||
SecureRandom.alphanumeric(14).upcase | ||
end | ||
|
||
private_key = ENV.fetch("UPLOADS_PRIVATE_KEY") do | ||
OpenSSL::PKey::RSA.generate(2048).to_pem | ||
end | ||
|
||
cookie_signer = Aws::CloudFront::CookieSigner.new(key_pair_id:, private_key:) | ||
|
||
BopsUploads.key_pair_id = key_pair_id | ||
BopsUploads.private_key = OpenSSL::PKey::RSA.new(private_key) | ||
BopsUploads.cookie_signer = cookie_signer | ||
end | ||
end | ||
end | ||
end |
This file contains 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 |
---|---|---|
@@ -1,9 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.configure do |config| | ||
config.before type: :request do | ||
host!("uploads.bops.services") | ||
end | ||
end |
This file contains 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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bops_uploads_helper" | ||
|
||
RSpec.describe "Generating urls" do | ||
let(:config) { Rails.configuration } | ||
let(:document) { create(:document) } | ||
let(:blob) { document.file } | ||
let(:key) { blob.key } | ||
|
||
before do | ||
request.headers["HTTP_HOST"] = "southwark.bops.services" | ||
allow(config).to receive(:uploads_base_url).and_return("http://uploads.bops.services") | ||
end | ||
|
||
context "when use_signed_urls is false" do | ||
before do | ||
allow(config).to receive(:use_signed_urls).and_return(false) | ||
end | ||
|
||
it "generates urls with the uploads subdomain" do | ||
expect(helper.uploaded_file_url(blob)).to eq("http://uploads.bops.services/#{key}") | ||
end | ||
end | ||
|
||
context "when use_signed_urls is true" do | ||
before do | ||
allow(config).to receive(:use_signed_urls).and_return(true) | ||
end | ||
|
||
it "generates urls with the local authority subdomain" do | ||
expect(helper.uploaded_file_url(blob)).to eq("http://southwark.bops.services/files/#{key}") | ||
end | ||
end | ||
end |
Oops, something went wrong.