From f46862345713d6e3397224be8a4855b857529158 Mon Sep 17 00:00:00 2001 From: "Brenton \"B-Train\" Fletcher" Date: Thu, 11 Jun 2015 20:27:30 +1000 Subject: [PATCH] Convert Mangar.x_dir methods to be Pathnames; Correctly URL-encode image paths --- app/helpers/items_helper.rb | 4 ++++ app/models/book.rb | 6 +++--- app/models/item.rb | 4 ++-- app/services/book_importer.rb | 9 +++++++-- app/services/books_importer.rb | 2 +- app/views/books/show.html.haml | 2 +- config/application.rb | 18 +++++++++--------- lib/system_static_middleware.rb | 2 +- spec/services/book_importer_spec.rb | 6 +++--- 9 files changed, 31 insertions(+), 22 deletions(-) diff --git a/app/helpers/items_helper.rb b/app/helpers/items_helper.rb index 60ded56..71ae286 100755 --- a/app/helpers/items_helper.rb +++ b/app/helpers/items_helper.rb @@ -27,4 +27,8 @@ def selector(name, options) out << hidden_field_tag(name, params[name]) raw out end + + def escape_path(path) + path.split('/').map { |component| Rack::Utils.escape_path(component) }.join('/') + end end diff --git a/app/models/book.rb b/app/models/book.rb index ad9d088..e5306c3 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -17,11 +17,11 @@ def rethumbnail begin start = Time.now puts "Rethumbnailing #{self.id}" - book_dir = "#{Mangar.books_dir}/#{self.path}" + book_dir = Mangar.books_dir + path puts "After step 1, #{Time.now - start}" - images = self.class.image_file_list(Dir.deep_entries(book_dir)) + images = self.class.image_file_list(book_dir.children) puts "After step 2, #{Time.now - start}" - update_attribute(:preview, File.open("#{book_dir}/#{images.first}", "r")) + update_attribute(:preview, File.open(images.first, "r")) puts "After step 3, #{Time.now - start}" rescue Exception => e ActionDispatch::ShowExceptions.new(Mangar::Application.instance).send(:log_error, e) diff --git a/app/models/item.rb b/app/models/item.rb index c43b61b..68e4471 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -14,7 +14,7 @@ def open def delete_original begin - FileUtils.mkdir_p(File.dirname("#{Mangar.deleted_dir}/#{path}")) + (Mangar.deleted_dir + path).dirname.mkpath File.rename(real_path, "#{Mangar.deleted_dir}/#{path}") rescue Exception => e ActionDispatch::ShowExceptions.new(Mangar::Application.instance).send(:log_error, e) @@ -24,7 +24,7 @@ def delete_original def export begin - FileUtils.mkdir_p(File.dirname("#{Mangar.exported_dir}/#{path}")) + (Mangar.exported_dir + path).dirname.mkpath FileUtils.cp_r(real_path, "#{Mangar.exported_dir}/#{path}") rescue Exception => e ActionDispatch::ShowExceptions.new(Mangar::Application.instance).send(:log_error, e) diff --git a/app/services/book_importer.rb b/app/services/book_importer.rb index 6a8b18f..57ba257 100644 --- a/app/services/book_importer.rb +++ b/app/services/book_importer.rb @@ -10,11 +10,11 @@ def initialize(path) end def relative_path - @path.relative_path_from(Pathname.new(Mangar.import_dir)) + @path.relative_path_from(Mangar.import_dir) end def destination_dir - Pathname.new(Mangar.books_dir) + relative_path + Mangar.books_dir + relative_path end def import @@ -66,6 +66,11 @@ def data_from_compressed_file end def data_from_directory + move_directory + FileUtils.chmod_R(0755, destination_dir.to_s) + end + + def move_directory @path.rename(destination_dir) rescue Errno::ENOTEMPTY @path.children.select { |c| c.file? }.each do |c| diff --git a/app/services/books_importer.rb b/app/services/books_importer.rb index 60028b1..86b766b 100644 --- a/app/services/books_importer.rb +++ b/app/services/books_importer.rb @@ -5,7 +5,7 @@ class BooksImporter #Else skip/recurse into dir. #Do not call more than once at a time def import_and_update - path_list = Pathname.new(Mangar.import_dir).realpath.descendant_directories + path_list = Mangar.import_dir.descendant_directories path_list.each { |path| BookImportWorker.perform_async(path.to_s) } #system("cd #{File.escape_name(Mangar.import_dir)} && find . -depth -type d -empty -exec rmdir {} \\;") diff --git a/app/views/books/show.html.haml b/app/views/books/show.html.haml index a8369ba..c5f6d5c 100644 --- a/app/views/books/show.html.haml +++ b/app/views/books/show.html.haml @@ -1,7 +1,7 @@ = content_for :head do = javascript_include_tag 'show' :javascript - var pages = #{raw @book.page_paths.to_json}; + var pages = #{raw @book.page_paths.map { |path| escape_path(path) }.to_json}; %img#image{ src: '/images/blank.png' } diff --git a/config/application.rb b/config/application.rb index eefd227..febbc99 100644 --- a/config/application.rb +++ b/config/application.rb @@ -70,22 +70,22 @@ class Application < Rails::Application mattr_accessor :dir, :mangar_dir, :books_dir, :videos_dir, :deleted_dir, :exported_dir, :import_dir - Mangar.dir = File.realpath(File.expand_path("~/.mangar/")) + Mangar.dir = Pathname.new("~/.mangar").expand_path.realpath - Mangar.mangar_dir = File.expand_path("#{Mangar.dir}/mangar-data") - Mangar.books_dir = File.expand_path("#{Mangar.mangar_dir}/public/system/books") - Mangar.videos_dir = File.expand_path("#{Mangar.mangar_dir}/public/system/videos") - Mangar.import_dir = File.expand_path("#{Mangar.dir}/import") - Mangar.exported_dir = File.expand_path("#{Mangar.dir}/exported") - Mangar.deleted_dir = File.expand_path("#{Mangar.dir}/deleted") + Mangar.mangar_dir = Mangar.dir + "mangar-data" + Mangar.books_dir = Mangar.mangar_dir + "public/system/books" + Mangar.videos_dir = Mangar.mangar_dir + "public/system/videos" + Mangar.import_dir = Mangar.dir + "import" + Mangar.exported_dir = Mangar.dir + "exported" + Mangar.deleted_dir = Mangar.dir + "deleted" - [Mangar.dir, Mangar.mangar_dir, Mangar.books_dir, Mangar.videos_dir, Mangar.import_dir, Mangar.deleted_dir, Mangar.exported_dir].each { |d| FileUtils.mkdir_p(d) unless File.exists?(d) } + [Mangar.dir, Mangar.mangar_dir, Mangar.books_dir, Mangar.videos_dir, Mangar.import_dir, Mangar.deleted_dir, Mangar.exported_dir].each { |d| d.mkpath } end module CarrierWave class << self def root - "#{Mangar.mangar_dir}/public" + (Mangar.mangar_dir + "public").to_s end end end diff --git a/lib/system_static_middleware.rb b/lib/system_static_middleware.rb index 7288aec..abd2055 100755 --- a/lib/system_static_middleware.rb +++ b/lib/system_static_middleware.rb @@ -6,7 +6,7 @@ class SystemStaticMiddleware def initialize(app) @app = app - @file_server = ::Rack::File.new("#{Mangar.mangar_dir}/public") + @file_server = ::Rack::File.new((Mangar.mangar_dir + "public").to_s) end def call(env) diff --git a/spec/services/book_importer_spec.rb b/spec/services/book_importer_spec.rb index 45542a6..21b3fd8 100644 --- a/spec/services/book_importer_spec.rb +++ b/spec/services/book_importer_spec.rb @@ -6,7 +6,7 @@ describe '#relative_path' do let(:path) { File.realpath('spec/fixtures/import/temp/child') } before do - allow(Mangar).to receive(:import_dir).and_return(File.realpath('spec/fixtures/import')) + allow(Mangar).to receive(:import_dir).and_return(Pathname.new('spec/fixtures/import').realpath) end specify do @@ -29,8 +29,8 @@ describe '#destination_dir' do let(:path) { File.realpath('spec/fixtures/import/temp/child') } before do - allow(Mangar).to receive(:import_dir).and_return(File.realpath('spec/fixtures/import')) - allow(Mangar).to receive(:books_dir).and_return('spec/fixtures/books') + allow(Mangar).to receive(:import_dir).and_return(Pathname.new('spec/fixtures/import').realpath) + allow(Mangar).to receive(:books_dir).and_return(Pathname.new('spec/fixtures/books')) end specify do