diff --git a/bin/gollum b/bin/gollum index bd50d9f6..5271c1a3 100755 --- a/bin/gollum +++ b/bin/gollum @@ -19,7 +19,10 @@ require 'gollum' exec = {} options = { 'port' => 4567, 'bind' => '0.0.0.0' } -wiki_options = { :live_preview => false } +wiki_options = { + :live_preview => false, + :allow_uploads => false, +} opts = OptionParser.new do |opts| opts.banner = help @@ -77,6 +80,10 @@ opts = OptionParser.new do |opts| wiki_options[:live_preview] = true end + opts.on("--allow-uploads", "Allows file uploads.") do + wiki_options[:allow_uploads] = true + end + opts.on("--mathjax", "Enables mathjax.") do wiki_options[:mathjax] = true end diff --git a/lib/gollum/app.rb b/lib/gollum/app.rb index 516ba9ea..5f6bb7f0 100644 --- a/lib/gollum/app.rb +++ b/lib/gollum/app.rb @@ -13,6 +13,10 @@ require 'gollum/views/has_page' require File.expand_path '../helpers', __FILE__ +#required to upload bigger binary files +Grit::Git.git_timeout = 120 # timeout in secs +Grit::Git.git_max_size = 190 * 10**6 # size in bytes (10^6=1 MB) + # Fix to_url class String alias :upstream_to_url :to_url @@ -146,6 +150,53 @@ module Precious end end + post '/uploadFile' do + wiki = wiki_new + + unless wiki.allow_uploads + @message = "File uploads are disabled" + mustache :error + return + end + + if params[:file] + fullname = params[:file][:filename] + tempfile = params[:file][:tempfile] + end + + dir = 'uploads' + ext = ::File.extname(fullname) + format = ext.split('.').last || 'txt' + filename = ::File.basename(fullname, ext) + contents = ::File.read(tempfile) + reponame = filename + '.' + format + + head = wiki.repo.head + + options = { + :message => "Uploaded file to uploads/#{reponame}", + :parent => wiki.repo.head.commit, + } + author = session['gollum.author'] + unless author.nil? + options.merge! author + end + + begin + committer = Gollum::Committer.new(wiki, options) + committer.add_to_index(dir, filename, format, contents) + committer.after_commit do |committer, sha| + wiki.clear_cache + committer.update_working_dir(dir, filename, format) + end + committer.commit + redirect to('/') + rescue Gollum::DuplicatePageError => e + @message = "Duplicate page: #{e.message}" + mustache :error + end + end + post '/rename/*' do wikip = wiki_page(params[:splat].first) halt 500 if wikip.nil? @@ -283,6 +334,7 @@ module Precious @mathjax = wiki.mathjax @h1_title = wiki.h1_title @editable = false + @allow_uploads = wiki.allow_uploads mustache :page end @@ -401,6 +453,7 @@ module Precious @mathjax = wiki.mathjax @h1_title = wiki.h1_title @bar_side = wiki.bar_side + @allow_uploads = wiki.allow_uploads mustache :page elsif file = wiki.file(fullpath, wiki.ref, true) diff --git a/lib/gollum/public/gollum/javascript/gollum.dialog.js b/lib/gollum/public/gollum/javascript/gollum.dialog.js index 4672e399..a058aa98 100755 --- a/lib/gollum/public/gollum/javascript/gollum.dialog.js +++ b/lib/gollum/public/gollum/javascript/gollum.dialog.js @@ -37,11 +37,14 @@ fieldMarkup += '
'; switch ( fieldArray[i].type ) { - // only text is supported for now case 'text': fieldMarkup += Dialog.createFieldText( fieldArray[i] ); break; + case 'file': + fieldMarkup += Dialog.createFieldFile( fieldArray[i] ); + break; + default: break; @@ -86,6 +89,22 @@ return html; }, + createFieldFile: function( fieldAttributes ) { + // Not actually a field, but an embedded form. + var html = ''; + + var id = fieldAttributes.id || 'upload'; + var name = fieldAttributes.name || 'file'; + var action = fieldAttributes.action || 'uploadFile'; + + html += '
'; + html += ''; + html += '
'; + + return html; + }, + createMarkup: function( title, body ) { Dialog.markupCreated = true; if ($.facebox) { diff --git a/lib/gollum/public/gollum/javascript/gollum.js b/lib/gollum/public/gollum/javascript/gollum.js index afbc0f2e..5e5bf857 100755 --- a/lib/gollum/public/gollum/javascript/gollum.js +++ b/lib/gollum/public/gollum/javascript/gollum.js @@ -145,6 +145,25 @@ $(document).ready(function() { } } + if ($('#minibutton-upload-page').length) { + $('#minibutton-upload-page').parent().removeClass('jaws'); + $('#minibutton-upload-page').click(function(e) { + e.preventDefault(); + + $.GollumDialog.init({ + title: 'Upload File', + fields: [ + { + type: 'file' + } + ], + OK: function( res ) { + $('#upload').submit(); + } + }); + }); + } + if ($('#minibutton-rename-page').length) { $('#minibutton-rename-page').parent().removeClass('jaws'); $('#minibutton-rename-page').click(function(e) { diff --git a/lib/gollum/templates/page.mustache b/lib/gollum/templates/page.mustache index 2af71275..a88643fb 100644 --- a/lib/gollum/templates/page.mustache +++ b/lib/gollum/templates/page.mustache @@ -20,6 +20,10 @@ Mousetrap.bind(['e'], function( e ) { class="action-fileview">Files
  • New
  • + {{#allow_uploads}} +
  • + Upload
  • + {{/allow_uploads}} {{#editable}}
  • Rename
  • diff --git a/lib/gollum/views/page.rb b/lib/gollum/views/page.rb index 17225f0a..04165649 100644 --- a/lib/gollum/views/page.rb +++ b/lib/gollum/views/page.rb @@ -39,6 +39,10 @@ module Precious @editable end + def allow_uploads + @allow_uploads + end + def has_header @header = (@page.header || false) if @header.nil? !!@header