Module: FileEmbedHelper

Defined in:
frontend/app/helpers/file_embed_helper.rb

Constant Summary collapse

SUPPORTED_URL_SCHEMES =
['http', 'https']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.supported_scheme?(scheme) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'frontend/app/helpers/file_embed_helper.rb', line 5

def self.supported_scheme?(scheme)
  SUPPORTED_URL_SCHEMES.include?(scheme)
end

Instance Method Details

#can_embed?(file_version) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'frontend/app/helpers/file_embed_helper.rb', line 25

def can_embed?(file_version)
  begin
    uri = URI(file_version['file_uri'])
    if %w(jpeg gif).include?(file_version['file_format_name']) &&
      uri.scheme =~ /http/ &&
      file_version['file_size_bytes'].to_i < 512001

      true
    else
      false
    end
  rescue Exception => ex
    false
  end
end

#uri_or_string(link) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'frontend/app/helpers/file_embed_helper.rb', line 9

def uri_or_string(link)
  # If `link` can be sensibly rendered as a URL, return a URL object.
  begin
    parsed = URI.parse(link)

    if FileEmbedHelper.supported_scheme?(parsed.scheme)
      # Great.  We'll take it.
      return parsed
    end
  rescue URI::InvalidURIError
  end

  # Otherwise, return the verbatim string
  link
end