Class: Cloudinary::Static

Inherits:
Object show all
Defined in:
lib/cloudinary/static.rb

Constant Summary collapse

IGNORE_FILES =
[".svn", "CVS", "RCS", ".git", ".hg"]
DEFAULT_IMAGE_DIRS =
["app/assets/images", "lib/assets/images", "vendor/assets/images", "public/images"]
DEFAULT_IMAGE_EXTENSION_MASK =
'gif|jpe?g|png|bmp|ico|webp|wdp|jxr|jp2|svg|pdf'
METADATA_FILE =
".cloudinary.static"
METADATA_TRASH_FILE =
".cloudinary.static.trash"

Class Method Summary collapse

Class Method Details

.public_id_and_resource_type_from_path(path) ⇒ Object

## Cloudinary::Utils support ###



79
80
81
82
83
84
85
86
87
88
# File 'lib/cloudinary/static.rb', line 79

def public_id_and_resource_type_from_path(path)
  @metadata ||= 
  path = path.sub(/^\//, '')
  prefix = public_prefixes.find {|prefix| @metadata[File.join(prefix, path)]}
  if prefix
    [@metadata[File.join(prefix, path)]['public_id'], resource_type(path)]
  else
    nil
  end
end

.sync(options = {}) ⇒ Object



12
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloudinary/static.rb', line 12

def sync(options={})
  options = options.clone
  delete_missing = options.delete(:delete_missing)
  found_paths = Set.new
  found_public_paths = {}
  found_public_ids = Set.new
   = 
   = []
  counts = { :not_changed => 0, :uploaded => 0, :deleted => 0, :not_found => 0}
  discover_all do |path, public_path|
    next if found_paths.include?(path)
    if found_public_paths[public_path]
      print "Warning: duplicate #{public_path} in #{path} - already taken from #{found_public_paths[public_path]}\n"
      next
    end
    found_paths << path
    found_public_paths[public_path] = path
    data = root.join(path).read(:mode=>"rb")
    ext = path.extname
    format = ext[1..-1]
    md5 = Digest::MD5.hexdigest(data)
    public_id = "#{public_path.basename(ext)}-#{md5}"
    found_public_ids << public_id
     = .delete(public_path.to_s)
    if  && ["public_id"] == public_id # Signature match
      counts[:not_changed] += 1
      print "#{public_path} - #{public_id} - Not changed\n"
      result = 
    else
      counts[:uploaded] += 1
      print "#{public_path} - #{public_id} - Uploading\n"
      result = Cloudinary::Uploader.upload(Cloudinary::Blob.new(data, :original_filename=>path.to_s),
        options.merge(:format=>format, :public_id=>public_id, :type=>:asset, :resource_type=>resource_type(path.to_s))
      ).merge("upload_time"=>Time.now)
    end
     << [public_path, public_id, result["upload_time"].to_i, result["version"], result["width"], result["height"]].join("\t")+"\n"
  end
  File.open(, "w"){|f| f.print(.join)}
  .to_a.each do |path, info|
    counts[:not_found] += 1
    print "#{path} - #{info["public_id"]} - Not found\n"      
  end
  # Files no longer needed 
  trash = .to_a + (, false).reject{|public_path, info| found_public_ids.include?(info["public_id"])}
  
  if delete_missing
    trash.each do
      |path, info|
      counts[:deleted] += 1
      print "#{path} - #{info["public_id"]} - Deleting\n"
      Cloudinary::Uploader.destroy(info["public_id"], options.merge(:type=>:asset))
    end
    FileUtils.rm_f()
  else
    # Add current removed file to the trash file.
     = trash.map do
      |public_path, info|
      [public_path, info["public_id"], info["upload_time"].to_i, info["version"], info["width"], info["height"]].join("\t")+"\n"
    end
    File.open(, "w"){|f| f.print(.join)}
  end
  
  print "\nCompleted syncing static resources to Cloudinary\n"
  print counts.sort.reject{|k,v| v == 0}.map{|k,v| "#{v} #{k.to_s.gsub('_', ' ').capitalize}"}.join(", ") + "\n"
end