From 1d82582c3b49b6adb733727124883c3743912045 Mon Sep 17 00:00:00 2001 From: Alexey Verkhovsky Date: Sun, 23 Jan 2005 01:36:51 +0000 Subject: [PATCH] Extracted storage of files functionality from controller to an object called FileYard. There is one file yard per web. --- app/controllers/file_controller.rb | 52 +++++-------------------- app/models/file_yard.rb | 43 ++++++++++++++++++++ app/models/web.rb | 1 - app/models/wiki_service.rb | 9 ++++- test/functional/file_controller_test.rb | 8 ++-- test/unit/file_yard_test.rb | 35 +++++++++++++++++ test/unit/wiki_service_test.rb | 18 ++++++--- 7 files changed, 112 insertions(+), 54 deletions(-) create mode 100644 app/models/file_yard.rb create mode 100644 test/unit/file_yard_test.rb diff --git a/app/controllers/file_controller.rb b/app/controllers/file_controller.rb index 94d65b3d..acf822f4 100644 --- a/app/controllers/file_controller.rb +++ b/app/controllers/file_controller.rb @@ -7,16 +7,20 @@ class FileController < ApplicationController layout 'default' def file - sanitize_file_name + 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 + + file_yard = @wiki.file_yard(@web) if @params['file'] # form supplied - upload_file + file_yard.upload(@file_name, @params['file']) flash[:info] = "File '#{@file_name}' successfully uploaded" return_to_last_remembered - elsif have_file? - send_file(file_path) + elsif file_yard.has_file?(@file_name) + send_file(file_yard.file_path(@file_name)) else - logger.debug("File not found: #{file_path}") + logger.debug("File not found: #{file_yard.files_path}/#{@file_name}") # go to the template, which is a file upload form end end @@ -25,42 +29,4 @@ class FileController < ApplicationController return_to_last_remembered end - private - - def have_file? - File.file?(file_path) - end - - def upload_file - if @params['file'].kind_of?(Tempfile) - @params['file'].close - FileUtils.mv(@params['file'].path, file_path) - elsif @params['file'].kind_of?(IO) - File.open(file_path, 'wb') { |f| f.write(@params['file'].read) } - else - raise 'File to be uploaded is not an IO object' - end - end - - SANE_FILE_NAME = /[-_\.A-Za-z0-9]{1,255}/ - - def sanitize_file_name - raise Instiki::ValidationError.new("Invalid path: no file name") unless @file_name - unless @file_name =~ SANE_FILE_NAME - raise ValidationError.new("Invalid file name: '#{@file_name}'.\n" + - "Only latin characters, digits, dots, underscores and dashes are accepted.") - end - end - - def file_area - raise Instiki::ValidationError.new("Invalid path: no web name") unless @web_name - file_area = File.expand_path("#{@wiki.storage_path}/#{@web_name}") - FileUtils.mkdir_p(file_area) unless File.directory?(file_area) - file_area - end - - def file_path - "#{file_area}/#{@file_name}" - end - end diff --git a/app/models/file_yard.rb b/app/models/file_yard.rb new file mode 100644 index 00000000..0c327b88 --- /dev/null +++ b/app/models/file_yard.rb @@ -0,0 +1,43 @@ +require 'instiki_errors' + +class FileYard + + attr_reader :files_path + + def initialize(files_path) + @files_path = files_path + @files = Dir["#{files_path}/*"].collect{|path| File.basename(path) if File.file?(path) }.compact + end + + def upload_file(name, io) + sanitize_file_name(name) + if io.kind_of?(Tempfile) + io.close + FileUtils.mv(io.path, file_path(name)) + else + File.open(file_path(name), 'wb') { |f| f.write(io.read) } + end + end + + def files + Dir["#{files_path}/*"].collect{|path| File.basename(path) if File.file?(path)}.compact + end + + def has_file?(name) + files.include?(name) + end + + def file_path(name) + "#{files_path}/#{name}" + end + + SANE_FILE_NAME = /[-_\.A-Za-z0-9]{1,255}/ + + def sanitize_file_name(name) + unless name =~ SANE_FILE_NAME + raise Instiki::ValidationError.new("Invalid file name: '#{name}'.\n" + + "Only latin characters, digits, dots, underscores and dashes are accepted.") + end + end + +end diff --git a/app/models/web.rb b/app/models/web.rb index 1a56002e..21e977be 100755 --- a/app/models/web.rb +++ b/app/models/web.rb @@ -7,7 +7,6 @@ require "zip/zip" class Web attr_accessor :name, :address, :password, :markup, :color, :safe_mode, :pages attr_accessor :additional_style, :published, :brackets_only, :count_pages - def initialize(name, address, password = nil) @name, @address, @password, @safe_mode = name, address, password, false @pages = {} diff --git a/app/models/wiki_service.rb b/app/models/wiki_service.rb index baf64c18..9bd242fb 100755 --- a/app/models/wiki_service.rb +++ b/app/models/wiki_service.rb @@ -7,6 +7,7 @@ require 'madeleine/zmarshal' require 'web' require 'page' require 'author' +require 'file_yard' module AbstractWikiService @@ -24,6 +25,12 @@ module AbstractWikiService @webs[address] = nil end + def file_yard(web) + raise "Web #{@web.name} does not belong to this wiki service" unless @webs.values.include?(web) + # TODO cache FileYards + FileYard.new("#{self.storage_path}/#{web.address}") + end + def init_wiki_service @webs = {} @system = {} @@ -110,7 +117,7 @@ class WikiService # These methods do not change the state of persistent objects, and # should not be ogged by Madeleine - automatic_read_only :authenticate, :read_page, :setup?, :webs, :storage_path + automatic_read_only :authenticate, :read_page, :setup?, :webs, :storage_path, :file_yard @@storage_path = './storage/' diff --git a/test/functional/file_controller_test.rb b/test/functional/file_controller_test.rb index 2efa70ff..3448441c 100644 --- a/test/functional/file_controller_test.rb +++ b/test/functional/file_controller_test.rb @@ -9,7 +9,7 @@ class FileController; def rescue_action(e) logger.error(e); raise e end; end class FileControllerTest < Test::Unit::TestCase - FILE_AREA = RAILS_ROOT + '/storage/test/wiki' + FILE_AREA = RAILS_ROOT + '/storage/test/wiki1' FileUtils.mkdir_p(FILE_AREA) unless File.directory?(FILE_AREA) def setup @@ -22,7 +22,7 @@ class FileControllerTest < Test::Unit::TestCase end def test_file - process 'file', 'web' => 'wiki', 'id' => 'foo.tgz' + process 'file', 'web' => 'wiki1', 'id' => 'foo.tgz' assert_success assert_rendered_file 'file/file' @@ -31,7 +31,7 @@ class FileControllerTest < Test::Unit::TestCase def test_file_download_text_file File.open(FILE_AREA + '/foo.txt', 'wb') { |f| f.write "aaa\nbbb\n" } - r = process 'file', 'web' => 'wiki', 'id' => 'foo.txt' + r = process 'file', 'web' => 'wiki1', 'id' => 'foo.txt' assert_success assert_equal "aaa\nbbb\n", r.binary_content @@ -41,7 +41,7 @@ class FileControllerTest < Test::Unit::TestCase def test_file_download_pdf_file File.open(FILE_AREA + '/foo.pdf', 'wb') { |f| f.write "aaa\nbbb\n" } - r = process 'file', 'web' => 'wiki', 'id' => 'foo.pdf' + r = process 'file', 'web' => 'wiki1', 'id' => 'foo.pdf' assert_success assert_equal "aaa\nbbb\n", r.binary_content diff --git a/test/unit/file_yard_test.rb b/test/unit/file_yard_test.rb new file mode 100644 index 00000000..7bf13d95 --- /dev/null +++ b/test/unit/file_yard_test.rb @@ -0,0 +1,35 @@ +#!/bin/env ruby -w + +require File.dirname(__FILE__) + '/../test_helper' +require 'fileutils' +require 'file_yard' +require 'stringio' + +class FileYardTest < Test::Unit::TestCase + + def setup + FileUtils.mkdir_p(file_path) + FileUtils.rm(Dir["#{file_path}/*"]) + @yard = FileYard.new(file_path) + end + + def test_files + assert_equal [], @yard.files + + # FileYard gets the list of files from directory in the constructor + @yard.upload_file('aaa', StringIO.new('file contents')) + assert_equal ["#{file_path}/aaa"], Dir["#{file_path}/*"] + assert_equal ['aaa'], @yard.files + assert @yard.has_file?('aaa') + assert_equal 'file contents', File.read(@yard.file_path('aaa')) + end + + def test_file_path + assert_equal "#{file_path}/abcd", @yard.file_path('abcd') + end + + def file_path + "#{RAILS_ROOT}/storage/test/instiki" + end + +end \ No newline at end of file diff --git a/test/unit/wiki_service_test.rb b/test/unit/wiki_service_test.rb index 6246df97..8f3cec6a 100755 --- a/test/unit/wiki_service_test.rb +++ b/test/unit/wiki_service_test.rb @@ -8,11 +8,12 @@ class WikiServiceTest < Test::Unit::TestCase # Clean the test storage directory before the run unless defined? @@storage_cleaned - FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.command_log']) - FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.snapshot']) - FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.tex']) - FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.zip']) - FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.pdf']) + FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.command_log']) + FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.snapshot']) + FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.tex']) + FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.zip']) + FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.pdf']) + FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/instiki/*']) @@cleaned_storage = true WikiService.instance.setup('pswd', 'Wiki', 'wiki') end @@ -20,6 +21,7 @@ class WikiServiceTest < Test::Unit::TestCase def setup @s = WikiService.instance @s.create_web 'Instiki', 'instiki' + @web = @s.webs['instiki'] end def teardown @@ -61,6 +63,12 @@ class WikiServiceTest < Test::Unit::TestCase } end + def test_file_yard + file_yard = @s.file_yard(@web) + assert_equal FileYard, file_yard.class + assert_equal(@s.storage_path + '/instiki', file_yard.files_path) + end + # Checks that a method call or a block doesn;t change the persisted state of the wiki # Usage: