Module: Diaspora::Camo

Defined in:
lib/diaspora/camo.rb

Class Method Summary collapse

Class Method Details

.from_markdown(markdown_text) ⇒ Object


6
7
8
9
10
11
12
13
14
# File 'lib/diaspora/camo.rb', line 6

def self.from_markdown(markdown_text)
  return unless markdown_text
  markdown_text = markdown_text.gsub(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/m) do |link|
    link.gsub($4, self.image_url($4))
  end
  markdown_text.gsub(/src=(['"])(.+?)\1/m) do |link|
    link.gsub($2, self.image_url($2))
  end
end

.image_url(url) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/diaspora/camo.rb', line 16

def self.image_url(url)
  return unless url
  return url unless self.url_eligible?(url)

  begin
    url = Addressable::URI.encode(Addressable::URI.unencode(url))
  rescue Addressable::URI::InvalidURIError
    return url
  end

  digest = OpenSSL::HMAC.hexdigest(
    OpenSSL::Digest.new('sha1'),
    AppConfig.privacy.camo.key,
    url
  )

  encoded_url = url.to_enum(:each_byte).map {|byte| '%02x' % byte}.join
  File.join(AppConfig.privacy.camo.root, digest, encoded_url)
end

.url_eligible?(url) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
39
40
41
# File 'lib/diaspora/camo.rb', line 36

def self.url_eligible?(url)
  return false unless url.start_with?('http', '//')
  return false if url.start_with?(AppConfig.environment.url.to_s,
                                  AppConfig.privacy.camo.root.to_s)
  true
end