Skip to content

Commit 278709d

Browse files
author
Andreas Zuber
committed
Admin namespace for index function. Basic upload and download function
1 parent 45648dc commit 278709d

File tree

12 files changed

+93
-67
lines changed

12 files changed

+93
-67
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Admin::UploadsController < ApplicationController
2+
before_filter :authenticate
3+
4+
def authenticate
5+
authenticate_or_request_with_http_basic do |name, pass|
6+
name == 'test' && pass == 'nopass'
7+
end
8+
end
9+
10+
# GET /uploads
11+
# GET /uploads.xml
12+
def index
13+
@uploads = Upload.find(:all)
14+
15+
respond_to do |format|
16+
format.html # index.html.erb
17+
format.xml { render :xml => @uploads }
18+
end
19+
end
20+
21+
end

app/controllers/application.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Likewise, all the methods added will be available for all controllers.
33

44
class ApplicationController < ActionController::Base
5+
before_filter :load_settings
6+
57
helper :all # include all helpers, all the time
68

79
# See ActionController::RequestForgeryProtection for details
@@ -12,4 +14,9 @@ class ApplicationController < ActionController::Base
1214
# Uncomment this to filter the contents of submitted sensitive data parameters
1315
# from your application log (in this case, all fields with names like "password").
1416
# filter_parameter_logging :password
17+
18+
def load_settings
19+
@filepaste_settings = YAML.load( File.open( RAILS_ROOT + "/config/settings.yml" ) )
20+
end
21+
1522
end

app/controllers/uploads_controller.rb

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
class UploadsController < ApplicationController
2-
# GET /uploads
3-
# GET /uploads.xml
4-
def index
5-
@uploads = Upload.find(:all)
6-
7-
respond_to do |format|
8-
format.html # index.html.erb
9-
format.xml { render :xml => @uploads }
10-
end
11-
end
122

133
# GET /uploads/1
144
# GET /uploads/1.xml
@@ -40,7 +30,30 @@ def edit
4030
# POST /uploads
4131
# POST /uploads.xml
4232
def create
43-
@upload = Upload.new(params[:upload])
33+
datafile = params[:uploads][:file]
34+
35+
# Create a new hash
36+
chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
37+
random_hash = ""
38+
@filepaste_settings['general']['hash_length'].to_i.times do
39+
random_hash << chars[rand(chars.size)]
40+
end
41+
42+
dirname = File.join RAILS_ROOT, 'public', 'downloads', random_hash
43+
filepath = File.join dirname, datafile.original_filename
44+
45+
# Now we save the File to the disk
46+
Dir.mkdir dirname
47+
File.open(filepath, "wb") { |file| file.write datafile.read }
48+
49+
# Save the file information to the database
50+
@upload = Upload.new
51+
@upload.filename = datafile.original_filename
52+
@upload.description = params[:uploads][:description]
53+
@upload.hash_key = random_hash
54+
@upload.content_type = datafile.content_type
55+
@upload.updated_at = Time.now
56+
@upload.created_at = Time.now
4457

4558
respond_to do |format|
4659
if @upload.save
@@ -58,7 +71,7 @@ def create
5871
# PUT /uploads/1.xml
5972
def update
6073
@upload = Upload.find(params[:id])
61-
74+
6275
respond_to do |format|
6376
if @upload.update_attributes(params[:upload])
6477
flash[:notice] = 'Upload was successfully updated.'
@@ -75,11 +88,18 @@ def update
7588
# DELETE /uploads/1.xml
7689
def destroy
7790
@upload = Upload.find(params[:id])
91+
92+
dirname = File.join RAILS_ROOT, 'public', 'downloads', @upload.random_hash
93+
filepath = File.join dirname, @upload.filename
94+
95+
File.delete filepath
96+
Dir.delete dirname
7897
@upload.destroy
7998

8099
respond_to do |format|
81100
format.html { redirect_to(uploads_url) }
82101
format.xml { head :ok }
83102
end
84103
end
104+
85105
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module Admin::UploadsHelper
2+
end

app/views/uploads/index.html.erb renamed to app/views/admin/uploads/index.html.erb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@
2222
</table>
2323

2424
<br />
25-
26-
<%= link_to 'New upload', new_upload_path %>

app/views/uploads/edit.html.erb

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/views/uploads/new.html.erb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
<h1>New upload</h1>
22

3-
<% form_for(@upload) do |f| %>
3+
<% form_for :uploads, :url => { :action => 'create' }, :html => { :multipart => true } do |f| %>
44
<%= f.error_messages %>
55

6-
<p>
7-
<%= f.label :filename %><br />
8-
<%= f.text_field :filename %>
9-
</p>
106
<p>
117
<%= f.label :description %><br />
128
<%= f.text_area :description %>
139
</p>
1410
<p>
15-
<%= f.label :hash_key %><br />
16-
<%= f.text_field :hash_key %>
17-
</p>
18-
<p>
19-
<%= f.label :content_type %><br />
20-
<%= f.text_field :content_type %>
11+
<%= f.label 'Path to File' %><br />
12+
<%= f.file_field 'file' %>
2113
</p>
2214
<p>
2315
<%= f.submit "Create" %>
2416
</p>
2517
<% end %>
2618

27-
<%= link_to 'Back', uploads_path %>

app/views/uploads/show.html.erb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
<p>
2-
<b>Filename:</b>
2+
<b>File:</b><br/>
33
<%=h @upload.filename %>
44
</p>
55

66
<p>
7-
<b>Description:</b>
7+
<b>Description:</b><br/>
88
<%=h @upload.description %>
99
</p>
1010

1111
<p>
12-
<b>Hash key:</b>
13-
<%=h @upload.hash_key %>
12+
<b>This is the URL to download your file:</b><br/>
13+
<% download_url = root_url + 'downloads/' + @upload.hash_key + '/' + @upload.filename %>
14+
<a href="<%=h download_url%>"><h3><%=h download_url%></h3></a>
1415
</p>
1516

16-
<p>
17-
<b>Content type:</b>
18-
<%=h @upload.content_type %>
19-
</p>
20-
21-
22-
<%= link_to 'Edit', edit_upload_path(@upload) %> |
23-
<%= link_to 'Back', uploads_path %>
17+
<%= link_to 'Upload new File', new_upload_path %>

config/routes.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
ActionController::Routing::Routes.draw do |map|
2-
map.root :controller => 'uploads'
2+
3+
map.namespace :admin do |admin|
4+
admin.resources :uploads
5+
end
6+
7+
map.root :controller => 'uploads', :action => 'new'
38
map.resources :uploads
49

510
# The priority is based upon order of creation: first created -> highest priority.

config/settings.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
general:
3+
hash_length: "32"
4+
5+
ldap_settings:
6+
host: "ldap.yourserver.com"
7+
port: "389"
8+
base: "dc=your,dc=base"

0 commit comments

Comments
 (0)