知易行难

Posts Tagged ‘attachment_fu’

attachment_fu: how to delete the original file

Aug 26, 2008

Tags

In one of our ongoing rails projects, we need to delete the original file after the user uploads an image file.

We use attachment_fu to handle file uploading. My first try was to delete the file in the after_create block of the Photo class, but it seemed at that time the file hasn’t been copied to the directory yet.

Fortunately attachment_fu provides an after_attachment_saved callback which will be called after an attachment has been saved to the file system. That’s exact what we need to get our job done:

class Photo < ActiveRecord::Base
    has_attachment options_for_attachment_fu

    after_attachment_saved do |foto|
        # delete original file for disk space saving 
        FileUtils.rm foto.full_filename if foto.parent.nil?
    end

end

Very handy. Be aware though now calling the public_filename without a :thumbnail parameter on a Photo instance would return an image url that doesn’t exist.

See also: Use S3 Virtual Hosting Name With attachment_fu

attachment_fu使用自定义域名的S3服务

Mar 2, 2008

Tags

Rails插件attachment_fu提供非常棒的文件上传管理功能。它支持文件系统、数据库及Amazon S3三种方式来存储上传的文件。

要使用S3这种存储方式,仅需在作为上传对象的Model类中(以Asset为例)调用带:storage => :s3选项的has_attachment方法。实际效果是,上传文件后,通过访问新创建的Asset对象的public_filename方法,便可获得形如http://s3.amazonaws.com/mybucketname/assets/1/file.ext这样的URL。

Continue reading»

Archives: Monthly or