class FilesController ApplicationControlle require digest md5 def inde

 1
 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
31
32
33
34
35
36
class FilesController < ApplicationController
require 'digest/md5'
def index
end
def new
@file = Files.new
end
def create
uploaded_io = params[:file][:file]
ext = uploaded_io.original_filename.split('.').last
md5hash = Digest::MD5.hexdigest(uploaded_io.original_filename)
File.open(Rails.root.join('public', 'uploads', md5hash + '.' + ext), 'wb') do |file|
file.write(uploaded_io.read)
end
file = File.open(Rails.root.join('public', 'uploads', md5hash), 'wb')
@file = Files.new(:name => uploaded_io.original_filename, :ext => ext, :md5 => md5hash, :size => file.size,
:loaded => 0, :uploaded => DateTime.now.to_date
)
if @file.save
redirect_to @file
else
render 'index'
end
end
def show
@file = Files.find(params[:id])
end
def last
end
end