Class: Mongoid::GridFS

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid-grid_fs.rb,
lib/mongoid-grid_fs.rb

Defined Under Namespace

Classes: Defaults, Engine

Constant Summary collapse

NamespaceMixin =
proc do
  class << self
    attr_accessor :prefix
    attr_accessor :file_model
    attr_accessor :chunk_model

    def to_s
      prefix
    end

    def namespace
      prefix
    end

    def put(readable, attributes = {})
      chunks = []
      file = file_model.new
      attributes.to_options!

      if attributes.has_key?(:id)
        file.id = attributes.delete(:id)
      end

      if attributes.has_key?(:_id)
        file.id = attributes.delete(:_id)
      end

      if attributes.has_key?(:content_type)
        attributes[:contentType] = attributes.delete(:content_type)
      end

      if attributes.has_key?(:upload_date)
        attributes[:uploadDate] = attributes.delete(:upload_date)
      end

      md5 = Digest::MD5.new
      length = 0
      chunkSize = file.chunkSize
      n = 0

      GridFS.reading(readable) do |io|

        filename =
          attributes[:filename] ||=
            [file.id.to_s, GridFS.extract_basename(io)].join('/').squeeze('/')

        content_type =
          attributes[:contentType] ||=
            GridFS.extract_content_type(filename) || file.contentType

        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

      end

      attributes[:length] ||= length
      attributes[:uploadDate] ||= Time.now.utc
      attributes[:md5] ||= md5.hexdigest

      file.update_attributes(attributes)

      file.save!
      file
    rescue
      chunks.each{|chunk| chunk.destroy rescue nil}
      raise
    end

    def binary_for(*buf)
      if defined?(Moped::BSON)
        Moped::BSON::Binary.new(:generic, buf.join)
      else
        BSON::Binary.new(buf.join, :generic)
      end
    end

    def get(id)
      file_model.find(id)
    end

    def delete(id)
      file_model.find(id).destroy
    rescue
      nil
    end

    def where(conditions = {})
      case conditions
        when String
          file_model.where(:filename => conditions)
        else
          file_model.where(conditions)
      end
    end

    def find(*args)
      where(*args).first
    end

    def [](filename)
      file_model.where(:filename => filename.to_s).first
    end

    def []=(filename, readable)
      file = self[filename]
      file.destroy if file
      put(readable, :filename => filename.to_s)
    end

    def clear
      file_model.destroy_all
    end

  # 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
  end
end
MIME_TYPES =
{
  'md' => 'text/x-markdown; charset=UTF-8'
}
Helper =
Mongoid::GridFSHelper

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.chunk_modelObject

Returns the value of attribute chunk_model.



64
65
66
# File 'lib/mongoid-grid_fs.rb', line 64

def chunk_model
  @chunk_model
end

.file_modelObject

Returns the value of attribute file_model.



63
64
65
# File 'lib/mongoid-grid_fs.rb', line 63

def file_model
  @file_model
end

.namespaceObject

Returns the value of attribute namespace.



62
63
64
# File 'lib/mongoid-grid_fs.rb', line 62

def namespace
  @namespace
end

Class Method Details

.build_chunk_model_for(namespace) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/mongoid-grid_fs.rb', line 433

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"

  Class.new do
    include Mongoid::Document

    singleton_class = class << self; self; end

    singleton_class.instance_eval do
      define_method(:name){ chunk_model_name }
      attr_accessor :file_model
      attr_accessor :namespace
    end

    self.store_in :collection => "#{ prefix }.chunks"

    field(:n, :type => Integer, :default => 0)
    field(:data, :type => (defined?(Moped::BSON) ? Moped::BSON::Binary : BSON::Binary))

    belongs_to(:file, :foreign_key => :files_id, :class_name => file_model_name)

    index({:files_id => 1, :n => -1}, :unique => true)

    def namespace
      self.class.namespace
    end

    def to_s
      data.data
    end

    alias_method 'to_str', 'to_s'
  end
end

.build_file_model_for(namespace) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/mongoid-grid_fs.rb', line 290

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"

  Class.new do
    include Mongoid::Document
    include Mongoid::Attributes::Dynamic if Mongoid::VERSION.to_i >= 4

    singleton_class = class << self; self; end

    singleton_class.instance_eval do
      define_method(:name){ file_model_name }
      attr_accessor :namespace
      attr_accessor :chunk_model
      attr_accessor :defaults
    end

    self.store_in :collection => "#{ prefix }.files"

    self.defaults = Defaults.new

    self.defaults.chunkSize = 4 * (mb = 2**20)
    self.defaults.contentType = 'application/octet-stream'

    field(:filename, :type => String)
    field(:contentType, :type => String, :default => defaults.contentType)

    field(:length, :type => Integer, :default => 0)
    field(:chunkSize, :type => Integer, :default => defaults.chunkSize)
    field(:uploadDate, :type => Time, :default => Time.now.utc)
    field(:md5, :type => String, :default => Digest::MD5.hexdigest(''))

    %w( filename contentType length chunkSize uploadDate md5 ).each do |f|
      validates_presence_of(f)
    end
    validates_uniqueness_of(:filename)

    has_many(:chunks, :class_name => chunk_model_name, :inverse_of => :files, :dependent => :destroy, :order => [:n, :asc])

    index({:filename => 1}, :unique => true)

    def path
      filename
    end

    def basename
      ::File.basename(filename)
    end

    def prefix
      self.class.namespace.prefix
    end

    def each(&block)
      fetched, limit = 0, 7

      while fetched < chunks.size
        chunks.where(:n.lt => fetched+limit, :n.gte => fetched).
          order_by([:n, :asc]).each do |chunk|
            block.call(chunk.to_s)
          end

        fetched += limit
      end
    end

    def slice(*args)
      case args.first
        when Range
          range = args.first
          first_chunk = (range.min / chunkSize).floor
          last_chunk = (range.max / chunkSize).floor
          offset = range.min % chunkSize
          length = range.max - range.min + 1
        when Fixnum
          start = args.first
          start = self.length + start if start < 0
          length = args.size == 2 ? args.last : 1
          first_chunk = (start / chunkSize).floor
          last_chunk = ((start + length) / chunkSize).floor
          offset = start % chunkSize
      end

      data = ''

      chunks.where(:n => (first_chunk..last_chunk)).order_by(n: 'asc').each do |chunk|
        data << chunk
      end

      data[offset, length]
    end

    def data
      data = ''
      each{|chunk| data << chunk}
      data
    end

    def base64
      Array(to_s).pack('m')
    end

    def data_uri(options = {})
      data = base64.chomp
      "data:#{ content_type };base64,".concat(data)
    end

    def bytes(&block)
      if block
        each{|data| block.call(data)}
        length
      else
        bytes = []
        each{|data| bytes.push(*data)}
        bytes
      end
    end

    def close
      self
    end

    def content_type
      contentType
    end

    def update_date
      updateDate
    end

    def created_at
      updateDate
    end

    def namespace
      self.class.namespace
    end
  end
