diff --git a/app/controllers/projects/wikis_controller.rb b/app/controllers/projects/wikis_controller.rb index 496064c9a65..0e03956e738 100644 --- a/app/controllers/projects/wikis_controller.rb +++ b/app/controllers/projects/wikis_controller.rb @@ -12,12 +12,10 @@ class Projects::WikisController < Projects::ApplicationController def show @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 render 'show' - elsif file + elsif file = @project_wiki.find_file(params[:id], params[:version_id]) if file.on_disk? send_file file.on_disk_path, disposition: 'inline' else diff --git a/app/models/project_wiki.rb b/app/models/project_wiki.rb index 08a52782475..a8ba5efcc7c 100644 --- a/app/models/project_wiki.rb +++ b/app/models/project_wiki.rb @@ -72,6 +72,15 @@ class ProjectWiki 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) commit = commit_details(:created, message, title) diff --git a/spec/models/project_wiki_spec.rb b/spec/models/project_wiki_spec.rb index 32a82470e4f..f06a5cd4ecc 100644 --- a/spec/models/project_wiki_spec.rb +++ b/spec/models/project_wiki_spec.rb @@ -149,6 +149,40 @@ describe ProjectWiki do 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 after do destroy_page(subject.pages.first.page)