The :filename interpolation and files without extension

577 views
Skip to first unread message

srih4ri

unread,
Oct 22, 2010, 11:54:36 AM10/22/10
to Paperclip Plugin
Hi all,
I am new here , I have started using paperclip since yesterday . I had
the something like this :

In PersonsUploads.rb

has_attachment :upload , :path=>":rails_root/public/
uploads/:id/:filename"

Now i had an action for downloading file which looked like ,

def download_attachment
@personupload = PersonsUploads.find params[:id]
sendfile "#{Rails.root}/public/uploads/#{@personupload.id}/
@personupload.attachment_file_name"
end

Now the problem is , when a file without extension is uploaded , say
"song" is uploaded not "song.mp3" , the attachment_file_name is "song"
but the :filename in :path is returned as "song." the uploaded file is
saved with a trailing ".' -- in short , my download_attachment action
fails.

I modified the interpolation.rb file in plugin to overcome this
trouble , is this the right way to do this ?

--
srihari

Dennis

unread,
Oct 26, 2010, 6:29:36 AM10/26/10
to Paperclip Plugin
Do you actually want to allow song files to be uploaded without an
extension?
If not, you then can use the standard Paperclip approach which is to
validate the file content type as follows:

has_attachment :upload , :path=>":rails_root/public/
uploads/:id/:filename"

validates_attachment_content_type :upload , :content_type=>['audio/
basic', 'audio/mpeg'],
:message => 'can only be au, mp3.'

For additional audio mime types see:

http://www.w3schools.com/media/media_mimeref.asp

> Now the problem is , when a file without extension is uploaded , say
> "song" is uploaded not "song.mp3" , the attachment_file_name is "song"
> but the :filename in :path is returned as "song." the uploaded file is
> saved with a trailing ".' -- in short , my download_attachment action
> fails.
>
> srihari

Jon Yurek

unread,
Oct 29, 2010, 1:43:47 PM10/29/10
to papercli...@googlegroups.com
Hi,

Well, the first thing I would do is not build the path yourself and, instead, use

sendfile @personupload.attachment.path

which generates the path that paperclip uses to save the file.

But, yes, there's no problem with modifying the interpolations to do what you need them to do.

> --
> Individuals over processes. Interactions over tools.
>
> Agile Rails training from thoughtbot, the makers of Paperclip, Clearance, Shoulda, & Factory Girl:
> http://thoughtbot.com/services/training
>
> The Paperclip group:
> http://groups.google.com/group/paperclip-plugin
>
> To post to this group, send email to
> papercli...@googlegroups.com
>
> To unsubscribe from this group, send email to
> paperclip-plug...@googlegroups.com


--
Jonathan Yurek, Founder and CTO
thoughtbot, inc.
617.482.1300 x114
http://thoughtbot.com/
http://twitter.com/thoughtbot

srih4ri

unread,
Oct 31, 2010, 1:49:03 PM10/31/10
to Paperclip Plugin


On Oct 29, 10:43 pm, Jon Yurek <jyu...@thoughtbot.com> wrote:
> Hi,
>
> Well, the first thing I would do is not build the path yourself and, instead, use
>
>     sendfile @personupload.attachment.path
>
> which generates the path that paperclip uses to save the file.
>
> But, yes, there's no problem with modifying the interpolations to do what you need them to do.
>

I am sorry for replying late . I thought i was subscribed to the group
but i was not.. so i was thinking nobody replied :)

@Dennis
Thanks for the tip , it was not what actually i needed. It will help
for sure .

@Jon Yurek
Thanks for the reply , Yes using sendfile
@personupload.attachment.path was the right way for me , instead of
trying to write up the whole path myself . Using attachment.path
returns the correct filename where it is stored (with the trailing dot
for extension less files) . That removes the need for the change i
made in the interpolations.rb file

Cheers,
Srihari
Reply all
Reply to author
Forward
0 new messages