Extracted storage of files functionality from controller to an object called FileYard.
There is one file yard per web.
This commit is contained in:
parent
0d81292168
commit
1d82582c3b
|
@ -7,16 +7,20 @@ class FileController < ApplicationController
|
||||||
layout 'default'
|
layout 'default'
|
||||||
|
|
||||||
def file
|
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']
|
if @params['file']
|
||||||
# form supplied
|
# form supplied
|
||||||
upload_file
|
file_yard.upload(@file_name, @params['file'])
|
||||||
flash[:info] = "File '#{@file_name}' successfully uploaded"
|
flash[:info] = "File '#{@file_name}' successfully uploaded"
|
||||||
return_to_last_remembered
|
return_to_last_remembered
|
||||||
elsif have_file?
|
elsif file_yard.has_file?(@file_name)
|
||||||
send_file(file_path)
|
send_file(file_yard.file_path(@file_name))
|
||||||
else
|
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
|
# go to the template, which is a file upload form
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -25,42 +29,4 @@ class FileController < ApplicationController
|
||||||
return_to_last_remembered
|
return_to_last_remembered
|
||||||
end
|
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
|
end
|
||||||
|
|
43
app/models/file_yard.rb
Normal file
43
app/models/file_yard.rb
Normal file
|
@ -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
|
|
@ -7,7 +7,6 @@ require "zip/zip"
|
||||||
class Web
|
class Web
|
||||||
attr_accessor :name, :address, :password, :markup, :color, :safe_mode, :pages
|
attr_accessor :name, :address, :password, :markup, :color, :safe_mode, :pages
|
||||||
attr_accessor :additional_style, :published, :brackets_only, :count_pages
|
attr_accessor :additional_style, :published, :brackets_only, :count_pages
|
||||||
|
|
||||||
def initialize(name, address, password = nil)
|
def initialize(name, address, password = nil)
|
||||||
@name, @address, @password, @safe_mode = name, address, password, false
|
@name, @address, @password, @safe_mode = name, address, password, false
|
||||||
@pages = {}
|
@pages = {}
|
||||||
|
|
|
@ -7,6 +7,7 @@ require 'madeleine/zmarshal'
|
||||||
require 'web'
|
require 'web'
|
||||||
require 'page'
|
require 'page'
|
||||||
require 'author'
|
require 'author'
|
||||||
|
require 'file_yard'
|
||||||
|
|
||||||
module AbstractWikiService
|
module AbstractWikiService
|
||||||
|
|
||||||
|
@ -24,6 +25,12 @@ module AbstractWikiService
|
||||||
@webs[address] = nil
|
@webs[address] = nil
|
||||||
end
|
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
|
def init_wiki_service
|
||||||
@webs = {}
|
@webs = {}
|
||||||
@system = {}
|
@system = {}
|
||||||
|
@ -110,7 +117,7 @@ class WikiService
|
||||||
|
|
||||||
# These methods do not change the state of persistent objects, and
|
# These methods do not change the state of persistent objects, and
|
||||||
# should not be ogged by Madeleine
|
# 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/'
|
@@storage_path = './storage/'
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ class FileController; def rescue_action(e) logger.error(e); raise e end; end
|
||||||
|
|
||||||
class FileControllerTest < Test::Unit::TestCase
|
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)
|
FileUtils.mkdir_p(FILE_AREA) unless File.directory?(FILE_AREA)
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
@ -22,7 +22,7 @@ class FileControllerTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_file
|
def test_file
|
||||||
process 'file', 'web' => 'wiki', 'id' => 'foo.tgz'
|
process 'file', 'web' => 'wiki1', 'id' => 'foo.tgz'
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_rendered_file 'file/file'
|
assert_rendered_file 'file/file'
|
||||||
|
@ -31,7 +31,7 @@ class FileControllerTest < Test::Unit::TestCase
|
||||||
def test_file_download_text_file
|
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' => 'wiki', 'id' => 'foo.txt'
|
r = process 'file', 'web' => 'wiki1', 'id' => 'foo.txt'
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_equal "aaa\nbbb\n", r.binary_content
|
assert_equal "aaa\nbbb\n", r.binary_content
|
||||||
|
@ -41,7 +41,7 @@ class FileControllerTest < Test::Unit::TestCase
|
||||||
def test_file_download_pdf_file
|
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' => 'wiki', 'id' => 'foo.pdf'
|
r = process 'file', 'web' => 'wiki1', 'id' => 'foo.pdf'
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_equal "aaa\nbbb\n", r.binary_content
|
assert_equal "aaa\nbbb\n", r.binary_content
|
||||||
|
|
35
test/unit/file_yard_test.rb
Normal file
35
test/unit/file_yard_test.rb
Normal file
|
@ -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
|
|
@ -8,11 +8,12 @@ class WikiServiceTest < Test::Unit::TestCase
|
||||||
|
|
||||||
# Clean the test storage directory before the run
|
# Clean the test storage directory before the run
|
||||||
unless defined? @@storage_cleaned
|
unless defined? @@storage_cleaned
|
||||||
FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.command_log'])
|
FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.command_log'])
|
||||||
FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.snapshot'])
|
FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.snapshot'])
|
||||||
FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.tex'])
|
FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.tex'])
|
||||||
FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.zip'])
|
FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.zip'])
|
||||||
FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.pdf'])
|
FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/*.pdf'])
|
||||||
|
FileUtils.rm(Dir[RAILS_ROOT + '/storage/test/instiki/*'])
|
||||||
@@cleaned_storage = true
|
@@cleaned_storage = true
|
||||||
WikiService.instance.setup('pswd', 'Wiki', 'wiki')
|
WikiService.instance.setup('pswd', 'Wiki', 'wiki')
|
||||||
end
|
end
|
||||||
|
@ -20,6 +21,7 @@ class WikiServiceTest < Test::Unit::TestCase
|
||||||
def setup
|
def setup
|
||||||
@s = WikiService.instance
|
@s = WikiService.instance
|
||||||
@s.create_web 'Instiki', 'instiki'
|
@s.create_web 'Instiki', 'instiki'
|
||||||
|
@web = @s.webs['instiki']
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
|
@ -61,6 +63,12 @@ class WikiServiceTest < Test::Unit::TestCase
|
||||||
}
|
}
|
||||||
end
|
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
|
# Checks that a method call or a block doesn;t change the persisted state of the wiki
|
||||||
# Usage:
|
# Usage:
|
||||||
|
|
Loading…
Reference in a new issue