Module: S3Assets::Utility

Extended by:
Utility
Included in:
Utility
Defined in:
lib/s3_assets/utility.rb

Instance Method Summary collapse

Instance Method Details

#create!(filepath, parent: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/s3_assets/utility.rb', line 49

def create!(filepath, parent: nil)
  doc = nil
  File.open(filepath) do |file|
    doc = ::S3Assets::Model.new(parent: parent)
    doc.asset = file
    doc.save!
  end
  return doc
end

#download(url) ⇒ Object



43
44
45
46
47
# File 'lib/s3_assets/utility.rb', line 43

def download(url)
  temp_doc = ::S3Assets::Model.new
  temp_doc.asset.download!(url)
  return temp_doc.asset.file.file
end

#json(doc, type: nil, processing: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/s3_assets/utility.rb', line 4

def json(doc, type: nil, processing: nil)
  file_url = url(doc, type: type, processing: processing)
  return if file_url.blank?

  if doc.present? && doc.processed?
    filepath = doc.asset.path
    content_type = doc.content_type
  end
  {
    id: doc.try(:_id),
    url: file_url,
    filepath: filepath,
    content_type: content_type,
    name: doc.try(:original_filename)
  }
end

#url(doc, type: nil, processing: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/s3_assets/utility.rb', line 21

def url(doc, type: nil, processing: nil)
  return if doc.blank?
  return "https://#{::S3Assets.cloudfront_host}/#{doc.asset.path}" unless ::S3Assets.processing_enabled

  return doc.asset.url if !(doc.processed?)

  raw_url = "https://#{::S3Assets.cloudfront_host}/raw/#{doc.asset.path}"
  return raw_url if !(doc.image?)
  return raw_url if doc.content_type.include?("gif") && !(processing)

  processing ||= {}
  processing[:size] ||= "1500x1500"
  processing[:type] ||= "cover"
  processing_str = "/size:#{processing[:size]}"
  processing_str += "/extend:#{processing[:extend]}" if processing[:extend].present?
  processing_str += "/blur:#{processing[:blur].to_i}" if processing[:blur].present?
  processing_str += "/type:#{processing[:type]}"


  return "https://#{::S3Assets.cloudfront_host}/images#{processing_str}/#{doc.asset.path}"
end