Skip to content

Commit

Permalink
Convert Mangar.x_dir methods to be Pathnames; Correctly URL-encode im…
Browse files Browse the repository at this point in the history
…age paths
  • Loading branch information
bloopletech committed Jun 11, 2015
1 parent 46f104c commit f468623
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 22 deletions.
4 changes: 4 additions & 0 deletions app/helpers/items_helper.rb
Expand Up @@ -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
6 changes: 3 additions & 3 deletions app/models/book.rb
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/models/item.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions app/services/book_importer.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion app/services/books_importer.rb
Expand Up @@ -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 {} \\;")
Expand Down
2 changes: 1 addition & 1 deletion 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' }

Expand Down
18 changes: 9 additions & 9 deletions config/application.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/system_static_middleware.rb
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions spec/services/book_importer_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f468623

Please sign in to comment.