Merge pull request #6650 from lloeki/wiki_images_attachments

Added ability to serve files in wiki repository
This commit is contained in:
Dmitriy Zaporozhets 2014-07-23 20:28:03 +03:00
commit 4a1e98e4b6
3 changed files with 44 additions and 3 deletions

View file

@ -12,12 +12,10 @@ class Projects::WikisController < Projects::ApplicationController
def show def show
@page = @project_wiki.find_page(params[:id], params[:version_id]) @page = @project_wiki.find_page(params[:id], params[:version_id])
gollum_wiki = @project_wiki.wiki
file = gollum_wiki.file(params[:id], gollum_wiki.ref, true)
if @page if @page
render 'show' render 'show'
elsif file elsif file = @project_wiki.find_file(params[:id], params[:version_id])
if file.on_disk? if file.on_disk?
send_file file.on_disk_path, disposition: 'inline' send_file file.on_disk_path, disposition: 'inline'
else else

View file

@ -72,6 +72,15 @@ class ProjectWiki
end end
end end
def find_file(name, version = nil, try_on_disk = true)
version = wiki.ref if version.nil? # Gollum::Wiki#file ?
if wiki_file = wiki.file(name, version, try_on_disk)
wiki_file
else
nil
end
end
def create_page(title, content, format = :markdown, message = nil) def create_page(title, content, format = :markdown, message = nil)
commit = commit_details(:created, message, title) commit = commit_details(:created, message, title)

View file

@ -149,6 +149,40 @@ describe ProjectWiki do
end end
end end
describe '#find_file' do
before do
file = Gollum::File.new(subject.wiki)
Gollum::Wiki.any_instance.
stub(:file).with('image.jpg', 'master', true).
and_return(file)
Gollum::File.any_instance.
stub(:mime_type).
and_return('image/jpeg')
Gollum::Wiki.any_instance.
stub(:file).with('non-existant', 'master', true).
and_return(nil)
end
after do
Gollum::Wiki.any_instance.unstub(:file)
Gollum::File.any_instance.unstub(:mime_type)
end
it 'returns the latest version of the file if it exists' do
file = subject.find_file('image.jpg')
file.mime_type.should == 'image/jpeg'
end
it 'returns nil if the page does not exist' do
subject.find_file('non-existant').should == nil
end
it 'returns a Gollum::File instance' do
file = subject.find_file('image.jpg')
file.should be_a Gollum::File
end
end
describe "#create_page" do describe "#create_page" do
after do after do
destroy_page(subject.pages.first.page) destroy_page(subject.pages.first.page)