Class: Cloudinary::Static

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

Constant Summary collapse

IGNORE_FILES =
[".svn", "CVS", "RCS", ".git", ".hg"]
SUPPORTED_IMAGES =
[/\.(gif|jpe?g|png|bmp|ico|webp|wdp|jxr|jp2|svg|pdf)$/i]
STATIC_IMAGE_DIRS =
["app/assets/images", "lib/assets/images", "vendor/assets/images", "public/images"]
METADATA_FILE =
".cloudinary.static"
METADATA_TRASH_FILE =
".cloudinary.static.trash"

Class Method Summary collapse

Class Method Details

.discoverObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cloudinary/static.rb', line 11

def self.discover
  ignore_files = Cloudinary.config.ignore_files || IGNORE_FILES 
  relative_dirs = Cloudinary.config.static_image_dirs || STATIC_IMAGE_DIRS
  dirs = relative_dirs.map{|dir| self.root.join(dir)}.select(&:exist?)
  dirs.each do
    |dir|
    dir.find do
      |path|
      file = path.basename.to_s
      if ignore_files.any?{|pattern| pattern.is_a?(String) ? pattern == file : file.match(pattern)}
        Find.prune
        next
      elsif path.directory?
        next
      elsif SUPPORTED_IMAGES.none?{|pattern| pattern.is_a?(String) ? pattern == file : file.match(pattern)}
        next          
      else
        relative_path = path.relative_path_from(self.root)
        public_path = path.relative_path_from(dir.dirname)
        yield(relative_path, public_path)
      end
    end
  end
end

.metadata(metadata_file = metadata_file_path, hash = true) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cloudinary/static.rb', line 48

def self.( = , hash=true) 
   = []
  if File.exist?()
    IO.foreach() do
      |line|
      line.strip!
      next if line.blank?
      path, public_id, upload_time, version, width, height = line.split("\t")
       << [path, {
        "public_id" => public_id, 
        "upload_time" => Time.at(upload_time.to_i).getutc, 
        "version" => version,
        "width" => width.to_i,
        "height" => height.to_i
      }]
    end
  end
  hash ? Hash[*.flatten] : 
end

.metadata_file_pathObject



40
41
42
# File 'lib/cloudinary/static.rb', line 40

def self.
  self.root.join(METADATA_FILE)
end

.metadata_trash_file_pathObject



44
45
46
# File 'lib/cloudinary/static.rb', line 44

def self.
  self.root.join(METADATA_TRASH_FILE)
end

.rootObject



36
37
38
# File 'lib/cloudinary/static.rb', line 36

def self.root
  Cloudinary.app_root
end

.sync(options = {}) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cloudinary/static.rb', line 68

def self.sync(options={})
  options = options.clone
  delete_missing = options.delete(:delete_missing)
   = self.
  found_paths = Set.new
  found_public_ids = Set.new
   = []
  counts = { :not_changed => 0, :uploaded => 0, :deleted => 0, :not_found => 0}
  self.discover do
    |path, public_path|
    next if found_paths.include?(path)
    found_paths << path
    data = self.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
      $stderr.print "#{public_path} - #{public_id} - Not changed\n"
      result = 
    else
      counts[:uploaded] += 1
      $stderr.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)
      ).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(self., "w"){|f| f.print(.join)}
  .to_a.each do |path, info|
    counts[:not_found] += 1
    $stderr.print "#{path} - #{info["public_id"]} - Not found\n"      
  end
  # Files no longer needed 
  trash = .to_a + self.(, false).reject{|public_path, info| found_public_ids.include?(info["public_id"])} 
  
  if delete_missing
    trash.each do
      |path, info|
      counts[:deleted] += 1
      $stderr.print "#{path} - #{info["public_id"]} - Deleting\n"
      Cloudinary::Uploader.destroy(info["public_id"], options.merge(:type=>:asset))
    end
    FileUtils.rm_f(self.)
  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(self., "w"){|f| f.print(.join)}    
  end
  
  $stderr.print "\nCompleted syncing static resources to Cloudinary\n"
  $stderr.print counts.sort.reject{|k,v| v == 0}.map{|k,v| "#{v} #{k.to_s.gsub('_', ' ').capitalize}"}.join(", ") + "\n"
end