From 39f854a11e6ae9dff62f7e889917caf5ac40a1aa Mon Sep 17 00:00:00 2001 From: Alexey Verkhovsky Date: Sun, 23 Jan 2005 03:52:07 +0000 Subject: [PATCH] display images from the file yard --- app/controllers/file_controller.rb | 26 +++++++++++++++++++++---- test/functional/file_controller_test.rb | 13 +++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/controllers/file_controller.rb b/app/controllers/file_controller.rb index acf822f4..e357830f 100644 --- a/app/controllers/file_controller.rb +++ b/app/controllers/file_controller.rb @@ -7,11 +7,8 @@ class FileController < ApplicationController layout 'default' def file - raise Instiki::ValidationError.new("Invalid path: no file name") unless @file_name - raise Instiki::ValidationError.new("Invalid path: no web name") unless @web_name - raise Instiki::ValidationError.new("Invalid path: unknown web name") unless @web + check_path - file_yard = @wiki.file_yard(@web) if @params['file'] # form supplied file_yard.upload(@file_name, @params['file']) @@ -29,4 +26,25 @@ class FileController < ApplicationController return_to_last_remembered end + def pic + check_path + if file_yard.has_file?(@file_name) + send_file(file_yard.file_path(@file_name)) + else + render_text "Image not found: #{@file_name}", '404 Not Found' + end + end + + private + + def check_path + raise Instiki::ValidationError.new("Invalid path: no file name") unless @file_name + raise Instiki::ValidationError.new("Invalid path: no web name") unless @web_name + raise Instiki::ValidationError.new("Invalid path: unknown web name") unless @web + end + + def file_yard + @wiki.file_yard(@web) + end + end diff --git a/test/functional/file_controller_test.rb b/test/functional/file_controller_test.rb index 3448441c..b02d9fc0 100644 --- a/test/functional/file_controller_test.rb +++ b/test/functional/file_controller_test.rb @@ -29,7 +29,7 @@ class FileControllerTest < Test::Unit::TestCase end def test_file_download_text_file - File.open(FILE_AREA + '/foo.txt', 'wb') { |f| f.write "aaa\nbbb\n" } + File.open("#{FILE_AREA}/foo.txt", 'wb') { |f| f.write "aaa\nbbb\n" } r = process 'file', 'web' => 'wiki1', 'id' => 'foo.txt' @@ -39,7 +39,7 @@ class FileControllerTest < Test::Unit::TestCase end def test_file_download_pdf_file - File.open(FILE_AREA + '/foo.pdf', 'wb') { |f| f.write "aaa\nbbb\n" } + File.open("#{FILE_AREA}/foo.pdf", 'wb') { |f| f.write "aaa\nbbb\n" } r = process 'file', 'web' => 'wiki1', 'id' => 'foo.pdf' @@ -48,4 +48,13 @@ class FileControllerTest < Test::Unit::TestCase assert_equal 'application/pdf', r.headers['Content-Type'] end + def test_pic_download_gif + FileUtils.cp("#{RAILS_ROOT}/test/fixtures/rails.gif", FILE_AREA) + + r = process 'pic', 'web' => 'wiki1', 'id' => 'rails.gif' + + assert_success + assert_equal File.size("#{FILE_AREA}/rails.gif"), r.binary_content.size + end + end