Beginnings of a FileController (serving the file upload feature)
This commit is contained in:
parent
71407f9b9f
commit
12a34823a8
5 changed files with 83 additions and 26 deletions
|
@ -7,7 +7,7 @@ class ApplicationController < ActionController::Base
|
||||||
# implements Instiki's legacy URLs
|
# implements Instiki's legacy URLs
|
||||||
require 'url_rewriting_hack'
|
require 'url_rewriting_hack'
|
||||||
|
|
||||||
before_filter :set_utf8_http_header
|
before_filter :set_utf8_http_header, :connect_to_model
|
||||||
after_filter :remember_location
|
after_filter :remember_location
|
||||||
|
|
||||||
# For injecting a different wiki model implementation. Intended for use in tests
|
# For injecting a different wiki model implementation. Intended for use in tests
|
||||||
|
@ -58,4 +58,28 @@ class ApplicationController < ActionController::Base
|
||||||
@response.headers['Content-Type'] = 'text/html; charset=UTF-8'
|
@response.headers['Content-Type'] = 'text/html; charset=UTF-8'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def connect_to_model
|
||||||
|
@action_name = @params['action'] || 'index'
|
||||||
|
@web_name = @params['web']
|
||||||
|
@wiki = wiki
|
||||||
|
@web = @wiki.webs[@web_name] unless @web_name.nil?
|
||||||
|
@page_name = @params['id']
|
||||||
|
@page = @wiki.read_page(@web_name, @page_name) unless @page_name.nil?
|
||||||
|
@author = cookies['author'] || 'AnonymousCoward'
|
||||||
|
check_authorization(@action_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_authorization(action_name)
|
||||||
|
if in_a_web? and
|
||||||
|
not authorized? and
|
||||||
|
not %w( login authenticate published ).include?(action_name)
|
||||||
|
redirect_to :action => 'login'
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def in_a_web?
|
||||||
|
not @web_name.nil?
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
30
app/controllers/file_controller.rb
Normal file
30
app/controllers/file_controller.rb
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
require 'application'
|
||||||
|
class FileController < ApplicationController
|
||||||
|
|
||||||
|
layout 'default', :except => [:rss_feed, :rss_with_headlines, :tex, :export_tex, :export_html]
|
||||||
|
|
||||||
|
def file
|
||||||
|
if have_file?(@params['id'])
|
||||||
|
render_text 'Download file'
|
||||||
|
else
|
||||||
|
render_text 'form'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def have_file?(file_name)
|
||||||
|
sanitize_file_name(file_name)
|
||||||
|
@wiki.storage_path
|
||||||
|
end
|
||||||
|
|
||||||
|
SANE_FILE_NAME = /[-_A-Za-z0-9]{1,255}/
|
||||||
|
def sanitize_file_name(file_name)
|
||||||
|
unless file_name =~ SANE_FILE_NAME
|
||||||
|
raise "Invalid file name: '#{file_name}'.\n" +
|
||||||
|
"Only latin characters, digits, underscores and dashes are accepted."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
|
@ -5,7 +5,6 @@ require 'redcloth_for_tex'
|
||||||
class WikiController < ApplicationController
|
class WikiController < ApplicationController
|
||||||
|
|
||||||
layout 'default', :except => [:rss_feed, :rss_with_headlines, :tex, :export_tex, :export_html]
|
layout 'default', :except => [:rss_feed, :rss_with_headlines, :tex, :export_tex, :export_html]
|
||||||
before_filter :pre_process
|
|
||||||
|
|
||||||
def index
|
def index
|
||||||
if @web_name
|
if @web_name
|
||||||
|
@ -271,15 +270,6 @@ class WikiController < ApplicationController
|
||||||
password_check(@params['password'])
|
password_check(@params['password'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_authorization(action_name)
|
|
||||||
if in_a_web? and
|
|
||||||
not authorized? and
|
|
||||||
not %w( login authenticate published ).include?(action_name)
|
|
||||||
redirect_to :action => 'login'
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def convert_tex_to_pdf(tex_path)
|
def convert_tex_to_pdf(tex_path)
|
||||||
# TODO remove earlier PDF files with the same prefix
|
# TODO remove earlier PDF files with the same prefix
|
||||||
# TODO handle gracefully situation where pdflatex is not available
|
# TODO handle gracefully situation where pdflatex is not available
|
||||||
|
@ -329,10 +319,6 @@ class WikiController < ApplicationController
|
||||||
@revision = @page.revisions[@params['rev'].to_i]
|
@revision = @page.revisions[@params['rev'].to_i]
|
||||||
end
|
end
|
||||||
|
|
||||||
def in_a_web?
|
|
||||||
not @web_name.nil?
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_category
|
def parse_category
|
||||||
@categories = @web.categories
|
@categories = @web.categories
|
||||||
@category = @params['category']
|
@category = @params['category']
|
||||||
|
@ -361,17 +347,6 @@ class WikiController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def pre_process
|
|
||||||
@action_name = @params['action'] || 'index'
|
|
||||||
@web_name = @params['web']
|
|
||||||
@wiki = wiki
|
|
||||||
@web = @wiki.webs[@web_name] unless @web_name.nil?
|
|
||||||
@page_name = @params['id']
|
|
||||||
@page = @wiki.read_page(@web_name, @page_name) unless @page_name.nil?
|
|
||||||
@author = cookies['author'] || 'AnonymousCoward'
|
|
||||||
check_authorization(@action_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def redirect_show(page_name = @page_name, web = @web_name)
|
def redirect_show(page_name = @page_name, web = @web_name)
|
||||||
redirect_to :web => web, :action => 'show', :id => CGI.escape(page_name)
|
redirect_to :web => web, :action => 'show', :id => CGI.escape(page_name)
|
||||||
end
|
end
|
||||||
|
|
24
test/functional/file_controller_test.rb
Normal file
24
test/functional/file_controller_test.rb
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#!/bin/env ruby -w
|
||||||
|
|
||||||
|
require File.dirname(__FILE__) + '/../test_helper'
|
||||||
|
require 'file_controller'
|
||||||
|
|
||||||
|
# Raise errors beyond the default web-based presentation
|
||||||
|
class FileController; def rescue_action(e) logger.error(e); raise e end; end
|
||||||
|
|
||||||
|
class FileControllerTest < Test::Unit::TestCase
|
||||||
|
|
||||||
|
def setup
|
||||||
|
setup_test_wiki
|
||||||
|
setup_controller_test
|
||||||
|
end
|
||||||
|
|
||||||
|
def tear_down
|
||||||
|
tear_down_wiki
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_file
|
||||||
|
process 'file', 'id' => 'foo.tgz'
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -54,6 +54,10 @@ class WikiServiceWithNoPersistence
|
||||||
def initialize
|
def initialize
|
||||||
init_wiki_service
|
init_wiki_service
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def storage_path
|
||||||
|
RAILS_ROOT + '/storage/test'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue