<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>views/attach.erb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,6 @@
 #!/usr/bin/env ruby
 
+require 'fileutils'
 require 'environment'
 require 'sinatra/lib/sinatra'
 
@@ -139,6 +140,30 @@ get '/a/search' do
   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 @@ private
       f.close
       $repo.add('.meta')
     end
-  end
\ No newline at end of file
+  end</diff>
      <filename>git-wiki.rb</filename>
    </modified>
    <modified>
      <diff>@@ -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 @@ class Page
   def blob
     @blob ||= ($repo.gblob(@rev + ':' + @name))
   end
+  
+  # save a file into the _attachments directory
+  def save_file(file, name = '')
+    if name.size &gt; 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 = &quot;uploaded #{filename} for #{@name}&quot;
+    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 = &quot;removed #{file} for #{@name}&quot;
+      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;     &quot;1 Byte&quot;
+      when size &lt; 1024;        &quot;%d Bytes&quot; % size
+      when size &lt; (1024*1024); &quot;%.2f KB&quot;  % (size / 1024.0)
+      else                     &quot;%.2f MB&quot;  % (size / (1024 * 1024.0))
+      end.sub(/([0-9])\.?0+ /, '\1 ' )
+    end
+  end
+  
 end
\ No newline at end of file</diff>
      <filename>page.rb</filename>
    </modified>
    <modified>
      <diff>@@ -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 {</diff>
      <filename>public/style.css</filename>
    </modified>
    <modified>
      <diff>@@ -1 +1 @@
-Subproject commit 898b36eab4cbd0384d23000ee738c9d451558ce0
+Subproject commit f80203adb6bc5756bdcf2388c937891c756db5a6</diff>
      <filename>sinatra</filename>
    </modified>
    <modified>
      <diff>@@ -2,10 +2,29 @@
 
 &lt;div class=&quot;sub_nav&quot;&gt;
   &lt;a href=&quot;/&lt;%= @page.name %&gt;&quot; class=&quot;nav_link&quot;&gt;back&lt;/a&gt;
+  &amp;bull; &lt;a href=&quot;/a/file/upload/&lt;%= @page.name %&gt;&quot; class=&quot;nav_link&quot;&gt;attach&lt;/a&gt;  
   
   &lt;% if @page.tracked? %&gt;
     &amp;bull; &lt;a href=&quot;/h/&lt;%= @page.name %&gt;&quot; class=&quot;nav_link&quot;&gt;history&lt;/a&gt; 
   &lt;% end %&gt;
+  
+  &lt;% if files = @page.attachments %&gt;
+    &lt;h3&gt;Attachments&lt;/h3&gt;
+    &lt;% files.each do |file| %&gt;
+      &lt;li&gt;&lt;a href=&quot;&lt;%= file.link_path %&gt;&quot;&gt;&lt;%= file.name %&gt;&lt;/a&gt; 
+        &lt;span class=&quot;detail&quot;&gt;(&lt;%= file.size %&gt;)&lt;/span&gt;
+        &lt;div class=&quot;attach-options&quot;&gt;
+          &lt;a href=&quot;&lt;%= file.delete_path %&gt;&quot;&gt;delete&lt;/a&gt;
+          &amp;bull; &lt;a href=&quot;&lt;%= file.link_path %&gt;&quot;&gt;download&lt;/a&gt;
+          &lt;% if file.image? %&gt;
+          &amp;bull; &lt;a href=&quot;#&quot; onClick=&quot;$(edit_textarea).html($(edit_textarea).html() + '!&lt;%= file.link_path %&gt;!');&quot;&gt;insert &amp;#187;&lt;/a&gt;
+          &lt;% else %&gt;
+          &amp;bull; &lt;a href=&quot;#&quot; onClick=&quot;$(edit_textarea).html($(edit_textarea).html() + '[&lt;%= file.name %&gt;](&lt;%= file.link_path %&gt;)');&quot;&gt;insert &amp;#187;&lt;/a&gt;
+          &lt;% end %&gt;
+        &lt;/div&gt;
+      &lt;/span&gt;
+    &lt;% end %&gt;
+  &lt;% end %&gt;
 &lt;/div&gt;
 
 &lt;div class=&quot;content&quot;&gt;</diff>
      <filename>views/edit.erb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>caa9183c1a6a3052623856776c2dc3dbf03d1ba4</id>
    </parent>
  </parents>
  <author>
    <name>Scott Chacon</name>
    <email>schacon@gmail.com</email>
  </author>
  <url>http://github.com/al3x/git-wiki/commit/22cf4df0a579c7591f5de47c1c7de26ec865744b</url>
  <id>22cf4df0a579c7591f5de47c1c7de26ec865744b</id>
  <committed-date>2008-04-20T18:25:51-07:00</committed-date>
  <authored-date>2008-04-20T17:56:00-07:00</authored-date>
  <message>added file upload functionality

also updated sinatra submodule reference - file uploads dont work otherwise
and added attachment view page</message>
  <tree>3d03e581ba1f1923cfc62da75b9f70f7a0e01b5e</tree>
  <committer>
    <name>Scott Chacon</name>
    <email>schacon@gmail.com</email>
  </committer>
</commit>
