class Thoth::MediaController
Public Instance Methods
delete(id = nil)
click to toggle source
# File lib/thoth/controller/media.rb, line 46 def delete(id = nil) require_auth error_404 unless id && @file = Media[id] if request.post? error_403 unless form_token_valid? if request[:confirm] == 'yes' @file.destroy flash[:success] = 'File deleted.' redirect(rs(:list)) else redirect(rs(:edit, id)) end end @title = "Delete File: #{@file.filename}" @delete = true @show_file_edit = true end
edit(id = nil)
click to toggle source
# File lib/thoth/controller/media.rb, line 68 def edit(id = nil) require_auth redirect(rs(:new)) unless id && @file = Media[id] @title = "Edit Media - #{@file.filename}" @form_action = rs(:edit, id).to_s @show_file_edit = true if request.post? error_403 unless form_token_valid? tempfile, filename, type = request[:file].values_at( :tempfile, :filename, :type) @file.mimetype = type || 'application/octet-stream' begin unless File.directory?(File.dirname(@file.path)) FileUtils.mkdir_p(File.dirname(@file.path)) end FileUtils.mv(tempfile.path, @file.path) @file.save flash[:success] = 'File saved.' redirect(rs(:edit, id)) rescue => e @media_error = "Error: #{e}" end end end
index(filename = nil)
click to toggle source
# File lib/thoth/controller/media.rb, line 37 def index(filename = nil) error_404 unless filename && file = Media[:filename => filename.strip] send_media(file.path) rescue Errno::ENOENT => e error_404 end
list(page = 1)
click to toggle source
# File lib/thoth/controller/media.rb, line 101 def list(page = 1) require_auth page = page.to_i @columns = [:filename, :size, :created_at, :updated_at] @order = (request[:order] || :asc).to_sym @sort = (request[:sort] || :filename).to_sym @sort = :created_at unless @columns.include?(@sort) @sort_url = rs(:list, page) @files = Media.order(@order == :desc ? Sequel.desc(@sort) : @sort).paginate(page, 20) @title = "Media (page #{page} of #{[@files.page_count, 1].max})" @pager = pager(@files, rs(:list, '__page__', :sort => @sort, :order => @order)) end
new()
click to toggle source
# File lib/thoth/controller/media.rb, line 118 def new require_auth @title = "Upload Media" @form_action = rs(:new).to_s if request.post? error_403 unless form_token_valid? tempfile, filename, type = request[:file].values_at( :tempfile, :filename, :type) # Ensure that the filename is a name only and not a full path, since # certain browsers are stupid (I'm looking at you, IE). filename = filename[/([^\/\\]+)$/].strip if filename.empty? return @media_error = 'Error: Invalid filename.' end file = Media.new do |f| f.filename = filename f.mimetype = type || 'application/octet-stream' end begin unless File.directory?(File.dirname(file.path)) FileUtils.mkdir_p(File.dirname(file.path)) end FileUtils.mv(tempfile.path, file.path) file.save flash[:success] = 'File uploaded.' redirect(rs(:edit, file.id)) rescue => e @media_error = "Error: #{e}" end end end
Private Instance Methods
send_media(filename, content_type = nil)
click to toggle source
# File lib/thoth/controller/media.rb, line 161 def send_media(filename, content_type = nil) # This should eventually be eliminated in favor of using the frontend # server to send files directly without passing through Thoth/Ramaze. respond!(::File.open(filename, 'rb'), 200, 'Content-Length' => ::File.size(filename).to_s, 'Content-Type' => content_type || Rack::Mime.mime_type(::File.extname(filename)) ) end