Class: Slices::Asset::Maker

Inherits:
Object
  • Object
show all
Defined in:
lib/slices/asset/maker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Maker

Returns a new instance of Maker.



11
12
13
# File 'lib/slices/asset/maker.rb', line 11

def initialize(args)
  self.args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



5
6
7
# File 'lib/slices/asset/maker.rb', line 5

def args
  @args
end

#new_assetObject

Returns the value of attribute new_asset.



5
6
7
# File 'lib/slices/asset/maker.rb', line 5

def new_asset
  @new_asset
end

Class Method Details

.run(args) ⇒ Object



7
8
9
# File 'lib/slices/asset/maker.rb', line 7

def self.run(args)
  new(args).run
end

Instance Method Details

#create_new_assetObject



15
16
17
# File 'lib/slices/asset/maker.rb', line 15

def create_new_asset
  ::Asset.create!(args)
end

#delete_tempfile_from_s3Object



35
36
37
38
# File 'lib/slices/asset/maker.rb', line 35

def delete_tempfile_from_s3
  directory = new_asset.file.send(:directory)
  directory.files.new(key: s3_path).destroy
end

#find_matching_asset(new_asset) ⇒ Object



19
20
21
22
23
# File 'lib/slices/asset/maker.rb', line 19

def find_matching_asset(new_asset)
  ::Asset.first(conditions: {
    file_fingerprint: new_asset.file_fingerprint, :_id.ne => new_asset.id
  })
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/slices/asset/maker.rb', line 40

def run
  self.new_asset = create_new_asset
  delete_tempfile_from_s3 if tempfile_stored_on_s3?
  matching_asset = find_matching_asset(new_asset)

  if matching_asset.present?
    new_asset.destroy
    matching_asset.soft_restore!
    matching_asset
  else
    new_asset
  end
end

#s3_pathObject



30
31
32
33
# File 'lib/slices/asset/maker.rb', line 30

def s3_path
  s3_path = args[:file].path
  s3_path[s3_path.index(Slices::Config::S3_TEMPFILE_KEY_PREFIX) .. -1]
end

#tempfile_stored_on_s3?Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/slices/asset/maker.rb', line 25

def tempfile_stored_on_s3?
  Slices::Config.s3_storage? &&
    args[:file].is_a?(URI)
end