Skip to content
CliprWiki

Import pipeline

The path from an uploaded file or a mail attachment to the finished ticket.

Upload and mail fetching use the same pipeline: services/pipeline.py, entry point ingest().

flowchart TB
  IN["File<br/>(upload or mail attachment)"] --> CHK["Check<br/>size, file type, hash"]
  CHK -- "already imported" --> DUP["duplicate"]
  CHK --> SB
  subgraph SB["Sandbox subprocess (analyse)"]
    EX["Extract text<br/>PDF text or OCR"] --> CL["Classify<br/>points"]
    CL -- "no ticket" --> SK["skipped"]
    CL --> PA["Parsers<br/>rail, event, pass, BahnCard"]
    PA --> LY["Layout analysis<br/>(events)"]
    LY --> SP["Split<br/>per page or per code"]
    SP --> CO["Cut out codes"]
  end
  SB --> CM["Store (commit)<br/>duplicates, files, tickets"]
  CM --> ST["Stack"]

1. Checking (main process)

  • Size at most CLIPR_MAX_UPLOAD_MB.
  • File type from the bytes, not the name: PDF, PNG, JPEG or WebP.
  • SHA-256 of the file: if the account already has exactly this file, the import ends with duplicate.
  • At most two analyses at the same time (PDF analysis and OCR need a lot of memory).

2. Analysis in the sandbox

PDF and image parsers are the largest attack surface – uploads and mail attachments can come from any sender. This is why analyse() runs in a short-lived subprocess (services/sandbox.py):

  • Clean environment: no secrets, no access to database keys.
  • Limits: memory (CLIPR_SANDBOX_MEM_MB, default 768), CPU time, file size, open files; hard time limit (CLIPR_SANDBOX_TIMEOUT, default 180 s).
  • Landlock (Linux ≥ 5.13): read only program and library paths, write only /tmp. /data, /proc, /run and TCP connections are blocked.
  • Result as JSON only (bytes Base64-encoded, no pickle). The main process validates the result like any input (parsed_from).
  • The main process marks itself as non-dumpable so that an analysis process of the same UID cannot read CLIPR_SECRET_KEY from /proc even without Landlock.

CLIPR_SANDBOX=0 switches the subprocess off for development. In operation it stays on.

Extraction (extract.py, layout.py)

  • PDF with text: text with position data. Light grey security patterns and watermarks are filtered out.
  • Images and scans: OCR with Tesseract. Preprocessing (contrast, noise) and orientation detection at 0/90/180/270°. Languages: German, English, Dutch; with matching text a second pass in French, Italian, Spanish or Polish (CLIPR_OCR_LANG).
  • Text drawn as vector paths (some providers) is rendered and read via OCR.
  • PDFs with more than CLIPR_MAX_PDF_PAGES pages are rejected.

Classification (classify.py)

Point-based and traceable – the reasoning ends up in the import result (reasons):

  • Negative: terms and conditions, cancellation policy, privacy notices, conditions of carriage, invoices, advertising – in the supported languages.
  • Positive: features of train tickets, event tickets and passes, the file name and embedded images.
  • The result is a type (rail, event, other) or “no ticket” (skipped). With force_kind (button Import as event/transport) classification is skipped.

Parsers (parsers/)

Parser For
rail_db.py DB online ticket: journeys, legs, platforms, train binding, fare, class, BahnCard, City-Ticket, price, order
transit_pass.py Passes: Deutschland-Ticket, monthly passes of transport associations
bahncard.py BahnCard with level, class and term
event.py Event tickets: provider profiles (PROVIDERS), field-name synonyms (LABEL) in several languages
event_layout.py Layout analysis: font size, bold, columns, label-value pairs (beside, below, table header), address blocks, text around each code

New event providers: add a profile to event.PROVIDERS, field names to event.LABEL, and a test case with a recreated ticket in app/tests/.

For train tickets Clipr additionally reads the Aztec code according to UIC 918.3 if zxing-cpp is installed. The code's signature is not verified.

Splitting

  • Per page: if every page is a separate admission ticket, the PDF is split into single PDFs.
  • Per code: if a document contains several codes of the same size (say two tickets on one page), one ticket is created per code; seat details come from the text around that code. The same code several times on a page counts as one ticket.

Codes (codes.py, barcode.py)

QR, Aztec and barcodes are found as embedded images or vector graphics – also as individual bars, in scanned pages and rotated. They are cut out of the original at 300 dpi; rotated barcodes are straightened. For image tickets the image itself is the code.

3. Storing (main process)

_commit() works in the account's store:

  1. Duplicates by order number and seat (tickets.find_duplicate).
  2. Store file and code image encrypted (storage.store), check the storage quota.
  3. Create the ticket (tickets.create) with its source (upload, imap, share) and detection data.
  4. Stacking: several tickets from one file form a stack. A single ticket joins the stack of an existing ticket of the same order or forms a new one with it.

The result is an ImportResult with status imported, duplicate, skipped or error, shown by the interface in the import bar and in the mailbox log.

Re-detect

POST /api/tickets/{id}/reprocess runs the analysis again for the stored file. Status, train binding marks and notes are kept.