Yes, but not with Tempfile. Hash the video data, store your thumbnail as $HASH.png, keep a reference to this file and be sure to delete it when the user performs such actions as make the thumbnail no longer necessary.
On Thu, Nov 11, 2010 at 3:29 PM, Christian Fazzini <[email protected]> wrote: > Basically I need to create a new tempfile with a png extension. Is > this possible? > > On Nov 12, 4:21 am, Christian Fazzini <[email protected]> > wrote: >> No, the following saves the temp file. >> >> @video.attachment_thumbnail = >> File.open(File.expand_path(tmp.path)) >> >> However, File.expand_path(tmp.path) saves the file in the file system >> as thumb.png20101111-989-1pt72my >> >> Ideally, I would like it to save it as thumb20101111-989-1pt72my.png >> or, better, just thumb.png >> >> On Nov 12, 4:12 am, Brian Troutwine <[email protected]> wrote: >> >> > I'm confused. You're creating a tempfile and then keeping a reference >> > to it in your database? >> >> > On Thu, Nov 11, 2010 at 3:06 PM, Christian Fazzini >> >> > <[email protected]> wrote: >> > > Ok that makes sense. Thanks Brian. >> >> > > On the same topic... >> >> > > How would I create a temp file with .png extension in the end. >> >> > > tmp = Tempfile.new([ 'thumb', 'png' ].compact.join(".")) >> >> > You can't use Tempfile to do this. Well, you _can_ but you'll need to >> > close the file--without unlinking it!--and then move the file created >> > to the new location you want. At that points, you're better off just >> > giving thumbnails a sequential identifier, or use their hash. Destroy >> > the thumbnail file once you no longer need it. >> >> > > Whenever it creates a temp file, it creates it as such: >> > > thumb.png20101111-989-1pt72my, instead of >> > > thumb20101111-989-1pt72my.png >> >> > > If I try to save this via Active Record, with >> >> > > @video.attachment_thumbnail = >> > > File.open("#{File.expand_path(tmp.path)}"), >> >> > > It will save the file in the file system without the extension, which >> > > makes it impossible to read on the browser, unless I explicitly set >> > > the extension. >> >> > > On Nov 12, 3:54 am, Brian Troutwine <[email protected]> wrote: >> > >> Hi Christian, >> >> > >> Not by the end of the day, no. Each tempfile will be unlinked as soon >> > >> as you close it. Your example code didn't have the line >> >> > >> > dst.close(unlink_now=true) >> >> > >> in it. You may also use dst.close!() to the same effect. >> >> > >> -- >> > >> Brian >> >> > >> On Thu, Nov 11, 2010 at 2:49 PM, Christian Fazzini >> >> > >> <[email protected]> wrote: >> > >> > Hi Brian, >> >> > >> > Just to keep things clear. By the end of the day, /var/folders/uL/ >> > >> > uL0bYOOZEZaJH5E+BmDJVE+++TI/-Tmp-/ <-- temp dir _IS_ supposed to be >> > >> > empty. Is this correct? >> >> > >> > On Nov 12, 3:35 am, Brian Troutwine <[email protected]> wrote: >> > >> >> When you close the Tempfile, be sure to pass true for unlink_now. >> >> > >> >>http://ruby-doc.org/stdlib/libdoc/tempfile/rdoc/classes/Tempfile.html... >> >> > >> >> On Thu, Nov 11, 2010 at 2:33 PM, Christian Fazzini >> >> > >> >> <[email protected]> wrote: >> > >> >> > In video controller, create action. I am using FFMPEG to generate a >> > >> >> > thumbnail of videos that are uploaded. >> >> > >> >> > The tempfile is created as such: >> >> > >> >> > def create >> > >> >> > ...... >> > >> >> > dst = Tempfile.new(basename) >> > >> >> > dst.binmode >> >> > >> >> > system("ffmpeg -itsoffset -4 -i #{file.path} -y -vcodec mjpeg - >> > >> >> > vframes 1 -an -f rawvideo #{File.expand_path(dst.path)}.png") >> > >> >> > .... >> > >> >> > end >> >> > >> >> > As you can see from above, ffmpeg outputs a file to the var dst >> > >> >> > which >> > >> >> > is an instance of Tempfile. In console, when I do: >> >> > >> >> > ls /var/folders/uL/uL0bYOOZEZaJH5E+BmDJVE+++TI/-Tmp-/ >> >> > >> >> > thumb20101111-989-1gflwhr.png >> > >> >> > thumb20101111-989-1pt72my.png >> > >> >> > thumb20101111-989-kjcsgd.png >> > >> >> > thumb20101111-989-3ocf15.png >> > >> >> > thumb20101111-989-10azwnn.png >> > >> >> > thumb20101111-989-7x11w7.png >> >> > >> >> > I see a bunch of files in the directory. It seems that rails did >> > >> >> > not >> > >> >> > automatically delete these temp files. Should I manually delete >> > >> >> > these >> > >> >> > files? >> >> > >> >> > I am worried that, if my temp dir fills up with temp files, it'll >> > >> >> > eventually take up most of the server space... >> >> > >> >> > Your thoughts on this matter? >> >> > >> >> > -- >> > >> >> > You received this message because you are subscribed to the Google >> > >> >> > Groups "Ruby on Rails: Talk" group. >> > >> >> > To post to this group, send email to >> > >> >> > [email protected]. >> > >> >> > To unsubscribe from this group, send email to >> > >> >> > [email protected]. >> > >> >> > For more options, visit this group >> > >> >> > athttp://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > >> > -- >> > >> > You received this message because you are subscribed to the Google >> > >> > Groups "Ruby on Rails: Talk" group. >> > >> > To post to this group, send email to >> > >> > [email protected]. >> > >> > To unsubscribe from this group, send email to >> > >> > [email protected]. >> > >> > For more options, visit this group >> > >> > athttp://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > -- >> > > You received this message because you are subscribed to the Google >> > > Groups "Ruby on Rails: Talk" group. >> > > To post to this group, send email to [email protected]. >> > > To unsubscribe from this group, send email to >> > > [email protected]. >> > > For more options, visit this group >> > > athttp://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

