Skip to content

Commit caa79dd

Browse files
authored
Merge pull request #127 from ruby-no-kai/stop-enlarge-event-assets
PushEventAssetFileJob: avoid enlarging small images
2 parents 1a65a56 + 6c3fa5b commit caa79dd

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,t
7474
&& rm -rf /var/lib/apt/lists/*
7575

7676
COPY --from=vipsbuilder /opt/libvips /opt/libvips
77-
RUN ln -s /opt/libvips/bin/vipsthumbnail /usr/local/bin/vipsthumbnail \
77+
RUN for f in /opt/libvips/bin/vips*; do ln -s "$f" /usr/local/bin/; done \
7878
&& echo /opt/libvips/lib > /etc/ld.so.conf.d/libvips.conf && ldconfig \
7979
&& vipsthumbnail --vips-version
8080

app/jobs/push_event_asset_file_job.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ def convert_to_webp(asset_file)
4848
asset_file.get_object(response_target: f)
4949
end
5050

51+
thumbnail_size = thumbnail_size_for(input_path)
52+
5153
_stdout, stderr, status = Open3.capture3(
5254
{ 'VIPS_BLOCK_UNTRUSTED' => '1' },
5355
'vipsthumbnail', input_path.to_s,
54-
'-s', '800',
56+
'-s', thumbnail_size,
5557
'-o', output_path.to_s + '[Q=72,preset=drawing,smart_subsample=true,effort=6,strip=true]'
5658
)
5759
raise "vipsthumbnail failed (status=#{status.exitstatus}): #{stderr}" unless status.success?
@@ -61,6 +63,23 @@ def convert_to_webp(asset_file)
6163
content
6264
end
6365

66+
THUMBNAIL_MAX_SIZE = 800
67+
68+
# Avoid enlarging images smaller than THUMBNAIL_MAX_SIZE
69+
def thumbnail_size_for(input_path)
70+
stdout, _, status = Open3.capture3(
71+
{ 'VIPS_BLOCK_UNTRUSTED' => '1' },
72+
'vipsheader', '-f', 'width', input_path.to_s
73+
)
74+
if status.success?
75+
input_width = stdout.strip.to_i
76+
if input_width > 0 && input_width < THUMBNAIL_MAX_SIZE
77+
return input_width.to_s
78+
end
79+
end
80+
THUMBNAIL_MAX_SIZE.to_s
81+
end
82+
6483
def push_to_github(gi, repo, asset_file, webp_content)
6584
octokit = gi.octokit
6685
images_path = @conference.github_repo_images_path.chomp('/')

spec/jobs/push_event_asset_file_job_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@
118118
expect(Dir.exist?(tmpdir)).to be false
119119
end
120120

121+
it 'does not enlarge images smaller than 800px' do
122+
allow(Open3).to receive(:capture3).and_call_original
123+
124+
vipsthumbnail_calls = []
125+
allow(Open3).to receive(:capture3).and_wrap_original do |original, *args, **kwargs|
126+
env, cmd, *rest = args
127+
vipsthumbnail_calls << rest if cmd == 'vipsthumbnail'
128+
original.call(*args, **kwargs)
129+
end
130+
131+
described_class.perform_now(editing_history)
132+
133+
# test_image.png is 8x8 — size should be capped at input width
134+
expect(vipsthumbnail_calls.length).to eq(1)
135+
size_index = vipsthumbnail_calls.first.index('-s')
136+
expect(vipsthumbnail_calls.first[size_index + 1]).to eq('8')
137+
end
138+
121139
it 'raises on vipsthumbnail failure' do
122140
allow(Open3).to receive(:capture3).and_return(['', 'error message', double(success?: false, exitstatus: 1)])
123141
expect { described_class.perform_now(editing_history) }.to raise_error(/vipsthumbnail failed/)

0 commit comments

Comments
 (0)