[导入]generic file and image models for uploaded files
2007-12-07 17:32
543 查看
These are basic models that store a file in a dedicated files table. Use has_one or has_many to associate this with your actual models. RMagick is required for images.
This is my first code dealing with uploads and rmagick, so please comment if you have suggestions.
Controller Usage:
Model Usage:
文章来源:http://snippets.dzone.com/posts/show/793
This is my first code dealing with uploads and rmagick, so please comment if you have suggestions.
class DbFile < ActiveRecord::Base IMAGE_TYPES = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png'] before_validation :sanitize_filename validates_presence_of :size, :filename, :content_type class << self def new_file(file_data) content_type = file_data.content_type.strip (IMAGE_TYPES.include?(content_type) ? DbImage : DbFile).new \ :data => file_data.read, :filename => file_data.original_filename, :size => file_data.size, :content_type => content_type end end protected def sanitize_filename # NOTE: File.basename doesn't work right with Windows paths on Unix # get only the filename, not the whole path filename.gsub! /^.*(\\|\/)/, '' # Finally, replace all non alphanumeric, underscore or periods with underscore filename.gsub! /[^\w\.\-]/, '_' end end require 'rmagick' require 'base64' class DbImage < DbFile def data=(file_data) with_image(file_data, true) do |img| self.width = img.columns self.height = img.rows end end def with_image(file_data = nil, save_image = false, &block) img = Magick::Image::read_inline(Base64.b64encode(file_data || self.data)).first block.call(img) write_attribute('data', img.to_blob) if save_image img = nil GC.start end end
Controller Usage:
# returns DbImage if content_type matches db_file = DbFile.new_file(params[:file][:data]) db_file.save
Model Usage:
# raw binary image data File.open('my_file', 'w') { |f| f.write(db_file.data) } # Image resizing with rmagick # automatically creates RMagick::Image and # invokes GC.start db_file.with_image do |img| img.scale(.25) img.write('thumb.jpg') end
文章来源:http://snippets.dzone.com/posts/show/793
相关文章推荐
- Exploring Models and Data for Image Question Answering
- SharePoint 报错: The specified file or folder name is too long. The URL path for all files and folders must be 260 characters or less
- [导入]Wiley - VERIFICATION AND VALIDATION FOR QUALITY OF UML 2.0 MODELS
- Downloading files from a server to client, using ASP.Net, when file size is too big for MemoryStream using Generic Handlers (ashx)
- Generic Macro for UNICODE and ANSI
- Domain Transform for Edge-Aware Image and Video Processing - 论文阅读
- WCF: Generate Proxy Class and Configuration file for Client
- File Comparer - To compare two files and check whether they have the same content
- Magick++: NoDecodeDelegateForThisImageFormat with ICO file
- One or more files from the Emulator for Windows CE installation is missing.Please reinstall Emulator for Windows CE and try agai
- vs2010水晶报表问题"未能加载文件或程序集“file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common.."
- System and method for parallel execution of memory transactions using multiple memory models, including SSO, TSO, PSO and RMO
- HTTP Handler to Combine Multiple Files, Cache and Deliver Compressed Output for Faster Page Load
- hbase启动报错hbase-default.xml file seems to be for and old version of HBase
- Query-Adaptive Late Fusion for Image Search and Person Re-identification阅读笔记
- BaiXiang——【arXi2015】An End-to-End Trainable Neural Network for Image-based Sequence Recognition and
- error This file requires compiler and library support for the ISO C++ 2011 standard
- 笔记:Structure-Constrained Low-Rank and Partial Sparse Representation with Sample Selection for image
- [Yii Framework] A component for creating and getting the thumb image path
- How to create .lib file when you only have .dll and .h files