Class: Thoth::MediaController
- Inherits:
-
Controller
- Object
- Ramaze::Controller
- Controller
- Thoth::MediaController
- Defined in:
- lib/thoth/controller/media.rb
Instance Method Summary collapse
- #delete(id = nil) ⇒ Object
- #edit(id = nil) ⇒ Object
- #index(filename = nil) ⇒ Object
- #list(page = 1) ⇒ Object
- #new ⇒ Object
Methods inherited from Controller
Instance Method Details
#delete(id = nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# 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) ⇒ Object
37 38 39 40 41 42 43 44 |
# 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) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# 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 ⇒ Object
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 |
# 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 |