File size: 820 Bytes
2274b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""Bind resumable item IDs to image content, product and batch within a repository."""

import hashlib
import json
import re


def fingerprint(content, category, batch):
    metadata = json.dumps([category, batch.strip()], separators=(",", ":")).encode()
    return hashlib.sha256(metadata + b"\0" + content).hexdigest()


def existing_request(repo, request_id, content, category, batch):
    if not request_id:
        return None
    if not re.fullmatch("[a-f0-9]{32}", request_id):
        raise ValueError("Invalid inspection request ID. Start a new batch.")
    record = repo.get(request_id)
    if record and record.get("request_fingerprint") != fingerprint(content, category, batch):
        raise ValueError("This request ID belongs to another upload. Start a new batch.")
    return record