#!/usr/bin/env python3 import hashlib import io from PIL import Image from client import Client, arguments, check, run from stack import SAMPLE, upload, wait_status EXPECTED = {'thumbnail': ('image/jpeg', 'JPEG'), 'crop': ('image/jpeg', 'JPEG'), 'optimized': ('image/webp', 'WEBP')} def encode(img, fmt, **kw): buf = io.BytesIO() img.save(buf, fmt, **kw) return buf.getvalue() def halves(w, h): img = Image.new('RGB', (w, h), (0, 0, 255)) img.paste((255, 0, 0), (0, 0, w // 2, h)) return img def process(c, alice, bob, chat, data, content_type='image/png'): att = upload(c, alice, chat, data, content_type)['json'] ready = wait_status(c, bob, att['id'], ('ready', 'failed'), 60) check(ready['status'] == 'ready' and ready['error_code'] is None, 'attachment failed: %s' % ready['error_code']) check(sorted(v['name'] for v in ready['variants']) == sorted(EXPECTED), 'unexpected variant set') original = c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=bob)['bytes'] check(original == data and ready['sha256'] == hashlib.sha256(data).hexdigest(), 'original changed') out = {} for v in ready['variants']: r = c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=' + v['name'], actor=bob) body = r['bytes'] check(r['headers'].get_content_type() == v['content_type'] == EXPECTED[v['name']][0], v['name'] + ' content type') check(len(body) == v['size_bytes'] and hashlib.sha256(body).hexdigest() == v['sha256'], v['name'] + ' checksum') img = Image.open(io.BytesIO(body)) check(img.format == EXPECTED[v['name']][1] and img.size == (v['width'], v['height']), v['name'] + ' real size/format') check('exif' not in img.info, v['name'] + ' keeps EXIF') img.load() out[v['name']] = img.convert('RGB') return att, out def main(): args = arguments('Thumbnail, crop and optimized variant rules') c = Client(args.base_url, args.ca_file, timeout=30) alice, bob, eve, chat = c.bootstrap() att, v = process(c, alice, bob, chat, SAMPLE) check(v['thumbnail'].size == (256, 160) and v['crop'].size == (128, 128) and v['optimized'].size == (320, 200), 'fixture sizes') c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=bogus', 400, actor=bob) c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=thumbnail', 404, actor=eve) _, v = process(c, alice, bob, chat, encode(halves(2000, 1000), 'PNG')) check(v['thumbnail'].size == (256, 128) and v['optimized'].size == (1280, 640) and v['crop'].size == (128, 128), 'large image sizes') _, v = process(c, alice, bob, chat, encode(halves(100, 50), 'PNG')) check(v['thumbnail'].size == (100, 50) and v['optimized'].size == (100, 50), 'small image must not be upscaled') check(v['crop'].size == (128, 128), 'crop must be 128x128') _, v = process(c, alice, bob, chat, encode(Image.new('RGBA', (300, 300), (0, 0, 0, 0)), 'PNG')) for name, img in v.items(): check(min(img.getpixel((img.width // 2, img.height // 2))) >= 250, name + ': alpha not composited on white') exif = Image.Exif() exif[0x0112] = 6 _, v = process(c, alice, bob, chat, encode(halves(300, 200), 'JPEG', quality=95, exif=exif.tobytes()), 'image/jpeg') t = v['thumbnail'] check(t.size == (170, 256) and v['optimized'].size == (200, 300), 'EXIF orientation not applied to dimensions') top, bottom = t.getpixel((85, 20)), t.getpixel((85, 236)) check(top[0] > 200 and top[2] < 60 and bottom[2] > 200 and bottom[0] < 60, 'EXIF rotation direction wrong') print('PASS: thumbnail/crop/optimized sizes, formats, checksums, no upscale, alpha on white, EXIF orientation, no EXIF in variants') if __name__ == '__main__': run(main)