-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add engine for handling uploads domain
The serving is only used in development - in production it hits CloudFront.
- Loading branch information
Showing
21 changed files
with
237 additions
and
2 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
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
6 changes: 6 additions & 0 deletions
6
engines/bops_uploads/app/controllers/bops_uploads/application_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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsUploads | ||
class ApplicationController < ActionController::Base | ||
end | ||
end |
63 changes: 63 additions & 0 deletions
63
engines/bops_uploads/app/controllers/bops_uploads/files_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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsUploads | ||
class FilesController < ApplicationController | ||
before_action :set_service | ||
before_action :set_blob | ||
|
||
def show | ||
serve_file(blob_path, content_type:, disposition:) | ||
rescue Errno::ENOENT | ||
head :not_found | ||
end | ||
|
||
private | ||
|
||
def set_service | ||
@service = ActiveStorage::Blob.service | ||
end | ||
|
||
def set_blob | ||
@blob = ActiveStorage::Blob.find_by!(key: params[:key]) | ||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env ruby | ||
# This command will automatically be run when you run "rails" with Rails gems | ||
# installed from the root of your application. | ||
|
||
ENGINE_ROOT = File.expand_path("..", __dir__) | ||
ENGINE_PATH = File.expand_path("../lib/bops_uploads/engine", __dir__) | ||
APP_PATH = File.expand_path("../../../config/application", __dir__) | ||
|
||
# Set up gems listed in the Gemfile. | ||
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../Gemfile", __dir__) | ||
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"]) | ||
|
||
require "rails" | ||
|
||
require "active_model/railtie" | ||
require "active_record/railtie" | ||
require "action_controller/railtie" | ||
require "action_view/railtie" | ||
require "rails/engine/commands" |
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "bops_uploads" | ||
spec.version = "0.1.0" | ||
spec.authors = ["Unboxed Consulting Ltd"] | ||
spec.email = ["[email protected]"] | ||
spec.homepage = "https://unboxed.co/" | ||
spec.summary = "Provides the uploads facility for the BOPS system" | ||
|
||
spec.files = Dir.chdir(File.expand_path(__dir__)) do | ||
Dir["{app,config,db,lib}/**/*"] | ||
end | ||
|
||
spec.add_dependency "bops_core", "0.1.0" | ||
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
BopsUploads::Engine.routes.draw do | ||
get "/:key", to: "files#show", as: "file" | ||
end | ||
|
||
Rails.application.routes.draw do | ||
direct :uploaded_file do |blob, options| | ||
next "" if blob.blank? | ||
|
||
bops_uploads.file_url(blob.key, host: Rails.configuration.uploads_base_url) | ||
end | ||
|
||
resolve("ActiveStorage::Attachment") { |attachment, options| route_for(:uploaded_file, attachment.blob, options) } | ||
resolve("ActiveStorage::Blob") { |blob, options| route_for(:uploaded_file, blob, options) } | ||
resolve("ActiveStorage::Preview") { |preview, options| route_for(:uploaded_file, preview, options) } | ||
resolve("ActiveStorage::VariantWithRecord") { |variant, options| route_for(:uploaded_file, variant, options) } | ||
resolve("ActiveStorage::Variant") { |variant, options| route_for(:uploaded_file, variant, options) } | ||
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bops_uploads/engine" | ||
|
||
module BopsUploads | ||
class << self | ||
def env | ||
ActiveSupport::StringInquirer.new(ENV.fetch("BOPS_ENVIRONMENT", "development")) | ||
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsUploads | ||
class Engine < ::Rails::Engine | ||
isolate_namespace BopsUploads | ||
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,9 @@ | ||
# 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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bops_uploads_helper" | ||
|
||
RSpec.describe "Downloading files", show_exceptions: true do | ||
context "when a blob exists" do | ||
let(:document) { create(:document) } | ||
let(:blob) { document.file } | ||
let(:key) { blob.key } | ||
let(:path) { blob.service.path_for(blob.key) } | ||
|
||
it "returns 200 OK" do | ||
get "/#{key}" | ||
expect(response).to have_http_status(:ok) | ||
expect(response.content_type).to eq("image/png") | ||
end | ||
|
||
context "but the file is missing" do | ||
before do | ||
File.unlink(path) | ||
end | ||
|
||
it "returns 404 Not Found" do | ||
get "/#{key}" | ||
expect(response).to have_http_status(:not_found) | ||
end | ||
end | ||
end | ||
|
||
context "when a blob doesn't exist" do | ||
let(:key) { SecureRandom.base36(28) } | ||
|
||
it "returns 404 Not Found" do | ||
get "/#{key}" | ||
expect(response).to have_http_status(:not_found) | ||
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
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.configure do |config| | ||
config.around(:each, type: :request) do |example| | ||
if example.metadata.key?(:show_exceptions) | ||
begin | ||
env_config = Rails.application.env_config | ||
show_exceptions = env_config["action_dispatch.show_exceptions"] | ||
env_config["action_dispatch.show_exceptions"] = example.metadata[:show_exceptions] ? :all : :none | ||
|
||
example.run | ||
ensure | ||
env_config["action_dispatch.show_exceptions"] = show_exceptions | ||
end | ||
else | ||
example.run | ||
end | ||
end | ||
end |