forked from foodcoops/foodsoft
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add foodcoop scope to activestorage generated secret
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,57 @@ | ||
require 'active_storage/service/disk_service' | ||
|
||
module FoodsoftActiveStorageDiskController | ||
def self.included(base) # :nodoc: | ||
base.class_eval do | ||
def show | ||
if key = decode_verified_key | ||
# puts '--------------------------------------FoodsoftActiveStorageDiskController: ' + key[:scope], key.inspect | ||
FoodsoftConfig.select_foodcoop(key[:scope]) | ||
serve_file named_disk_service(key[:service_name]).path_for(key[:key]), content_type: key[:content_type], disposition: key[:disposition] | ||
else | ||
head :not_found | ||
end | ||
rescue Errno::ENOENT | ||
head :not_found | ||
end | ||
end | ||
end | ||
end | ||
|
||
module FoodsoftActiveStorageDiskService | ||
def self.included(base) # :nodoc: | ||
base.class_eval do | ||
def path_for(key) | ||
# puts 'FoodsoftActiveStorageDiskService------------------: '+FoodsoftConfig.scope | ||
File.join root, FoodsoftConfig.scope, folder_for(key), key | ||
end | ||
def generate_url(key, expires_in:, filename:, content_type:, disposition:) | ||
# puts 'AAAAAAAAAAAAAA generate_url SCOPE: '+FoodsoftConfig.scope | ||
# puts 'AAAAAAAAAAAAAA', ActiveStorage.verifier.inspect | ||
content_disposition = content_disposition_with(type: disposition, filename: filename) | ||
verified_key_with_expiration = ActiveStorage.verifier.generate( | ||
{ | ||
key: key, | ||
scope: FoodsoftConfig.scope, | ||
disposition: content_disposition, | ||
content_type: content_type, | ||
service_name: name | ||
}, | ||
expires_in: expires_in, | ||
purpose: :blob_key | ||
) | ||
|
||
if url_options.blank? | ||
raise ArgumentError, "Cannot generate URL for #{filename} using Disk service, please set ActiveStorage::Current.url_options." | ||
end | ||
|
||
url_helpers.rails_disk_service_url(verified_key_with_expiration, filename: filename, **url_options) | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveSupport.on_load(:after_initialize) do | ||
ActiveStorage::Service::DiskService.include FoodsoftActiveStorageDiskService | ||
ActiveStorage::DiskController.include FoodsoftActiveStorageDiskController | ||
end |