Class: EchoUploads::File

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/echo_uploads/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject

Returns the value of attribute file.



12
13
14
# File 'lib/echo_uploads/file.rb', line 12

def file
  @file
end

Class Method Details

.default_key_procObject

Returns a proc that takes as its only argument an ActionDispatch::UploadedFile and returns a key string.



30
31
32
33
34
35
36
37
38
39
# File 'lib/echo_uploads/file.rb', line 30

def self.default_key_proc
  ->(file) do
    digest = Digest::SHA512.new
    file.rewind
    until file.eof?
      digest.update file.read(1000)
    end
    digest.hexdigest
  end
end

.prune_temporary!Object



108
109
110
111
112
# File 'lib/echo_uploads/file.rb', line 108

def self.prune_temporary!
  where(temporary: true).where(['expires_at < ?', Time.now]).each do |file_meta|
    file_meta.destroy
  end
end

Instance Method Details

#compute_key!(file, options) ⇒ Object



24
25
26
# File 'lib/echo_uploads/file.rb', line 24

def compute_key!(file, options)
  self.key = options[:key].call file
end

#compute_mime!(options) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/echo_uploads/file.rb', line 14

def compute_mime!(options)
  if file and file.is_a?(::EchoUploads::MappedFile)
    name = file.mapped_filename
  else
    name = original_filename
  end
  type = MIME::Types.type_for(name).first
  self.mime_type = type ? type.content_type : 'application/octet-stream'
end

#delete_file_conditionallyObject

Deletes the file on disk if and only if no other instances of EchoUpload::File reference it.



43
44
45
46
47
# File 'lib/echo_uploads/file.rb', line 43

def delete_file_conditionally
  unless self.class.where(key: key).where(['id != ?', id]).exists?
    storage.delete key
  end
end

#original_filenameObject



49
50
51
# File 'lib/echo_uploads/file.rb', line 49

def original_filename
  original_basename + original_extension
end

#pathObject



53
54
55
# File 'lib/echo_uploads/file.rb', line 53

def path
  storage.path key
end

#persist!(attr, options) ⇒ Object

Pass in an attribute name, an ActionDispatch::Http::UploadedFile, and an options hash. Must set #file attribute first.



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/echo_uploads/file.rb', line 59

def persist!(attr, options)
  unless(
    file.is_a?(ActionDispatch::Http::UploadedFile) or
    file.is_a?(Rack::Test::UploadedFile)
  )
    raise(
      "Expected #file to be a ActionDispatch::Http::UploadedFile "+
      "or Rack::Test::UploadedFile, but was #{file.inspect}"
    )
  end
  
  # Configure and save the metadata object.
  compute_key! file, options
  self.owner_attr = attr
  self.original_extension = ::File.extname(file.original_filename)
  self.original_basename = ::File.basename(file.original_filename, original_extension)
  compute_mime! options
  if options[:storage].is_a? String
    self.storage_type = options[:storage]
  else
    self.storage_type = options[:storage].name
  end
  save!

  # Write the file to the filestore. It's possible that #file is an instance of
  # EchoUploads::MappedFile, which is a subclass of
  # ActionDispatch::Http::UploadedFile.
  if file.is_a?(ActionDispatch::Http::UploadedFile)
    storage.write key, file.tempfile
  else
    storage.write key, file
  end
  
  # If we mapped the files, they were temporarily written to tmp/echo_uploads.
  # Delete them.
  if file.is_a?(::EchoUploads::MappedFile)
    ::File.delete file.path
  end

  # Prune any expired temporary files. (Unless automatic pruning was turned off in
  # the app config.)
  unless (
    Rails.configuration.echo_uploads.respond_to?(:prune_tmp_files_on_upload) and
    !Rails.configuration.echo_uploads.prune_tmp_files_on_upload
  )
    self.class.prune_temporary!
  end
end

#readObject



114
115
116
# File 'lib/echo_uploads/file.rb', line 114

def read
  storage.read key
end

#storageObject



118
119
120
# File 'lib/echo_uploads/file.rb', line 118

def storage
  class_from_string(storage_type).new
end