Commit 0614793b authored by Douwe Maan's avatar Douwe Maan

DRY up upload and download services

parent 1e927d39
...@@ -16,18 +16,7 @@ module Projects ...@@ -16,18 +16,7 @@ module Projects
uploader.download!(@url) uploader.download!(@url)
uploader.store! uploader.store!
filename = uploader.image? ? uploader.file.basename : uploader.file.filename uploader.to_h
escaped_filename = filename.gsub("]", "\\]")
markdown = "[#{escaped_filename}](#{uploader.secure_url})"
markdown.prepend("!") if uploader.image?
{
'alt' => filename,
'url' => uploader.secure_url,
'is_image' => uploader.image?,
'markdown' => markdown
}
end end
private private
......
...@@ -10,18 +10,7 @@ module Projects ...@@ -10,18 +10,7 @@ module Projects
uploader = FileUploader.new(@project) uploader = FileUploader.new(@project)
uploader.store!(@file) uploader.store!(@file)
filename = uploader.image? ? uploader.file.basename : uploader.file.filename uploader.to_h
escaped_filename = filename.gsub("]", "\\]")
markdown = "[#{escaped_filename}](#{uploader.secure_url})"
markdown.prepend("!") if uploader.image?
{
alt: filename,
url: uploader.secure_url,
is_image: uploader.image?,
markdown: markdown
}
end end
private private
......
...@@ -30,4 +30,19 @@ class FileUploader < CarrierWave::Uploader::Base ...@@ -30,4 +30,19 @@ class FileUploader < CarrierWave::Uploader::Base
def secure_url def secure_url
File.join("/uploads", @secret, file.filename) File.join("/uploads", @secret, file.filename)
end end
def to_h
filename = image? ? self.file.basename : self.file.filename
escaped_filename = filename.gsub("]", "\\]")
markdown = "[#{escaped_filename}](#{self.secure_url})"
markdown.prepend("!") if image?
{
alt: filename,
url: self.secure_url,
is_image: image?,
markdown: markdown
}
end
end end
...@@ -232,7 +232,7 @@ module Gitlab ...@@ -232,7 +232,7 @@ module Gitlab
return nil if res.nil? return nil if res.nil?
res['markdown'] res[:markdown]
end end
def build_attachment_url(rel_url) def build_attachment_url(rel_url)
......
...@@ -33,12 +33,12 @@ describe Projects::DownloadService, services: true do ...@@ -33,12 +33,12 @@ describe Projects::DownloadService, services: true do
@link_to_file = download_file(@project, url) @link_to_file = download_file(@project, url)
end end
it { expect(@link_to_file).to have_key('alt') } it { expect(@link_to_file).to have_key(:alt) }
it { expect(@link_to_file).to have_key('url') } it { expect(@link_to_file).to have_key(:url) }
it { expect(@link_to_file).to have_key('is_image') } it { expect(@link_to_file).to have_key(:is_image) }
it { expect(@link_to_file['is_image']).to be true } it { expect(@link_to_file[:is_image]).to be true }
it { expect(@link_to_file['url']).to match('rails_sample.jpg') } it { expect(@link_to_file[:url]).to match('rails_sample.jpg') }
it { expect(@link_to_file['alt']).to eq('rails_sample') } it { expect(@link_to_file[:alt]).to eq('rails_sample') }
end end
context 'a txt file' do context 'a txt file' do
...@@ -47,12 +47,12 @@ describe Projects::DownloadService, services: true do ...@@ -47,12 +47,12 @@ describe Projects::DownloadService, services: true do
@link_to_file = download_file(@project, url) @link_to_file = download_file(@project, url)
end end
it { expect(@link_to_file).to have_key('alt') } it { expect(@link_to_file).to have_key(:alt) }
it { expect(@link_to_file).to have_key('url') } it { expect(@link_to_file).to have_key(:url) }
it { expect(@link_to_file).to have_key('is_image') } it { expect(@link_to_file).to have_key(:is_image) }
it { expect(@link_to_file['is_image']).to be false } it { expect(@link_to_file[:is_image]).to be false }
it { expect(@link_to_file['url']).to match('doc_sample.txt') } it { expect(@link_to_file[:url]).to match('doc_sample.txt') }
it { expect(@link_to_file['alt']).to eq('doc_sample.txt') } it { expect(@link_to_file[:alt]).to eq('doc_sample.txt') }
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment