Add file upload functionality
Adds an :allow_uploads wiki option, an --allow-uploads flag to bin/gollum, an "Upload" button with a file upload dialog, and a handler to commit uploaded files into the repository. :allow_uploads defaults to false, to prevent unauthenticated users from uploading arbitrary files into the repository (albeit only in the uploads directory). This code is based on the patch from @l3iggs at https://github.com/gollum/gollum/issues/694, but the handling on the backend is completely rewritten to use the Committer infrastructure.
This commit is contained in:
+8
-1
@@ -19,7 +19,10 @@ require 'gollum'
|
|||||||
|
|
||||||
exec = {}
|
exec = {}
|
||||||
options = { 'port' => 4567, 'bind' => '0.0.0.0' }
|
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 = OptionParser.new do |opts|
|
||||||
opts.banner = help
|
opts.banner = help
|
||||||
@@ -77,6 +80,10 @@ opts = OptionParser.new do |opts|
|
|||||||
wiki_options[:live_preview] = true
|
wiki_options[:live_preview] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
opts.on("--allow-uploads", "Allows file uploads.") do
|
||||||
|
wiki_options[:allow_uploads] = true
|
||||||
|
end
|
||||||
|
|
||||||
opts.on("--mathjax", "Enables mathjax.") do
|
opts.on("--mathjax", "Enables mathjax.") do
|
||||||
wiki_options[:mathjax] = true
|
wiki_options[:mathjax] = true
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ require 'gollum/views/has_page'
|
|||||||
|
|
||||||
require File.expand_path '../helpers', __FILE__
|
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
|
# Fix to_url
|
||||||
class String
|
class String
|
||||||
alias :upstream_to_url :to_url
|
alias :upstream_to_url :to_url
|
||||||
@@ -146,6 +150,53 @@ module Precious
|
|||||||
end
|
end
|
||||||
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
|
post '/rename/*' do
|
||||||
wikip = wiki_page(params[:splat].first)
|
wikip = wiki_page(params[:splat].first)
|
||||||
halt 500 if wikip.nil?
|
halt 500 if wikip.nil?
|
||||||
@@ -283,6 +334,7 @@ module Precious
|
|||||||
@mathjax = wiki.mathjax
|
@mathjax = wiki.mathjax
|
||||||
@h1_title = wiki.h1_title
|
@h1_title = wiki.h1_title
|
||||||
@editable = false
|
@editable = false
|
||||||
|
@allow_uploads = wiki.allow_uploads
|
||||||
mustache :page
|
mustache :page
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -401,6 +453,7 @@ module Precious
|
|||||||
@mathjax = wiki.mathjax
|
@mathjax = wiki.mathjax
|
||||||
@h1_title = wiki.h1_title
|
@h1_title = wiki.h1_title
|
||||||
@bar_side = wiki.bar_side
|
@bar_side = wiki.bar_side
|
||||||
|
@allow_uploads = wiki.allow_uploads
|
||||||
|
|
||||||
mustache :page
|
mustache :page
|
||||||
elsif file = wiki.file(fullpath, wiki.ref, true)
|
elsif file = wiki.file(fullpath, wiki.ref, true)
|
||||||
|
|||||||
@@ -37,11 +37,14 @@
|
|||||||
fieldMarkup += '<div class="field">';
|
fieldMarkup += '<div class="field">';
|
||||||
switch ( fieldArray[i].type ) {
|
switch ( fieldArray[i].type ) {
|
||||||
|
|
||||||
// only text is supported for now
|
|
||||||
case 'text':
|
case 'text':
|
||||||
fieldMarkup += Dialog.createFieldText( fieldArray[i] );
|
fieldMarkup += Dialog.createFieldText( fieldArray[i] );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'file':
|
||||||
|
fieldMarkup += Dialog.createFieldFile( fieldArray[i] );
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -86,6 +89,22 @@
|
|||||||
return html;
|
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 += '<form method=post enctype="multipart/form-data" ' +
|
||||||
|
'action="' + action + '" ' + 'id="' + id + '">';
|
||||||
|
html += '<input type=file name="' + name + '">';
|
||||||
|
html += '</form>';
|
||||||
|
|
||||||
|
return html;
|
||||||
|
},
|
||||||
|
|
||||||
createMarkup: function( title, body ) {
|
createMarkup: function( title, body ) {
|
||||||
Dialog.markupCreated = true;
|
Dialog.markupCreated = true;
|
||||||
if ($.facebox) {
|
if ($.facebox) {
|
||||||
|
|||||||
@@ -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) {
|
if ($('#minibutton-rename-page').length) {
|
||||||
$('#minibutton-rename-page').parent().removeClass('jaws');
|
$('#minibutton-rename-page').parent().removeClass('jaws');
|
||||||
$('#minibutton-rename-page').click(function(e) {
|
$('#minibutton-rename-page').click(function(e) {
|
||||||
|
|||||||
@@ -20,6 +20,10 @@ Mousetrap.bind(['e'], function( e ) {
|
|||||||
class="action-fileview">Files</a></li>
|
class="action-fileview">Files</a></li>
|
||||||
<li class="minibutton jaws">
|
<li class="minibutton jaws">
|
||||||
<a href="#" id="minibutton-new-page">New</a></li>
|
<a href="#" id="minibutton-new-page">New</a></li>
|
||||||
|
{{#allow_uploads}}
|
||||||
|
<li class="minibutton jaws">
|
||||||
|
<a href="#" id="minibutton-upload-page">Upload</a></li>
|
||||||
|
{{/allow_uploads}}
|
||||||
{{#editable}}
|
{{#editable}}
|
||||||
<li class="minibutton jaws">
|
<li class="minibutton jaws">
|
||||||
<a href="#" id="minibutton-rename-page">Rename</a></li>
|
<a href="#" id="minibutton-rename-page">Rename</a></li>
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ module Precious
|
|||||||
@editable
|
@editable
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def allow_uploads
|
||||||
|
@allow_uploads
|
||||||
|
end
|
||||||
|
|
||||||
def has_header
|
def has_header
|
||||||
@header = (@page.header || false) if @header.nil?
|
@header = (@page.header || false) if @header.nil?
|
||||||
!!@header
|
!!@header
|
||||||
|
|||||||
Reference in New Issue
Block a user