module JekyllAdmin::FileHelper

Public Instance Methods

delete_file(path) click to toggle source

Delete the file at the given path

# File lib/jekyll-admin/file_helper.rb, line 34
def delete_file(path)
  Jekyll.logger.debug "DELETING:", path
  FileUtils.rm_f sanitized_path(path)
  site.process
end
delete_file_without_process(path) click to toggle source
# File lib/jekyll-admin/file_helper.rb, line 40
def delete_file_without_process(path)
  Jekyll.logger.debug "DELETING:", path
  FileUtils.rm_f sanitized_path(path)
end
requested_file() click to toggle source

The file the user requested in the URL

# File lib/jekyll-admin/file_helper.rb, line 6
def requested_file
  find_by_path(path)
end
write_file(path, content) click to toggle source

Write a file to disk with the given content

# File lib/jekyll-admin/file_helper.rb, line 18
def write_file(path, content)
  Jekyll.logger.debug "WRITING:", path
  path = sanitized_path(path)
  FileUtils.mkdir_p File.dirname(path)
  File.open(path, "wb") do |file|
    file.write(content)
  end
  # we should fully process in dev mode for tests to pass
  if ENV["RACK_ENV"] == "production"
    site.read
  else
    site.process
  end
end
written_file() click to toggle source

The file ultimately written to disk This may be the requested file, or in the case of a rename will be read from the new path that was actually written to disk

# File lib/jekyll-admin/file_helper.rb, line 13
def written_file
  find_by_path(write_path)
end

Private Instance Methods

ensure_directory() click to toggle source
# File lib/jekyll-admin/file_helper.rb, line 87
def ensure_directory
  render_404 unless Dir.exist?(directory_path)
end
ensure_file(file) click to toggle source
# File lib/jekyll-admin/file_helper.rb, line 75
def ensure_file(file)
  render_404 if file.nil?
end
ensure_not_file(file) click to toggle source
# File lib/jekyll-admin/file_helper.rb, line 79
def ensure_not_file(file)
  return if file.nil?

  Jekyll.logger.warn "Jekyll Admin:", "Could not create file."
  Jekyll.logger.warn "", "Path #{file.relative_path.inspect} already exists!"
  render_404
end
ensure_not_overwriting_existing_file() click to toggle source
# File lib/jekyll-admin/file_helper.rb, line 55
def ensure_not_overwriting_existing_file
  ensure_not_file(written_file)
end
ensure_requested_file() click to toggle source
# File lib/jekyll-admin/file_helper.rb, line 47
def ensure_requested_file
  ensure_file(requested_file)
end
ensure_written_file() click to toggle source
# File lib/jekyll-admin/file_helper.rb, line 51
def ensure_written_file
  ensure_file(written_file)
end
find_by_path(path) click to toggle source
# File lib/jekyll-admin/file_helper.rb, line 59
def find_by_path(path)
  files = case namespace
          when "collections"
            collection.docs
          when "data"
            DataFile.all
          when "drafts"
            drafts
          when "pages", "static_files"
            site.public_send(namespace.to_sym)
          else
            []
          end
  files.find { |f| sanitized_path(f.path) == path }
end