GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of al3x/git-wiki
Description: A wiki engine that uses a Git repository as its data store.
Homepage: http://atonie.org/2008/02/git-wiki
Clone URL: git://github.com/zmalltalker/git-wiki.git
added file upload functionality

also updated sinatra submodule reference - file uploads dont work 
otherwise
and added attachment view page
schacon (author)
Sun Apr 20 17:56:00 -0700 2008
commit  22cf4df0a579c7591f5de47c1c7de26ec865744b
tree    3d03e581ba1f1923cfc62da75b9f70f7a0e01b5e
parent  caa9183c1a6a3052623856776c2dc3dbf03d1ba4
...
1
2
 
3
4
5
...
139
140
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
143
144
...
160
161
162
163
164
 
...
1
2
3
4
5
6
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
...
185
186
187
 
188
189
0
@@ -1,5 +1,6 @@
0
 #!/usr/bin/env ruby
0
 
0
+require 'fileutils'
0
 require 'environment'
0
 require 'sinatra/lib/sinatra'
0
 
0
@@ -139,6 +140,30 @@ get '/a/search' do
0
   show :search, 'Search Results'
0
 end
0
 
0
+# file upload attachments
0
+
0
+get '/a/file/upload/:page' do
0
+ @page = Page.new(params[:page])
0
+ show :attach, 'Attach File for ' + @page.name
0
+end
0
+
0
+post '/a/file/upload/:page' do
0
+ @page = Page.new(params[:page])
0
+ @page.save_file(params[:file], params[:name])
0
+ redirect '/e/' + @page.name
0
+end
0
+
0
+get '/a/file/delete/:page/:file.:ext' do
0
+ @page = Page.new(params[:page])
0
+ @page.delete_file(params[:file] + '.' + params[:ext])
0
+ redirect '/e/' + @page.name
0
+end
0
+
0
+get '/_attachment/:page/:file.:ext' do
0
+ @page = Page.new(params[:page])
0
+ send_file(File.join(@page.attach_dir, params[:file] + '.' + params[:ext]))
0
+end
0
+
0
 # support methods
0
 
0
 def page_url(page)
0
@@ -160,4 +185,4 @@ private
0
       f.close
0
       $repo.add('.meta')
0
     end
0
- end
0
\ No newline at end of file
0
+ end
...
1
2
 
3
4
5
6
7
 
8
9
10
...
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
92
...
1
 
2
3
4
5
6
7
8
9
10
11
...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
0
@@ -1,10 +1,11 @@
0
 class Page
0
- attr_reader :name
0
+ attr_reader :name, :attach_dir
0
 
0
   def initialize(name, rev=nil)
0
     @name = name
0
     @rev = rev
0
     @filename = File.join(GIT_REPO, @name)
0
+ @attach_dir = File.join(GIT_REPO, '_attachments', @name)
0
   end
0
 
0
   def body
0
@@ -88,4 +89,90 @@ class Page
0
   def blob
0
     @blob ||= ($repo.gblob(@rev + ':' + @name))
0
   end
