Skip to content

Instantly share code, notes, and snippets.

@simonista
Last active December 14, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simonista/5163912 to your computer and use it in GitHub Desktop.
Save simonista/5163912 to your computer and use it in GitHub Desktop.
Gollum with basic auth + file uploads
require 'gollum/frontend/app'
require 'digest/sha1'
class App < Precious::App
User = Struct.new(:name, :email, :password_hash, :can_write)
before { authenticate! }
helpers do
def authenticate!
@auth ||= Rack::Auth::Basic::Request.new(request.env)
if @auth.provided? && @auth.basic? && @auth.credentials
set_author
else
response['WWW-Authenticate'] = %(Basic realm="Gollum Wiki")
throw(:halt, [401, "Not authorized\n"])
end
end
def set_author
session['gollum.author'] = {
:name => @auth.username,
:email => "#{@auth.username}@instructure.com",
}
end
end
post "/upload" do
if params[:file]
name = params[:file][:filename]
format = nil
data = params[:file][:tempfile].read
path = ''
wiki = wiki_new
begin
params[:message] = "Uploaded #{name}"
wiki.write_page(name, format, data, commit_message, path)
redirect to("/")
rescue Gollum::DuplicatePageError => e
@message = "Duplicate file: #{e.message}"
mustache :error
end
else
redirect to("/")
end
end
end
module Gollum
class Wiki
alias_method :orig_page_file_name, :page_file_name
def page_file_name(name, format)
return name unless format
orig_page_file_name(name, format)
end
end
class Committer
def add_to_index(dir, name, format, data, allow_same_ext = false)
# spaces must be dashes
dir.gsub!(' ', '-')
name.gsub!(' ', '-')
path = @wiki.page_file_name(name, format)
dir = '/' if dir.strip.empty?
fullpath = ::File.join(*[@wiki.page_file_dir, dir, path].compact)
fullpath = fullpath[1..-1] if fullpath =~ /^\//
if index.current_tree && tree = index.current_tree / (@wiki.page_file_dir || '/')
tree = tree / dir unless tree.nil?
end
if tree
downpath = path.downcase.sub(/\.\w+$/, '')
tree.blobs.each do |blob|
next if page_path_scheduled_for_deletion?(index.tree, fullpath)
existing_file = blob.name.downcase.sub(/\.\w+$/, '')
existing_file_ext = ::File.extname(blob.name).sub(/^\./, '')
new_file_ext = ::File.extname(path).sub(/^\./, '')
if downpath == existing_file && !(allow_same_ext && new_file_ext == existing_file_ext)
raise DuplicatePageError.new(dir, blob.name, path)
end
end
end
fullpath = fullpath.force_encoding('ascii-8bit') if fullpath.respond_to?(:force_encoding)
# Don't normalize for file uploads
data = @wiki.normalize(data) if format
index.add(fullpath, data)
end
end
end
require 'rubygems'
require 'app'
App.set(:gollum_path, File.expand_path(File.dirname(__FILE__)) + '/wiki')
App.set(:default_markup, :markdown)
App.set(:wiki_options, {
:universal_toc => false, # ???
:show_all => true, # Shows all files in file view. By default only valid pages are shown.
# :css => true, # Inject custom css. Uses custom.css from root repository.
# :page_file_dir => "", # Specify the sub directory for all page files (default: repository root).
# :base_path => "", # Specify the base path.
# :gollum_path => "", # Specify the gollum path.
# :ref => "", # Specify the repository ref to use (default: master).
# :live_preview => false, # Disables livepreview.
# :mathjax => true, # Enables mathjax.
# :user_icons => "", # Set the history user icons. Valid values: gravatar, identicon, none. Default: none.
# :collapse_tree => true, # Collapse file view tree. By default, expanded tree is shown.
# :h1_title => true, # Sets page title to value of first h1.
})
run App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment