Class: Mongoid::GridFs

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/grid_fs.rb,
lib/mongoid/grid_fs/version.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 = {})
      file = file_model.create
      attributes.to_options!

      file.id = attributes.delete(:id) if attributes.key?(:id)

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

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

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

      if attributes.key?(:meta_data)
        attributes[:metadata] = attributes.delete(:meta_data)
      end

      if attributes.key?(:aliases)
        attributes[:aliases] = Array(attributes.delete(:aliases)).flatten.compact.map(&:to_s)
      end

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

      GridFs.reading(readable) do |io|
        unless attributes.key?(:filename)
          attributes[:filename] =
            [file.id.to_s, GridFs.extract_basename(io)].join('/').squeeze('/')
        end

        unless attributes.key?(:contentType)
          attributes[:contentType] =
            GridFs.extract_content_type(attributes[:filename]) || file.contentType
        end

        GridFs.chunking(io, chunkSize) do |buf|
          md5 << buf
          length += buf.size
          chunk = file.chunks.create(data: binary_for(buf), n: n)
          n += 1
        end
      end

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

      file.update_attributes(attributes)

      file
    rescue
      file.destroy
      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)
        .order_by(uploadDate: :desc)
        .limit(1)
        .first
    end

    def []=(filename, readable)
      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')
      raise NotImplementedError
    end
  end
end
MIME_TYPES =
{
  'md' => 'text/x-markdown; charset=UTF-8'
}.freeze
Helper =
Mongoid::GridFsHelper
VERSION =
'2.4.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.chunk_modelObject

Returns the value of attribute chunk_model.



11
12
13
# File 'lib/mongoid/grid_fs.rb', line 11

def chunk_model
  @chunk_model
end

.file_modelObject

Returns the value of attribute file_model.



10
11
12
# File 'lib/mongoid/grid_fs.rb', line 10

def file_model
  @file_model
end

.namespaceObject

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

Class Method Details

.build_chunk_model_for(namespace) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/mongoid/grid_fs.rb', line 407

def self.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

    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



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
# File 'lib/mongoid/grid_fs.rb', line 237

def self.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

    store_in collection: "#{prefix}.files"

    self.defaults = Defaults.new

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

    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(''))

    field(:filename, type: String)
    field(:contentType, type: String, default: defaults.contentType)
    field(:aliases, type: Array)
    begin
        field(:metadata)
      rescue
        nil
      end

    required = %w(length chunkSize uploadDate md5)

    required.each do |f|
      validates_presence_of(f)
    end

    index(filename: 1)
    index(aliases: 1)
    index(uploadDate: 1)
    index(md5: 1)

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

    def path
      filename
    end

    def basename
      ::File.basename(filename) if filename
    end

    def attachment_filename(*paths)
      return basename if basename

      if paths.empty?
        paths.push('attachment')
        paths.push(id.to_s)
        paths.push(updateDate.iso8601)
      end

      path = paths.join('--')
      base = ::File.basename(path).split('.', 2).first
      ext = GridFs.extract_extension(contentType)

      "#{base}.#{ext}"
    end

    def prefix
      self.class.namespace.prefix
    end

    def each
      fetched = 0
      limit = 7

      while fetched < chunks.size
        chunks.where(:n.lt => fetched + limit, :n.gte => fetched)
              .order_by([:n, :asc]).each do |chunk|
          yield(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 Integer
        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| yield(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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mongoid/grid_fs.rb', line 54

def self.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



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/mongoid/grid_fs.rb', line 458

def self.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



552
553
554
555
# File 'lib/mongoid/grid_fs.rb', line 552

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

.extract_basename(object) ⇒ Object



499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/mongoid/grid_fs.rb', line 499

def self.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



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/mongoid/grid_fs.rb', line 520

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

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

  default =
    if options[:default] == false
      nil
    elsif 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

.extract_extension(content_type) ⇒ Object



546
547
548
549
550
# File 'lib/mongoid/grid_fs.rb', line 546

def self.extract_extension(content_type)
  list = MIME::Types[content_type.to_s]
  type = list.first
  type.extensions.first if type
end

.init!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongoid/grid_fs.rb', line 13

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

.mime_typesObject



516
517
518
# File 'lib/mongoid/grid_fs.rb', line 516

def self.mime_types
  MIME_TYPES
end

.namespace_for(prefix) ⇒ Object



45
46
47
48
49
50
# File 'lib/mongoid/grid_fs.rb', line 45

def self.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



446
447
448
449
450
451
452
453
454
455
456
# File 'lib/mongoid/grid_fs.rb', line 446

def self.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



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/mongoid/grid_fs.rb', line 479

def self.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