Commit e640481625a0d9076572b9431052a179a606e716

Authored by Dmitriy Zaporozhets
2 parents 478d6bd4 00cd3ecc

Merge branch 'files_in_wiki' into 'master'

Add support to show files from wiki repository on wiki pages
app/controllers/projects/wikis_controller.rb
@@ -12,9 +12,22 @@ class Projects::WikisController < Projects::ApplicationController @@ -12,9 +12,22 @@ class Projects::WikisController < Projects::ApplicationController
12 12
13 def show 13 def show
14 @page = @project_wiki.find_page(params[:id], params[:version_id]) 14 @page = @project_wiki.find_page(params[:id], params[:version_id])
  15 + gollum_wiki = @project_wiki.wiki
  16 + file = gollum_wiki.file(params[:id], gollum_wiki.ref, true)
15 17
16 if @page 18 if @page
17 render 'show' 19 render 'show'
  20 + elsif file
  21 + if file.on_disk?
  22 + send_file file.on_disk_path, disposition: 'inline'
  23 + else
  24 + send_data(
  25 + file.raw_data,
  26 + type: file.mime_type,
  27 + disposition: 'inline',
  28 + filename: file.name
  29 + )
  30 + end
18 else 31 else
19 return render('empty') unless can?(current_user, :write_wiki, @project) 32 return render('empty') unless can?(current_user, :write_wiki, @project)
20 @page = WikiPage.new(@project_wiki) 33 @page = WikiPage.new(@project_wiki)
app/models/project_wiki.rb
@@ -64,7 +64,8 @@ class ProjectWiki @@ -64,7 +64,8 @@ class ProjectWiki
64 # 64 #
65 # Returns an initialized WikiPage instance or nil 65 # Returns an initialized WikiPage instance or nil
66 def find_page(title, version = nil) 66 def find_page(title, version = nil)
67 - if page = wiki.page(title, version) 67 + page_title, page_dir = page_title_and_dir(title)
  68 + if page = wiki.page(page_title, version, page_dir)
68 WikiPage.new(self, page, true) 69 WikiPage.new(self, page, true)
69 else 70 else
70 nil 71 nil
@@ -90,6 +91,12 @@ class ProjectWiki @@ -90,6 +91,12 @@ class ProjectWiki
90 wiki.delete_page(page, commit_details(:deleted, message, page.title)) 91 wiki.delete_page(page, commit_details(:deleted, message, page.title))
91 end 92 end
92 93
  94 + def page_title_and_dir(title)
  95 + title_array = title.split("/")
  96 + title = title_array.pop
  97 + [title.gsub(/\.[^.]*$/, ""), title_array.join("/")]
  98 + end
  99 +
93 private 100 private
94 101
95 def create_repo! 102 def create_repo!
app/models/wiki_page.rb
@@ -175,14 +175,24 @@ class WikiPage @@ -175,14 +175,24 @@ class WikiPage
175 end 175 end
176 176
177 def save(method, *args) 177 def save(method, *args)
178 - if valid? && wiki.send(method, *args)  
179 - @page = wiki.wiki.paged(title) 178 + project_wiki = wiki
  179 + if valid? && project_wiki.send(method, *args)
  180 +
  181 + page_details = if method == :update_page
  182 + @page.path
  183 + else
  184 + title
  185 + end
  186 +
  187 + page_title, page_dir = project_wiki.page_title_and_dir(page_details)
  188 + gollum_wiki = project_wiki.wiki
  189 + @page = gollum_wiki.paged(page_title, page_dir)
