attachment :file, uploader: FileUploader module SelectedAction def select_action_by_params params # skip action table lookups for current revision rev_id = params[:rev_id] super unless rev_id && rev_id == last_content_action_id end def last_content_action_id # find action id from content (saves lookups) db_content.to_s.split(/[\/\.]/)[1] end end include SelectedAction format do view :source do |_args| card.attachment.url end view :core do |args| handle_source args do |source| card_url source end end def handle_source args source = _render_source args source ? yield(source) : '' rescue 'File Error' end end format :file do view :core do |_args| # returns send_file args. not in love with this... if format = card.attachment_format(params[:format]) # this means we only support known formats. dislike. if params[:explicit_file] && (r = controller.response) r.headers['Expires'] = 1.year.from_now.httpdate # r.headers["Cache-Control"] = "public" # currently using default "private", because proxy servers could block needed permission checks end # elsif ![format, 'file'].member? params[:format] # formerly supported redirecting to correct file format # return redirect_to( request.fullpath.sub( /\.#{params[:format]}\b/, '.' + format ) ) #card.attachment.url(style) ) file = selected_file_version [file.path, { type: file.content_type, filename: "#{card.cardname.safe_key}#{file.extension}", x_sendfile: true, disposition: (params[:format] == 'file' ? 'attachment' : 'inline') } ] else _render_not_found end end def selected_file_version card.attachment end end format :html do view :core do |args| handle_source args do |source| "Download #{showname args[:title]}" end end view :editor do |args| file_chooser args end def preview _args '' end view :preview_editor, tags: :unknown_ok do |args| cached_upload_card_name = Card::Env.params[:attachment_upload] cached_upload_card_name.gsub!(/\[\w+\]$/, '[action_id_of_cached_upload]') <<-HTML
HTML end def file_chooser args <<-HTML
#{preview(args)} #{card.new_card? ? 'Add' : 'Replace'} #{card.attachment_name}... #{hidden_field_tag 'attachment_type_id', card.type_id} #{hidden_field card.attachment_name, class: 'attachment_card_name', value: ''} #{hidden_field_tag 'file_card_name', card.cardname.url_key}
HTML end end