end

.build_namespace_for(prefix) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mongoid-grid_fs.rb', line 107

def GridFS.build_namespace_for(prefix)
  prefix = prefix.to_s.downcase
  const = prefix.camelize

  namespace =
    Module.new do
      module_eval(&NamespaceMixin)
      self
    end

  const_set(const, namespace)

  file_model = build_file_model_for(namespace)
  chunk_model = build_chunk_model_for(namespace)

  file_model.namespace = namespace
  chunk_model.namespace = namespace

  file_model.chunk_model = chunk_model
  chunk_model.file_model = file_model

  namespace.prefix = prefix
  namespace.file_model = file_model
  namespace.chunk_model = chunk_model

  namespace.send(:const_set, :File, file_model)
  namespace.send(:const_set, :Chunk, chunk_model)

  #at_exit{ file_model.create_indexes rescue nil }
  #at_exit{ chunk_model.create_indexes rescue nil }

  const_get(const)
end

.chunking(io, chunk_size, &block) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/mongoid-grid_fs.rb', line 484

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

      break if offset >= data.size

      buf = data[offset, length]
      block.call(buf)
      i += 1
    end
  else
    while((buf = io.read(chunk_size)))
      block.call(buf)
    end
  end
end

.cleanname(pathname) ⇒ Object



573
574
575
576
# File 'lib/mongoid-grid_fs.rb', line 573

def GridFS.cleanname(pathname)
  basename = ::File.basename(pathname.to_s)
  CGI.unescape(basename).gsub(%r/[^0-9a-zA-Z_@)(~.-]/, '_').gsub(%r/_+/,'_')
end

.dependenciesObject



12
13
14
15
16
17
# File 'lib/mongoid-grid_fs.rb', line 12

def dependencies
  {
    'mongoid'         => [ 'mongoid'         , '>= 3.0', '< 5.0' ] ,
    'mime/types'      => [ 'mime-types'      , '>= 1.19', '< 3.0'] ,
  }
end

.extract_basename(object) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/mongoid-grid_fs.rb', line 525

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
  end

  filename ? cleanname(filename) : nil
end

.extract_content_type(filename, options = {}) ⇒ Object



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/mongoid-grid_fs.rb', line 546

def GridFS.extract_content_type(filename, options = {})
  options.to_options!

  basename = ::File.basename(filename.to_s)
  parts = basename.split('.')
  parts.shift
  ext = parts.pop

  default =
    case
      when options[:default]==false
        nil
      when options[:default]==true
        "application/octet-stream"
      else
        (options[:default] || "application/octet-stream").to_s
    end

  content_type = mime_types[ext] || MIME::Types.type_for(::File.basename(filename.to_s)).first

  if content_type
    content_type.to_s
  else
    default
  end
end

.init!Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mongoid-grid_fs.rb', line 66

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 self.#{ method }(*args, &block)
        ::Mongoid::GridFS::Fs::#{ method }(*args, &block)
      end
    __
  end
end

.libdir(*args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid-grid_fs.rb', line 19

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
  end
end

.load(*libs) ⇒ Object



33
34
35
36
# File 'lib/mongoid-grid_fs.rb', line 33

def load(*libs)
  libs = libs.join(' ').scan(/[^\s+]+/)
  libdir{ libs.each{|lib| Kernel.load(lib) } }
end

.mime_typesObject



542
543
544
# File 'lib/mongoid-grid_fs.rb', line 542

def GridFS.mime_types
  MIME_TYPES
end

.namespace_for(prefix) ⇒ Object



98
99
100
101
102
103
# File 'lib/mongoid-grid_fs.rb', line 98

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

.reading(arg, &block) ⇒ Object



472
473
474
475
476
477
478
479
480
481
482
# File 'lib/mongoid-grid_fs.rb', line 472

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
  end
end

.rewind(io, &block) ⇒ Object



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/mongoid-grid_fs.rb', line 505

def GridFS.rewind(io, &block)
  begin
    pos = io.pos
    io.flush
    io.rewind
  rescue
    nil
  end

  begin
    block.call(io)
  ensure
    begin
      io.pos = pos
    rescue
      nil
    end
  end
end

.versionObject



8
9
10
# File 'lib/mongoid-grid_fs.rb', line 8

def version
  const_get :Version
end