Test

Ruby code posted
created at 01 Feb 12:28, updated at 02 Feb 01:04

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
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
module RScale
  extend RScale::UUID
  
  @@config = nil
  @@formats = {}
  
  # Configure RScale
  def self.configure
    @@config = Configuration.new if @@config.nil?
    yield @@config
  end
  
  # Get current RScale configuration
  def self.config
    @@config
  end
  
  # Add new format
  def self.format(name)
    unless @@formats.key?(name)
      @@formats[name] = Format.new(name)
      yield @@formats[name] if block_given?
    else
      raise FormatError, "Format with name [#{name.to_s}] is already defined"
    end
  end
  
  # Get list of formats
  def self.formats
    @@formats
  end
  
  # Generate thumbnails for the image format
  def self.image_for(format, file)
    if @@formats.key?(format.to_sym)
      fmt = @@formats[format.to_sym]
      file_info = File.get_info(file)
      url = fmt.url
      image = {}
      
      options = file_info.merge(:format => fmt.name)
      
      options[:time] = Time.now.to_i unless url[':time'].nil?
      options[:md5] = Digest::MD5.filedigest(file) unless url[':md5'].nil?
      unless url[':uuid'].nil?
        options[:uuid] = uuid
        options[:uuid_dir] = "#{options[:uuid][0,2]}/#{options[:uuid][2,2]}"
      end
      
      fmt.styles.each_pair do |k,v|
        url = fmt.url.placeholders(options.merge(v))
        file_out = @@config.public + "/#{url}"
        if Processor.process(file, file_out, v)
          image[k] = url
        end
      end
      
      return image.size == fmt.styles.size ? image : nil
    else
      raise ArgumentError, "Format #{format.to_s} cannot be found!"
    end
  end
end
<script type="text/javascript">
alert(1)
</script>
1.66 KB in 4 ms with coderay