carrierware gridfs 图片上传

rails mysql mongoid carrierware gridfs 实现图片上传功能

1.安装

Git: git的安装
MongoDB: MongoDB安装
Nginx: Nginx安装
注: 官方的nginx没有nginx-gridfs功能,需要手动编译进去

安装步骤:
[bash]
cd /tmp
git clone https://github.com/mdirolf/nginx-gridfs
cd nginx-gridfs/
git submodule init
git submodule update
#在 gcc 版本 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) 上编译时,可能会遇到问题。可以在configure时加上
./configure –add-module=/path/to/nginx-gridfs/source/ –with-cc-opt=-Wno-missing-field-initializers
make && make install

#编辑nginx配置文件
vim /usr/local/nginx/conf/nginx.conf

#注: gridfs 指路由名
location /gridfs/ {
gridfs my_app
root_collection=pics
field=_id
type=int
user=foo
pass=bar;
mongo 127.0.0.1:27017;
}
[/bash]
参考: https://github.com/mdirolf/nginx-gridfs

http://serverfault.com/questions/345516/compiling-nginx-with-nginx-gridfs-getting-mongo-c-driver-errors-during-make

二.rails配置

1,搭建rails环境 -> rvm 参考:rails环境搭建
[bash]
vim Gemfile
source ‘http://rubygems.org’
gem ‘rails’
gem ‘mysql2′
gem ‘carrierwave’
gem ‘mini_magick’
gem ‘mongo’
gem ‘bson’
gem ‘bson_ext’

bundle install
[/bash]

2,创建一个rails项目
[bash]
rails new product –database=mysql
rails g scaffold photo name:string image:string #image存储图片名
[/bash]

3,通过generator生成uploader 文件在app/uploader/base_uploader.rb
[bash]
rails g uploader base
#以上创建的父类文件,可以在app/uploader下创建子类文件用于不同的图片上传需要
#修改base_uploader.rb文件并加入MiniMagic,用来对图片进行处理,改变大小等。
# encoding: utf-8
class BaseUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :grid_fs
end

vim app/uploaders/photo_uploader.rb
# encoding: utf-8
class PhotoUploader < BaseUploader
process :resize_to_limit => [800, 800]
version :thumb do
process :resize_to_limit => [30, 30]
end
end
[/bash]

4, 在model中 mount uploader到image 字段中
[bash]
class Photo < ActiveRecord::Base
attr_accessible :image, :name
mount_uploader :image, PhotoUploader
end
[/bash]

5.在config/initializers/下创建文件carrierwave.rb配置mongodb
[bash]
vim config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.grid_fs_database = ‘photos’ #数据库名
config.grid_fs_host = ‘localhost’ #mongodb地址
config.grid_fs_access_url = "/uploader" #好像是访问时图片url的前缀
end
[/bash]

6.html模板部分
[bash]
#form
<div class="field">
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
#index
<%= image_tag photo.image %>
<%= image_tag photo.image_url(:thumb) %>
[/bash]

7. 启动部分
如果是在develop模式下,需要配置路由已经创建controller文件
[bash]
vim app/controller/gridfs_controller.rb

class GridfsController < ActionController::Metal
def serve
gridfs_path = env["PATH_INFO"].gsub("/upload/", "")
begin
gridfs_file = Mongo::GridFileSystem.new(Mongo::DB.new(‘photos’, Mongo::Connection.new(‘localhost’))).open(gridfs_path, ‘r’)
self.response_body = gridfs_file.read
self.content_type = gridfs_file.content_type
rescue Exception => e
self.status = :file_not_found
self.content_type = ‘text/plain’
self.response_body = ”
raise e
end
end
end

vim config/routes.rb
if Rails.env.development?
match "/upload/*path" => "gridfs#serve"
end
[/bash]

8.配置nginx
[bash]
location /images/ {
gridfs photos root_collection=fs field=filename type=string;
mongo 127.0.0.1:27017;
}
[/bash]

9. 安装 nginx-gridfs 模块

注意事项: 如果需要使用nginx直接读取mongo gridfs, 需要重新编辑nginx 安装nginx-gridfs模块
官方的nginx没有nginx-gridfs功能,因此需要手动编译进去。
安装步骤见项目的github主页: https://github.com/mdirolf/nginx-gridfs

遇到的问题及解决方法:
[bash]
# 在 gcc 版本 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) 上编译时,可能会遇到问题。可以在configure时加上
–with-cc-opt=-Wno-missing-field-initializers

# 选项试试。( 见 http://serverfault.com/questions/345516/compiling-nginx-with-nginx-gridfs-getting-mongo-c-driver-errors-during-make )

# 当make是出现错误
#错误: 变量‘chunksize’被设定但未被使用 [-Werror=unused-but-set-variable]
#cc1: all warnings being treated as errors
#去掉objs/makefile中 -Werror这样一段,重新make就ok了
[/bash]

This entry was posted in ruby on Rails

Leave a Reply

电子邮件地址不会被公开。

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">