match_mongo_chunk_size/lib/mongoid-grid_fs.rb

494 lines
12 KiB
Ruby
Raw Normal View History

2012-07-23 06:29:15 +00:00
##
#
2012-07-25 03:09:51 +00:00
module Mongoid
class GridFS
const_set :Version, '1.2.1'
2012-07-24 00:23:48 +00:00
2012-07-25 03:09:51 +00:00
class << GridFS
def version
const_get :Version
end
2012-07-24 00:23:48 +00:00
2012-07-25 03:09:51 +00:00
def dependencies
{
'mongoid' => [ 'mongoid' , '>= 3.0.1' ] ,
'mime/types' => [ 'mime-types' , '>= 1.19' ] ,
}
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def libdir(*args, &block)
@libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
args.empty? ? @libdir : File.join(@libdir, *args)
ensure
if block
begin
$LOAD_PATH.unshift(@libdir)
block.call()
ensure
$LOAD_PATH.shift()
end
2012-07-23 06:29:15 +00:00
end
2012-07-24 00:23:48 +00:00
end
2012-07-25 03:09:51 +00:00
def load(*libs)
libs = libs.join(' ').scan(/[^\s+]+/)
libdir{ libs.each{|lib| Kernel.load(lib) } }
end
2012-07-23 06:29:15 +00:00
end
2012-07-25 03:09:51 +00:00
begin
require 'rubygems'
rescue LoadError
nil
2012-07-24 00:23:48 +00:00
end
2012-07-25 03:09:51 +00:00
if defined?(gem)
dependencies.each do |lib, dependency|
gem(*dependency)
require(lib)
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
end
2012-07-25 03:09:51 +00:00
require "digest/md5"
require "cgi"
2012-07-24 00:23:48 +00:00
end
2012-07-25 03:09:51 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
##
#
module Mongoid
class GridFS
class << GridFS
attr_accessor :namespace
attr_accessor :file_model
attr_accessor :chunk_model
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def init!
GridFS.build_namespace_for(:Fs)
GridFS.namespace = Fs
GridFS.file_model = Fs.file_model
GridFS.chunk_model = Fs.chunk_model
const_set(:File, Fs.file_model)
const_set(:Chunk, Fs.chunk_model)
to_delegate = %w(
put
get
delete
find
[]
[]=
clear
)
to_delegate.each do |method|
class_eval <<-__
def GridFS.#{ method }(*args, &block)
::GridFS::Fs::#{ method }(*args, &block)
end
__
end
2012-07-24 00:23:48 +00:00
end
2012-07-25 03:09:51 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
##
#
def GridFS.namespace_for(prefix)
prefix = prefix.to_s.downcase
const = "::GridFS::#{ prefix.to_s.camelize }"
namespace = const.split(/::/).last
const_defined?(namespace) ? const_get(namespace) : build_namespace_for(namespace)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
##
#
def GridFS.build_namespace_for(prefix)
prefix = prefix.to_s.downcase
const = prefix.camelize
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
namespace =
Module.new do
module_eval(&NamespaceMixin)
self
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
const_set(const, namespace)
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
file_model = build_file_model_for(namespace)
chunk_model = build_chunk_model_for(namespace)
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
file_model.namespace = namespace
chunk_model.namespace = namespace
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
file_model.chunk_model = chunk_model
chunk_model.file_model = file_model
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
namespace.prefix = prefix
namespace.file_model = file_model
namespace.chunk_model = chunk_model
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
namespace.send(:const_set, :File, file_model)
namespace.send(:const_set, :Chunk, chunk_model)
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
#at_exit{ file_model.create_indexes rescue nil }
#at_exit{ chunk_model.create_indexes rescue nil }
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
const_get(const)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
NamespaceMixin = proc do
class << self
attr_accessor :prefix
attr_accessor :file_model
attr_accessor :chunk_model
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def to_s
prefix
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def namespace
prefix
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def put(readable, attributes = {})
chunks = []
file = file_model.new
attributes.to_options!
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
if attributes.has_key?(:id)
file.id = attributes.delete(:id)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
if attributes.has_key?(:_id)
file.id = attributes.delete(:_id)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
if attributes.has_key?(:content_type)
attributes[:contentType] = attributes.delete(:content_type)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
if attributes.has_key?(:upload_date)
attributes[:uploadDate] = attributes.delete(:upload_date)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
md5 = Digest::MD5.new
length = 0
chunkSize = file.chunkSize
n = 0
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
GridFS.reading(readable) do |io|
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
filename =
attributes[:filename] ||=
[file.id.to_s, GridFS.extract_basename(io)].join('/').squeeze('/')
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
content_type =
attributes[:contentType] ||=
GridFS.extract_content_type(filename) || file.contentType
2012-07-24 00:23:48 +00:00
2012-07-25 03:09:51 +00:00
GridFS.chunking(io, chunkSize) do |buf|
md5 << buf
length += buf.size
chunk = file.chunks.build
chunk.data = binary_for(buf)
chunk.n = n
n += 1
chunk.save!
chunks.push(chunk)
end
2012-07-24 00:23:48 +00:00
2012-07-25 03:09:51 +00:00
end
attributes[:length] ||= length
attributes[:uploadDate] ||= Time.now.utc
attributes[:md5] ||= md5.hexdigest
file.update_attributes(attributes)
2012-07-24 00:23:48 +00:00
2012-07-25 03:09:51 +00:00
file.save!
file
ensure
chunks.each{|chunk| chunk.destroy rescue nil} if $!
2012-07-24 00:23:48 +00:00
end
2012-07-25 03:09:51 +00:00
if defined?(Moped)
def binary_for(*buf)
Moped::BSON::Binary.new(:generic, buf.join)
end
else
def binary_for(buf)
BSON::Binary.new(buf.bytes.to_a)
end
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def get(id)
file_model.find(id)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def delete(id)
file_model.find(id).destroy
rescue
nil
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def where(conditions = {})
case conditions
when String
file_model.where(:filename => conditions)
else
file_model.where(conditions)
end
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def find(*args)
where(*args).first
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def [](filename)
file_model.where(:filename => filename.to_s).first
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def []=(filename, readable)
file = self[filename]
file.destroy if file
put(readable, :filename => filename.to_s)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def clear
file_model.destroy_all
end
2012-07-24 04:08:14 +00:00
2012-07-25 03:09:51 +00:00
# TODO - opening with a mode = 'w' should return a GridIO::IOProxy
# implementing a StringIO-like interface
#
def open(filename, mode = 'r', &block)
raise NotImplementedError
end
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
end
2012-07-25 03:09:51 +00:00
##
#
def GridFS.build_file_model_for(namespace)
prefix = namespace.name.split(/::/).last.downcase
file_model_name = "#{ namespace.name }::File"
chunk_model_name = "#{ namespace.name }::Chunk"
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
Class.new do
include Mongoid::Document
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
singleton_class = class << self; self; end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
singleton_class.instance_eval do
define_method(:name){ file_model_name }
attr_accessor :chunk_model
attr_accessor :namespace
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
self.default_collection_name = "#{ prefix }.files"
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
field(:filename, :type => String)
field(:contentType, :type => String, :default => 'application/octet-stream')
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
field(:length, :type => Integer, :default => 0)
field(:chunkSize, :type => Integer, :default => (256 * (2 ** 20)))
field(:uploadDate, :type => Date, :default => Time.now.utc)
field(:md5, :type => String, :default => Digest::MD5.hexdigest(''))
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
%w( filename contentType length chunkSize uploadDate md5 ).each do |f|
validates_presence_of(f)
end
validates_uniqueness_of(:filename)
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
has_many(:chunks, :class_name => chunk_model_name, :inverse_of => :files, :dependent => :destroy, :order => [:n, :asc])
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
index({:filename => 1}, :unique => true)
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def path
filename
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def basename
::File.basename(filename)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def prefix
self.class.namespace.prefix
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def each(&block)
chunks.all.order_by([:n, :asc]).each do |chunk|
block.call(chunk.to_s)
end
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def data
data = ''
each{|chunk| data << chunk}
data
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def base64
Array(to_s).pack('m')
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def data_uri(options = {})
data = base64.chomp
"data:#{ content_type };base64,".concat(data)
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def bytes(&block)
if block
each{|data| block.call(data)}
length
else
bytes = []
each{|data| bytes.push(*data)}
bytes
end
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def close
self
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def content_type
contentType
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def update_date
updateDate
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def created_at
updateDate
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def namespace
self.class.namespace
end
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
end
2012-07-25 03:09:51 +00:00
##
#
def GridFS.build_chunk_model_for(namespace)
prefix = namespace.name.split(/::/).last.downcase
file_model_name = "#{ namespace.name }::File"
chunk_model_name = "#{ namespace.name }::Chunk"
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
Class.new do
include Mongoid::Document
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
singleton_class = class << self; self; end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
singleton_class.instance_eval do
define_method(:name){ chunk_model_name }
attr_accessor :file_model
attr_accessor :namespace
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
self.default_collection_name = "#{ prefix }.chunks"
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
field(:n, :type => Integer, :default => 0)
field(:data, :type => (defined?(Moped) ? Moped::BSON::Binary : BSON::Binary))
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
belongs_to(:file, :foreign_key => :files_id, :class_name => file_model_name)
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
index({:files_id => 1, :n => -1}, :unique => true)
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def namespace
self.class.namespace
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def to_s
data.data
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
alias_method 'to_str', 'to_s'
end
2012-07-23 06:29:15 +00:00
end
2012-07-25 03:09:51 +00:00
##
#
def GridFS.reading(arg, &block)
if arg.respond_to?(:read)
rewind(arg) do |io|
block.call(io)
end
else
open(arg.to_s) do |io|
block.call(io)
end
2012-07-24 00:23:48 +00:00
end
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def GridFS.chunking(io, chunk_size, &block)
if io.method(:read).arity == 0
data = io.read
i = 0
loop do
offset = i * chunk_size
length = i + chunk_size < data.size ? chunk_size : data.size - offset
2012-07-25 03:09:51 +00:00
break if offset >= data.size
2012-07-25 03:09:51 +00:00
buf = data[offset, length]
block.call(buf)
i += 1
end
else
while((buf = io.read(chunk_size)))
block.call(buf)
end
end
end
2012-07-24 00:23:48 +00:00
2012-07-25 03:09:51 +00:00
def GridFS.rewind(io, &block)
2012-07-24 00:23:48 +00:00
begin
2012-07-25 03:09:51 +00:00
pos = io.pos
io.flush
io.rewind
2012-07-24 00:23:48 +00:00
rescue
nil
end
2012-07-25 03:09:51 +00:00
begin
block.call(io)
ensure
begin
io.pos = pos
rescue
nil
end
end
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def GridFS.extract_basename(object)
filename = nil
[:original_path, :original_filename, :path, :filename, :pathname].each do |msg|
if object.respond_to?(msg)
filename = object.send(msg)
break
end
2012-07-24 00:23:48 +00:00
end
2012-07-25 03:09:51 +00:00
filename ? cleanname(filename) : nil
2012-07-23 06:29:15 +00:00
end
2012-07-25 03:09:51 +00:00
def GridFS.extract_content_type(filename)
content_type = MIME::Types.type_for(::File.basename(filename.to_s)).first
content_type.to_s if content_type
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
def GridFS.cleanname(pathname)
basename = ::File.basename(pathname.to_s)
CGI.unescape(basename).gsub(%r/[^0-9a-zA-Z_@)(~.-]/, '_').gsub(%r/_+/,'_')
end
2012-07-24 00:23:48 +00:00
end
2012-07-23 06:29:15 +00:00
2012-07-25 03:09:51 +00:00
GridFs = GridFS
GridFS.init!
end