Class: Cas::MediaFile
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Cas::MediaFile
- Defined in:
- app/models/cas/media_file.rb
Defined Under Namespace
Classes: UnknownFileService, UnknownPath
Instance Method Summary collapse
Instance Method Details
#site ⇒ Object
18 19 20 |
# File 'app/models/cas/media_file.rb', line 18 def site attachable.site || raise("Attachable doesn't have a Cas::Site association") end |
#url(version:, use_cdn: true) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/models/cas/media_file.rb', line 22 def url(version:, use_cdn: true) cdn = ENV.fetch("CDN_HOST", nil) if use_cdn # When `path` is present, it means no gem was used for uploads, therefore # the image has to be treat as raw URL. if path.present? if service.downcase == "s3" bucket = ENV.fetch('S3_BUCKET') region = ENV.fetch('S3_REGION', "s3") if cdn host = cdn else host = "https://s3-#{region}.amazonaws.com/#{bucket}" end [host, path].join("/").gsub(/([^:])\/\//, '\1/') else raise UnknownFileService end # Shrine gem uses `file_data` because `file` is the name we specified for # the Attachment at the top of this model. elsif JSON.parse(file_data).present? params = { public: true } params[:host] = cdn if cdn.present? # With Shrine, the default image version is :original. Other versions # are called derivatives. The `file_url` method expects a derivative # name as first argument. # # When we pass :original, it just returns `nil` because the main file is # not considered a derivative. If we had something like :larger, then # that would be the argument. url = if version.to_sym == :original file_url(params) else file_url(version, params) end # Shrine 3 returns `nil` when a derivative doesn't exist. It also # returns `nil` when we pass parameters. This is a fallback way of # avoiding any `nil` returns. However, notice that we can't pass # `params` because Shrine just ignores it and returns `nil` if it's # present (it wasn't like that in Shrine 2). url = file_url if url.blank? # Amazon S3 has URLs that include some query strings like signatures # which we don't want to include in URLs. url&.gsub(/\?.*/, "") else raise UnknownPath end end |