Viewing attachments inline: MIME type fix

Normally RForum attachments don’t have a content type set when RForum
sends the file to the browser, so application/octet-stream is used as a
default and most browsers pop up a “Save as” dialogue. If you would
prefer files to be shown inline (particularly handy for images) then the
following achieves it.

(1) Install mime-types gem: ‘gem install mime-types’
(2) Alter ‘app/controllers/attachment_controller.rb’:

Index: app/controllers/attachment_controller.rb

— app/controllers/attachment_controller.rb (revision 44)
+++ app/controllers/attachment_controller.rb (working copy)
@@ -1,7 +1,14 @@
+require ‘mime/types’
+
class AttachmentController < ApplicationController
def fetch

  • id = @params[:id]
  • send_file Attachment.find(id).file_path
  • id = @params[:id]
  • file_path = Attachment.find(id).file_path
  • mime_type = MIME::Types.type_for(file_path) ||
    ‘application/octet-stream’
  • send_file file_path,
  •          :type        => mime_type[0].content_type,
    
  •          :disposition => 'inline'
    

[Rest of the diff snipped]

Does the job for me. I’ve not submitted this as a patch / ticket as it
introduces a new external dependency so I don’t expect it to be too
interesting as a general feature addition, but I’m happy to do so if
people on the list think it worthwhile.

Hi Andrew H. :
I do want you write,but after i uploaded a picture(.jpg), i download
the picture,the browser just redirect to a URL some like
http://localhost/attachment/3/32680.jpg” and the templete is just the
same words. i can’t download the picture.

i fixed my problem,but there is still a few question:

  1. i suggest to modify
    ===index attachement.rb===
    def after_save

    Create attachment directory

    begin
    Dir.mkdir(self.dir_path)
    rescue Errno::EEXIST
    end

    Save uploaded file to attachment directory

  • File.open(self.file_path, ‘w’) do |f|
  • File.open(self.file_path, ‘wb’) do |f|
    f.write(@data)
    end
    @data = nil
    end

if do not do that ,i won’t see upload file(images,or some file else)

2.if the upload file(test.txt) contain some chinese words ,it seems the
browser can’t decode these words

my os is windows xp.

if we upload the attachment file(e.g demo1.jpg), can we edit the
attachment file to anthoer(e.g demo2.jpg)? it seems i can’t change it.
current version don’t surpport it?

Vincent Tsao wrote:

  • File.open(self.file_path, ‘w’) do |f|
  • File.open(self.file_path, ‘wb’) do |f|

Well spotted; the platform I’m running RForum on doesn’t seem to care
about ‘w’ vs ‘wb’ given that attachments are saved correctly. Yours, on
the other hand, must. Out of interest, what operating system are you
using?

2.if the upload file(test.txt) contain some chinese words ,it seems the
browser can’t decode these words

That’s presumably because the file ‘test.txt’ by itself contains no
indication of the character set in which the text file is encoded. Plain
text files, unlike HTML, have no mechanism for indicating this. I don’t
think this has any connection with RForum. You probably need to manually
set the encoding type (e.g. in Firefox, use the “View -> Character
encoding” menu), set the default encoding type system-wide (the way to
do this depends on your operating system and the browser might ignore it
anyway), or just load the file into a text editor that you know will
display the Chinese words properly.