2015-10-06 14:09:03 +00:00
|
|
|
require 'uri'
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-10-06 14:09:03 +00:00
|
|
|
# HTML filter that "fixes" relative upload links to files.
|
|
|
|
# Context options:
|
|
|
|
# :project (required) - Current project
|
|
|
|
#
|
|
|
|
class UploadLinkFilter < HTML::Pipeline::Filter
|
|
|
|
def call
|
2016-05-10 11:46:04 +00:00
|
|
|
return doc unless project
|
|
|
|
|
2015-10-06 14:09:03 +00:00
|
|
|
doc.search('a').each do |el|
|
|
|
|
process_link_attr el.attribute('href')
|
|
|
|
end
|
|
|
|
|
|
|
|
doc.search('img').each do |el|
|
|
|
|
process_link_attr el.attribute('src')
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def process_link_attr(html_attr)
|
|
|
|
return if html_attr.blank?
|
|
|
|
|
|
|
|
uri = html_attr.value
|
|
|
|
if uri.starts_with?("/uploads/")
|
|
|
|
html_attr.value = build_url(uri).to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_url(uri)
|
2016-05-07 19:28:04 +00:00
|
|
|
File.join(Gitlab.config.gitlab.url, project.path_with_namespace, uri)
|
2015-10-06 14:09:03 +00:00
|
|
|
end
|
|
|
|
|
2016-05-10 11:46:04 +00:00
|
|
|
def project
|
|
|
|
context[:project]
|
|
|
|
end
|
|
|
|
|
2015-10-06 14:09:03 +00:00
|
|
|
# Ensure that a :project key exists in context
|
|
|
|
#
|
|
|
|
# Note that while the key might exist, its value could be nil!
|
|
|
|
def validate
|
|
|
|
needs :project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|