Class: LaserBlob::Blob

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BlobHelpers
Defined in:
app/models/laserblob/blob.rb

Direct Known Subclasses

Image, PDF, Video

Defined Under Namespace

Classes: Image, PDF, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BlobHelpers

#file_content_type, #file_sha1, filename_from_file

Instance Attribute Details

#dataObject

Returns the value of attribute data.



7
8
9
# File 'app/models/laserblob/blob.rb', line 7

def data
  @data
end

Class Method Details

.new(attrs = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/laserblob/blob.rb', line 19

def self.new(attrs={})
  if attrs[:url] && attrs[:url] =~ URI::regexp(%w(http https))
    attrs[:file] = download_url(attrs.delete(:url))
  end

  if attrs[:base64]
    attrs[:file] = base64_file(attrs.delete(:base64), attrs)
  end

  if attrs[:data]
    attrs[:file] = data_file(attrs.delete(:data), attrs)
  end

  if attrs[:file]
    klass = attrs[:content_type] ? content_type_class(attrs[:content_type]) : file_class(attrs[:file])
    sha1 = file_sha1(attrs[:file])
    klass.find_by_sha1(sha1) || (klass == self ? super(attrs) : klass.new(attrs))
  else
    super(attrs)
  end
end

Instance Method Details

#extensionObject



77
78
79
# File 'app/models/laserblob/blob.rb', line 77

def extension
  "." + MiniMime.lookup_by_content_type(content_type).extension
end

#fileObject



62
63
64
# File 'app/models/laserblob/blob.rb', line 62

def file
  @queued
end

#file=(file) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/laserblob/blob.rb', line 81

def file=(file)
  raise 'Cannot set file on persisted blob or blob with file already set' if persisted? || @file
  return if file.nil?

  if self.content_type.nil? || self.content_type == 'application/octet-stream'
    self.content_type = file_content_type(file)
  end
  self.size = file.size
  self.sha1 = file_sha1(file)

  @queued = Tempfile.new(binmode: true)
  FileUtils.cp(file.path, @queued.path)
end

#open(basename: nil, &block) ⇒ Object



95
96
97
98
# File 'app/models/laserblob/blob.rb', line 95

def open(basename: nil, &block)
  basename ||= ['', extension]
  self.class.storage.copy_to_tempfile(id, basename: basename, &block)
end

#url(**options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/laserblob/blob.rb', line 45

def url(**options)
  options[:disposition] = options[:disposition] == 'inline' ? 'inline' : 'attachment'
  if options[:filename]
    options[:disposition] += "; filename=\"#{URI.encode_www_form_component(options[:filename].force_encoding(Encoding::UTF_8)).gsub("%2F", "/")}\""
  end

  if persisted?
    if self.class.storage.local?
      self.class.storage.url(id)
    else
      self.class.storage.url(id, **options.slice(:disposition, :expires_in))
    end
  else
    @url
  end
end

#url=(value) ⇒ Object



41
42
43
# File 'app/models/laserblob/blob.rb', line 41

def url=(value)
  @url = value if !persisted?
end