180 190
181 set_attributes 191 set_attributes
182 192
183 @persisted = true 193 @persisted = true
184 else 194 else
185 - errors.add(:base, wiki.error_message) if wiki.error_message 195 + errors.add(:base, project_wiki.error_message) if project_wiki.error_message
186 @persisted = false 196 @persisted = false
187 end 197 end
188 @persisted 198 @persisted
app/views/projects/wikis/_new.html.haml
@@ -9,6 +9,6 @@ @@ -9,6 +9,6 @@
9 %span Page slug 9 %span Page slug
10 = text_field_tag :new_wiki_path, nil, placeholder: 'how-to-setup', class: 'form-control', required: true, :'data-wikis-path' => project_wikis_path(@project) 10 = text_field_tag :new_wiki_path, nil, placeholder: 'how-to-setup', class: 'form-control', required: true, :'data-wikis-path' => project_wikis_path(@project)
11 %p.hint 11 %p.hint
12 - Please don't use spaces and slashes 12 + Please don't use spaces.
13 .modal-footer 13 .modal-footer
14 = link_to 'Build', '#', class: 'build-new-wiki btn btn-create' 14 = link_to 'Build', '#', class: 'build-new-wiki btn btn-create'
config/routes.rb
@@ -206,7 +206,7 @@ Gitlab::Application.routes.draw do @@ -206,7 +206,7 @@ Gitlab::Application.routes.draw do
206 end 206 end
207 end 207 end
208 208
209 - resources :wikis, only: [:show, :edit, :destroy, :create], constraints: {id: /[a-zA-Z.0-9_\-]+/} do 209 + resources :wikis, only: [:show, :edit, :destroy, :create], constraints: {id: /[a-zA-Z.0-9_\-\/]+/} do
210 collection do 210 collection do
211 get :pages 211 get :pages
212 put ':id' => 'wikis#update' 212 put ':id' => 'wikis#update'
features/project/wiki.feature
@@ -45,3 +45,20 @@ Feature: Project Wiki @@ -45,3 +45,20 @@ Feature: Project Wiki
45 And I browse to that Wiki page 45 And I browse to that Wiki page
46 And I click on the "Pages" button 46 And I click on the "Pages" button
47 Then I should see the existing page in the pages list 47 Then I should see the existing page in the pages list
  48 +
  49 + Scenario: Image in wiki repo shown on the page
  50 + Given I have an existing Wiki page with images linked on page
  51 + And I browse to wiki page with images
  52 + Then Image should be shown on the page
  53 +
  54 + Scenario: File does not exist in wiki repo
  55 + Given I have an existing Wiki page with images linked on page
  56 + And I browse to wiki page with images
  57 + And I click on image link
  58 + Then I should see the new wiki page form
  59 +
  60 + Scenario: File exists in wiki repo
  61 + Given I have an existing Wiki page with images linked on page
  62 + And I browse to wiki page with images
  63 + And I click on existing image link
  64 + Then I should see the image from wiki repo
features/steps/project/wiki.rb
@@ -86,6 +86,45 @@ class Spinach::Features::ProjectWiki < Spinach::FeatureSteps @@ -86,6 +86,45 @@ class Spinach::Features::ProjectWiki < Spinach::FeatureSteps
86 page.should have_content @page.title 86 page.should have_content @page.title
87 end 87 end
88 88
  89 + Given 'I have an existing Wiki page with images linked on page' do
  90 + wiki.create_page("pictures", "Look at this [image](image.jpg)\n\n ![image](image.jpg)", :markdown, "first commit")
  91 + @wiki_page = wiki.find_page("pictures")
  92 + end
  93 +
  94 + And 'I browse to wiki page with images' do
  95 + visit project_wiki_path(project, @wiki_page)
  96 + end
  97 +
  98 + And 'I click on existing image link' do
  99 + file = Gollum::File.new(wiki.wiki)
  100 + Gollum::Wiki.any_instance.stub(:file).with("image.jpg", "master", true).and_return(file)
  101 + Gollum::File.any_instance.stub(:mime_type).and_return("image/jpeg")
  102 + page.should have_link('image', href: "image.jpg")
  103 + click_on "image"
  104 + end
  105 +
  106 + Then 'I should see the image from wiki repo' do
  107 + url = URI.parse(current_url)
  108 + url.path.should match("wikis/image.jpg")
  109 + page.should_not have_xpath('/html') # Page should render the image which means there is no html involved
  110 + end
  111 +
  112 + Then 'Image should be shown on the page' do
  113 + page.should have_xpath("//img[@src=\"image.jpg\"]")
  114 + end
  115 +
  116 + And 'I click on image link' do
  117 + page.should have_link('image', href: "image.jpg")
  118 + click_on "image"
  119 + end
  120 +
  121 + Then 'I should see the new wiki page form' do
  122 + url = URI.parse(current_url)
  123 + url.path.should match("wikis/image.jpg")
  124 + page.should have_content('New Wiki Page')
  125 + page.should have_content('Editing - image.jpg')
  126 + end
  127 +
89 def wiki 128 def wiki
90 @project_wiki = ProjectWiki.new(project, current_user) 129 @project_wiki = ProjectWiki.new(project, current_user)
91 end 130 end