111 lines
3.3 KiB
Markdown
111 lines
3.3 KiB
Markdown
# Subtask: QR Code Generation
|
|
|
|
**Parent Task**: Decomp/001
|
|
**Subtask ID**: Decomp/001-07
|
|
**Dependencies**: Decomp/001-03 (Unique Short Code Generation), Decomp/001-04 (Redirect Endpoint)
|
|
|
|
## Description
|
|
|
|
Generate QR codes for shortened URLs. The QR code should encode the full short URL and be returned as an image file (PNG format). Supports both on-demand generation and pre-generation.
|
|
|
|
## Requirements
|
|
|
|
- Generate QR code from short URL (e.g., `https://sh.rt/abc123`)
|
|
- Return QR code as PNG image
|
|
- Configurable size (default: 300x300px)
|
|
- Configurable error correction level (L/M/Q/H)
|
|
- Support QR code caching (store generated images)
|
|
- Serve QR codes via dedicated endpoint (`/qr/{short_code}`)
|
|
- Optional: SVG format for scalability
|
|
|
|
## Technical Specifications
|
|
|
|
**QR Code library**: Use `qrcode` Python package (no external API calls)
|
|
|
|
**API endpoint**:
|
|
```
|
|
GET /qr/{short_code}
|
|
GET /qr/{short_code}?size=400&format=png&ecc=M
|
|
```
|
|
|
|
**QR code generation**:
|
|
```python
|
|
import qrcode
|
|
from io import BytesIO
|
|
|
|
def generate_qr_code(url: str, size: int = 300, ecc: str = 'M') -> bytes:
|
|
"""
|
|
Generate QR code PNG for given URL.
|
|
|
|
Args:
|
|
url: Full short URL to encode
|
|
size: Image size in pixels (width=height)
|
|
ecc: Error correction level (L=7%, M=15%, Q=25%, H=30%)
|
|
|
|
Returns:
|
|
PNG image bytes
|
|
"""
|
|
qr = qrcode.QRCode(
|
|
version=None, # Auto-detect
|
|
error_correction=getattr(qrcode.constants, f'ERROR_CORRECT_{ecc}'),
|
|
box_size=max(10, size // 25), # Scale based on requested size
|
|
border=4,
|
|
)
|
|
qr.add_data(url)
|
|
qr.make(fit=True)
|
|
|
|
img = qr.make_image(fill_color="black", back_color="white")
|
|
|
|
# Resize to exact dimensions
|
|
img = img.resize((size, size), resample=0) # Nearest neighbor
|
|
|
|
buffer = BytesIO()
|
|
img.save(buffer, format='PNG')
|
|
return buffer.getvalue()
|
|
```
|
|
|
|
**Storage strategy**:
|
|
- Generate QR codes on-demand (no pre-generation)
|
|
- Cache generated QR codes in filesystem or S3
|
|
- Cache key: `qr/{short_code}_{size}_{ecc}.png`
|
|
- TTL: Same as URL TTL (infinite if no expiration)
|
|
|
|
**Response headers**:
|
|
```
|
|
Content-Type: image/png
|
|
Content-Disposition: inline; filename="qr_{short_code}.png"
|
|
Cache-Control: public, max-age=86400 # 24 hours
|
|
```
|
|
|
|
## Acceptance Criteria
|
|
|
|
- [ ] QR code scans to correct short URL
|
|
- [ ] QR code is readable at 300x300px size
|
|
- [ ] Different error correction levels work correctly
|
|
- [ ] QR code endpoint returns valid PNG
|
|
- [ ] Non-existent short_code returns 404
|
|
- [ ] QR code caching reduces generation time on subsequent requests
|
|
- [ ] QR code works with damaged/missing corner (error correction)
|
|
|
|
## Notes
|
|
|
|
**Trade-offs**:
|
|
- On-demand vs pre-generation: On-demand chosen for simplicity
|
|
- PNG vs SVG: PNG default (better compatibility), SVG optional
|
|
- Error correction M (15%): balanced between size and robustness
|
|
|
|
**Edge cases**:
|
|
- Very long URLs: QR code auto-version selection handles this
|
|
- High ECC requests: larger QR code size
|
|
- Concurrent QR generation: cache stampede possible (use lock if needed)
|
|
|
|
**Performance targets**:
|
|
- Generation: <100ms (without cache)
|
|
- Cached retrieval: <10ms
|
|
- Image size: ~2-5KB for 300x300px PNG
|
|
|
|
**Alternatives considered**:
|
|
- External API (Google Charts API): rejected, adds dependency and latency
|
|
- Pre-generation for all URLs: rejected, wasted storage for unused QR codes
|
|
- SVG only: rejected, PNG has better mobile browser support
|