Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added file upload functionality

also updated sinatra submodule reference - file uploads dont work otherwise
and added attachment view page
  • Loading branch information...
commit 22cf4df0a579c7591f5de47c1c7de26ec865744b 1 parent caa9183
@schacon schacon authored
View
27 git-wiki.rb
@@ -1,5 +1,6 @@
#!/usr/bin/env ruby
+require 'fileutils'
require 'environment'
require 'sinatra/lib/sinatra'
@@ -139,6 +140,30 @@
show :search, 'Search Results'
end
+# file upload attachments
+
+get '/a/file/upload/:page' do
+ @page = Page.new(params[:page])
+ show :attach, 'Attach File for ' + @page.name
+end
+
+post '/a/file/upload/:page' do
+ @page = Page.new(params[:page])
+ @page.save_file(params[:file], params[:name])
+ redirect '/e/' + @page.name
+end
+
+get '/a/file/delete/:page/:file.:ext' do
+ @page = Page.new(params[:page])
+ @page.delete_file(params[:file] + '.' + params[:ext])
+ redirect '/e/' + @page.name
+end
+
+get '/_attachment/:page/:file.:ext' do
+ @page = Page.new(params[:page])
+ send_file(File.join(@page.attach_dir, params[:file] + '.' + params[:ext]))
+end
+
# support methods
def page_url(page)
@@ -160,4 +185,4 @@ def touchfile
f.close
$repo.add('.meta')
end
- end
+ end
View
89 page.rb
@@ -1,10 +1,11 @@
class Page
- attr_reader :name
+ attr_reader :name, :attach_dir
def initialize(name, rev=nil)
@name = name
@rev = rev
@filename = File.join(GIT_REPO, @name)
+ @attach_dir = File.join(GIT_REPO, '_attachments', @name)
end
def body
@@ -88,4 +89,90 @@ def version(rev)
def blob
@blob ||= ($repo.gblob(@rev + ':' + @name))
end
+
+ # save a file into the _attachments directory
+ def save_file(file, name = '')
+ if name.size > 0
+ filename = name + File.extname(file[:filename])
+ else
+ filename = file[:filename]
+ end
+ File.makedirs(@attach_dir) if !File.exists?(@attach_dir)
+ new_file = File.join(@attach_dir, filename)
+
+ f = File.new(new_file, 'w')
+ f.write(file[:tempfile].read)
+ f.close
+
+ commit_message = "uploaded #{filename} for #{@name}"
+ begin
+ $repo.add(new_file)
+ $repo.commit(commit_message)
+ rescue
+ nil
+ end
+ end
+
+ def delete_file(file)
+ file_path = File.join(@attach_dir, file)
+ if File.exists?(file_path)
+ File.unlink(file_path)
+
+ commit_message = "removed #{file} for #{@name}"
+ begin
+ $repo.remove(file_path)
+ $repo.commit(commit_message)
+ rescue
+ nil
+ end
+
+ end
+ end
+
+ def attachments
+ if File.exists?(@attach_dir)
+ return Dir.glob(File.join(@attach_dir, '*')).map { |f| Attachment.new(f, self.name) }
+ else
+ false
+ end
+ end
+
+ class Attachment
+ attr_accessor :path, :page_name
+ def initialize(file_path, name)
+ @path = file_path
+ @page_name = name
+ end
+
+ def name
+ File.basename(@path)
+ end
+
+ def link_path
+ File.join('/_attachment', @page_name, name)
+ end
+
+ def delete_path
+ File.join('/a/file/delete', @page_name, name)
+ end
+
+ def image?
+ ext = File.extname(@path)
+ case ext
+ when '.png', '.jpg', '.jpeg', '.gif'; return true
+ else; return false
+ end
+ end
+
+ def size
+ size = File.size(@path).to_i
+ case
+ when size.to_i == 1; "1 Byte"
+ when size < 1024; "%d Bytes" % size
+ when size < (1024*1024); "%.2f KB" % (size / 1024.0)
+ else "%.2f MB" % (size / (1024 * 1024.0))
+ end.sub(/([0-9])\.?0+ /, '\1 ' )
+ end
+ end
+
end
View
11 public/style.css
@@ -157,7 +157,16 @@ ul {
font-size: .85em;
margin-left: 0.3em;
}
-
+span.detail {
+ color: #888;
+ font-size: .85em;
+}
+div.attach-options {
+ margin-left: 30px;
+ margin-bottom: 10px;
+ font-size: .85em;
+ color: #888;
+}
/* ids */
#container {
2  sinatra
@@ -1 +1 @@
-Subproject commit 898b36eab4cbd0384d23000ee738c9d451558ce0
+Subproject commit f80203adb6bc5756bdcf2388c937891c756db5a6
View
17 views/attach.erb
@@ -0,0 +1,17 @@
+<h1>Attach File</h1>
+
+<div class="content">
+ <h2>New File Upload</h2>
+ <form method="post" action="/a/file/upload/<%= @page.name %>"
+ class="niceform" enctype="multipart/form-data">
+ <label for="name">name</label>
+ <input type="text" name="name" />
+ <br/>
+ <label for="name">file</label>
+ <input type="file" name="file" />
+ <br />
+ <label for="submit"></label>
+ <input type="submit" value="upload" class="submit" />
+ </form>
+</div>
+
View
19 views/edit.erb
@@ -2,10 +2,29 @@
<div class="sub_nav">
<a href="/<%= @page.name %>" class="nav_link">back</a>
+ &bull; <a href="/a/file/upload/<%= @page.name %>" class="nav_link">attach</a>
<% if @page.tracked? %>
&bull; <a href="/h/<%= @page.name %>" class="nav_link">history</a>
<% end %>
+
+ <% if files = @page.attachments %>
+ <h3>Attachments</h3>
+ <% files.each do |file| %>
+ <li><a href="<%= file.link_path %>"><%= file.name %></a>
+ <span class="detail">(<%= file.size %>)</span>
+ <div class="attach-options">
+ <a href="<%= file.delete_path %>">delete</a>
+ &bull; <a href="<%= file.link_path %>">download</a>
+ <% if file.image? %>
+ &bull; <a href="#" onClick="$(edit_textarea).html($(edit_textarea).html() + '!<%= file.link_path %>!');">insert &#187;</a>
+ <% else %>
+ &bull; <a href="#" onClick="$(edit_textarea).html($(edit_textarea).html() + '[<%= file.name %>](<%= file.link_path %>)');">insert &#187;</a>
+ <% end %>
+ </div>
+ </span>
+ <% end %>
+ <% end %>
</div>
<div class="content">
Please sign in to comment.
Something went wrong with that request. Please try again.