0
+
0
+ # save a file into the _attachments directory
0
+ def save_file(file, name = '')
0
+ if name.size > 0
0
+ filename = name + File.extname(file[:filename])
0
+ else
0
+ filename = file[:filename]
0
+ end
0
+ File.makedirs(@attach_dir) if !File.exists?(@attach_dir)
0
+ new_file = File.join(@attach_dir, filename)
0
+
0
+ f = File.new(new_file, 'w')
0
+ f.write(file[:tempfile].read)
0
+ f.close
0
+
0
+ commit_message = "uploaded #{filename} for #{@name}"
0
+ begin
0
+ $repo.add(new_file)
0
+ $repo.commit(commit_message)
0
+ rescue
0
+ nil
0
+ end
0
+ end
0
+
0
+ def delete_file(file)
0
+ file_path = File.join(@attach_dir, file)
0
+ if File.exists?(file_path)
0
+ File.unlink(file_path)
0
+
0
+ commit_message = "removed #{file} for #{@name}"
0
+ begin
0
+ $repo.remove(file_path)
0
+ $repo.commit(commit_message)
0
+ rescue
0
+ nil
0
+ end
0
+
0
+ end
0
+ end
0
+
0
+ def attachments
0
+ if File.exists?(@attach_dir)
0
+ return Dir.glob(File.join(@attach_dir, '*')).map { |f| Attachment.new(f, self.name) }
0
+ else
0
+ false
0
+ end
0
+ end
0
+
0
+ class Attachment
0
+ attr_accessor :path, :page_name
0
+ def initialize(file_path, name)
0
+ @path = file_path
0
+ @page_name = name
0
+ end
0
+
0
+ def name
0
+ File.basename(@path)
0
+ end
0
+
0
+ def link_path
0
+ File.join('/_attachment', @page_name, name)
0
+ end
0
+
0
+ def delete_path
0
+ File.join('/a/file/delete', @page_name, name)
0
+ end
0
+
0
+ def image?
0
+ ext = File.extname(@path)
0
+ case ext
0
+ when '.png', '.jpg', '.jpeg', '.gif'; return true
0
+ else; return false
0
+ end
0
+ end
0
+
0
+ def size
0
+ size = File.size(@path).to_i
0
+ case
0
+ when size.to_i == 1; "1 Byte"
0
+ when size < 1024; "%d Bytes" % size
0
+ when size < (1024*1024); "%.2f KB" % (size / 1024.0)
0
+ else "%.2f MB" % (size / (1024 * 1024.0))
0
+ end.sub(/([0-9])\.?0+ /, '\1 ' )
0
+ end
0
+ end
0
+
0
 end
0
\ No newline at end of file
...
157
158
159
160
 
 
 
 
 
 
 
 
 
 
161
162
163
...
157
158
159
 
160
161
162
163
164
165
166
167
168
169
170
171
172
0
@@ -157,7 +157,16 @@ ul {
0
   font-size: .85em;
0
   margin-left: 0.3em;
0
 }
0
-
0
+span.detail {
0
+ color: #888;
0
+ font-size: .85em;
0
+}
0
+div.attach-options {
0
+ margin-left: 30px;
0
+ margin-bottom: 10px;
0
+ font-size: .85em;
0
+ color: #888;
0
+}
0
 /* ids */
0
 
0
 #container {
...
1
 
...
 
1
0
@@ -1 +1 @@
0
-Subproject commit 898b36eab4cbd0384d23000ee738c9d451558ce0
0
+Subproject commit f80203adb6bc5756bdcf2388c937891c756db5a6
...
2
3
4
 
5
6
7
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
10
11
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
0
@@ -2,10 +2,29 @@
0
 
0
 <div class="sub_nav">
0
   <a href="/<%= @page.name %>" class="nav_link">back</a>
0
+ &bull; <a href="/a/file/upload/<%= @page.name %>" class="nav_link">attach</a>
0
   
0
   <% if @page.tracked? %>
0
     &bull; <a href="/h/<%= @page.name %>" class="nav_link">history</a>
0
   <% end %>
0
+
0
+ <% if files = @page.attachments %>
0
+ <h3>Attachments</h3>
0
+ <% files.each do |file| %>
0
+ <li><a href="<%= file.link_path %>"><%= file.name %></a>
0
+ <span class="detail">(<%= file.size %>)</span>
0
+ <div class="attach-options">
0
+ <a href="<%= file.delete_path %>">delete</a>
0
+ &bull; <a href="<%= file.link_path %>">download</a>
0
+ <% if file.image? %>
0
+ &bull; <a href="#" onClick="$(edit_textarea).html($(edit_textarea).html() + '!<%= file.link_path %>!');">insert &#187;</a>
0
+ <% else %>
0
+ &bull; <a href="#" onClick="$(edit_textarea).html($(edit_textarea).html() + '[<%= file.name %>](<%= file.link_path %>)');">insert &#187;</a>
0
+ <% end %>
0
+ </div>
0
+ </span>
0
+ <% end %>
0
+ <% end %>
0
 </div>
0
 
0
 <div class="content">

Comments

    No one has commented yet.