Class: Cas::MediaFile

Inherits:
ApplicationRecord show all
Defined in:
app/models/cas/media_file.rb

Defined Under Namespace

Classes: UnknownFileService, UnknownPath

Instance Method Summary collapse

Instance Method Details

#siteObject



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
# 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`
  elsif JSON.parse(file_data).present?
    if cdn.present?
      file_url(version.to_sym, host: cdn, public: true)
    else
      file_url(version.to_sym, public: true).gsub(/\?.*/, "")
    end
  else
    raise UnknownPath
  end
end