2016-03-24 11:28:43 +00:00
|
|
|
module Gitlab
|
|
|
|
module Gfm
|
|
|
|
##
|
|
|
|
# Class that rewrites markdown links for uploads
|
|
|
|
#
|
2016-03-29 11:21:57 +00:00
|
|
|
# Using a pattern defined in `FileUploader` it copies files to a new
|
|
|
|
# project and rewrites all links to uploads in in a given text.
|
|
|
|
#
|
2016-03-24 11:28:43 +00:00
|
|
|
#
|
|
|
|
class UploadsRewriter
|
|
|
|
def initialize(text, source_project, _current_user)
|
|
|
|
@text = text
|
|
|
|
@source_project = source_project
|
|
|
|
@pattern = FileUploader::MARKDOWN_PATTERN
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite(target_project)
|
|
|
|
return unless @text
|
|
|
|
|
|
|
|
new_uploader = file_uploader(target_project)
|
2016-03-29 11:21:57 +00:00
|
|
|
@text.gsub(@pattern) do |markdown|
|
|
|
|
file = find_file(@source_project, $~[:secret], $~[:file])
|
|
|
|
return markdown unless file.try(:exists?)
|
2016-03-24 11:28:43 +00:00
|
|
|
|
2016-03-29 11:21:57 +00:00
|
|
|
new_uploader.store!(file)
|
2016-03-24 11:28:43 +00:00
|
|
|
new_uploader.to_h[:markdown]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-29 11:21:57 +00:00
|
|
|
def needs_rewrite?
|
|
|
|
files.any?
|
2016-03-24 11:28:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def files
|
|
|
|
referenced_files = @text.scan(@pattern).map do
|
|
|
|
find_file(@source_project, $~[:secret], $~[:file])
|
|
|
|
end
|
|
|
|
|
|
|
|
referenced_files.compact.select(&:exists?)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_file(project, secret, file)
|
|
|
|
uploader = file_uploader(project, secret)
|
|
|
|
uploader.retrieve_from_store!(file)
|
|
|
|
uploader.file
|
|
|
|
end
|
|
|
|
|
2016-03-29 11:21:57 +00:00
|
|
|
def file_uploader(project, secret = nil)
|
|
|
|
uploader = FileUploader.new(project, secret)
|
2016-03-24 11:28:43 +00:00
|
|
|
uploader.define_singleton_method(:move_to_store) { false }
|
|
|
|
uploader
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|