aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/active_storage/direct_uploads_controller.rb11
-rw-r--r--app/controllers/active_storage/disk_controller.rb38
-rw-r--r--app/controllers/active_storage/variants_controller.rb25
3 files changed, 74 insertions, 0 deletions
diff --git a/app/controllers/active_storage/direct_uploads_controller.rb b/app/controllers/active_storage/direct_uploads_controller.rb
new file mode 100644
index 0000000000..dccd864e8d
--- /dev/null
+++ b/app/controllers/active_storage/direct_uploads_controller.rb
@@ -0,0 +1,11 @@
+class ActiveStorage::DirectUploadsController < ActionController::Base
+ def create
+ blob = ActiveStorage::Blob.create_before_direct_upload!(blob_args)
+ render json: { url: blob.url_for_direct_upload, sgid: blob.to_sgid.to_param }
+ end
+
+ private
+ def blob_args
+ params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, :metadata).to_h.symbolize_keys
+ end
+end
diff --git a/app/controllers/active_storage/disk_controller.rb b/app/controllers/active_storage/disk_controller.rb
new file mode 100644
index 0000000000..16a295d00d
--- /dev/null
+++ b/app/controllers/active_storage/disk_controller.rb
@@ -0,0 +1,38 @@
+require "action_controller"
+require "active_storage/blob"
+require "active_storage/verified_key_with_expiration"
+
+require "active_support/core_ext/object/inclusion"
+
+# This controller is a wrapper around local file downloading. It allows you to
+# make abstraction of the URL generation logic and to serve files with expiry
+# if you are using the +Disk+ service.
+#
+# By default, mounting the Active Storage engine inside your application will
+# define a +/rails/blobs/:encoded_key/*filename+ route that will reference this
+# controller's +show+ action and will be used to serve local files.
+#
+# A URL for an attachment can be generated through its +#url+ method, that
+# will use the aforementioned route.
+class ActiveStorage::DiskController < ActionController::Base
+ def show
+ if key = decode_verified_key
+ blob = ActiveStorage::Blob.find_by!(key: key)
+
+ if stale?(etag: blob.checksum)
+ send_data blob.download, filename: blob.filename, type: blob.content_type, disposition: disposition_param
+ end
+ else
+ head :not_found
+ end
+ end
+
+ private
+ def decode_verified_key
+ ActiveStorage::VerifiedKeyWithExpiration.decode(params[:encoded_key])
+ end
+
+ def disposition_param
+ params[:disposition].presence_in(%w( inline attachment )) || "inline"
+ end
+end
diff --git a/app/controllers/active_storage/variants_controller.rb b/app/controllers/active_storage/variants_controller.rb
new file mode 100644
index 0000000000..dde7e1458f
--- /dev/null
+++ b/app/controllers/active_storage/variants_controller.rb
@@ -0,0 +1,25 @@
+class ActiveStorage::VariantsController < ActionController::Base
+ def show
+ if blob_key = decode_verified_blob_key
+ redirect_to processed_variant_for(blob_key).url(disposition: disposition_param)
+ else
+ head :not_found
+ end
+ end
+
+ private
+ def decode_verified_blob_key
+ ActiveStorage::VerifiedKeyWithExpiration.decode(params[:encoded_blob_key])
+ end
+
+ def processed_variant_for(blob_key)
+ ActiveStorage::Variant.new(
+ ActiveStorage::Blob.find_by!(key: blob_key),
+ ActiveStorage::Variation.decode(params[:variation_key])
+ ).processed
+ end
+
+ def disposition_param
+ params[:disposition].presence_in(%w( inline attachment )) || 'inline'
+ end
+end