Class: ImageBotRepl

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
src/ruby/image-bot-repl.rb

Class Method Summary collapse

Class Method Details

.perform_updateObject



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
77
78
79
# File 'src/ruby/image-bot-repl.rb', line 17

def self.perform_update()
    STDERR.puts ">>> Refreshing uploaded images!"
    file_count = 0
    start_time = Time.now
    # convert uploaded images
    paths = Dir['/raw/uploads/images/*'].sort
    paths.each do |path|
        tag = File.basename(path).split('.').first
        last_jpg_path = path
        (GEN_IMAGE_WIDTHS.reverse + [:p]).each do |width|
            jpg_path = File.join("/gen/i/#{tag}-#{width}.jpg")
            unless File.exist?(jpg_path)
                STDERR.puts jpg_path
                if width == :p
                    system("convert -auto-orient -set colorspace RGB  \"#{last_jpg_path}\" -blur 0x8 -quality 85 -sampling-factor 4:2:0 -strip \"#{jpg_path}\"")
                else
                    system("convert -auto-orient -set colorspace RGB  \"#{last_jpg_path}\" -resize #{width}x\\> -quality 85 -sampling-factor 4:2:0 -strip \"#{jpg_path}\"")
                end
                file_count += 1
            end
            webp_path = File.join("/gen/i/#{tag}-#{width}.webp")
            unless File.exist?(webp_path)
                STDERR.puts webp_path
                system("cwebp -quiet \"#{jpg_path}\" -q #{webp_path.include?('-b') ? 100 : 85} -o \"#{webp_path}\"")
                file_count += 1
            end
            last_jpg_path = jpg_path
        end
    end
    end_time = Time.now
    STDERR.puts sprintf("<<< Finished refreshing uploaded images in %1.2f seconds, wrote #{file_count} files.", (end_time - start_time).to_f)
    STDERR.puts '-' * 59

    STDERR.puts ">>> Refreshing background images!"
    file_count = 0
    start_time = Time.now
    # convert background images
    paths = Dir['/gen/bg/*.svg'].sort
    paths.each do |svg_path|
        tag = File.basename(svg_path).split('.').first
        png_path = "/gen/bg/#{tag}.png"
        jpg_path = "/gen/bg/#{tag}.jpg"
        jpg_512_path = "/gen/bg/#{tag}-512.jpg"
        unless File.exist?(png_path)
            STDERR.puts "Creating #{png_path}..."
            system("inkscape --export-filename=#{png_path} #{svg_path}")
            file_count += 1
        end
        unless File.exist?(jpg_path)
            STDERR.puts "Creating #{jpg_path}..."
            system("convert #{png_path} #{jpg_path}")
            file_count += 1
        end
        unless File.exist?(jpg_512_path)
            STDERR.puts "Creating #{jpg_512_path}..."
            system("convert #{jpg_path} -resize 512x #{jpg_512_path}")
            file_count += 1
        end
    end
    end_time = Time.now
    STDERR.puts sprintf("<<< Finished refreshing background images in %1.2f seconds, wrote #{file_count} files.", (end_time - start_time).to_f)
    STDERR.puts '-' * 59
end