1
1
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
12
2
13
3
# GET /uploads/1
14
4
# GET /uploads/1.xml
@@ -40,7 +30,30 @@ def edit
40
30
# POST /uploads
41
31
# POST /uploads.xml
42
32
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
44
57
45
58
respond_to do |format |
46
59
if @upload . save
@@ -58,7 +71,7 @@ def create
58
71
# PUT /uploads/1.xml
59
72
def update
60
73
@upload = Upload . find ( params [ :id ] )
61
-
74
+
62
75
respond_to do |format |
63
76
if @upload . update_attributes ( params [ :upload ] )
64
77
flash [ :notice ] = 'Upload was successfully updated.'
@@ -75,11 +88,18 @@ def update
75
88
# DELETE /uploads/1.xml
76
89
def destroy
77
90
@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
78
97
@upload . destroy
79
98
80
99
respond_to do |format |
81
100
format . html { redirect_to ( uploads_url ) }
82
101
format . xml { head :ok }
83
102
end
84
103
end
104
+
85
105
end
0 commit comments