File size: 1,140 Bytes
f05d877
 
18399c7
 
f05d877
 
 
 
 
 
 
 
d55aaab
 
 
 
 
 
 
f05d877
 
 
 
 
 
 
 
 
 
 
d55aaab
f05d877
 
d55aaab
 
 
 
 
 
 
f05d877
 
 
 
 
 
d55aaab
f05d877
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""High-level image processing entry points (no UI dependencies)."""

from __future__ import annotations

import os
import shutil
import tempfile

from image_resizer.models import ImageItem, ProcessResult
from image_resizer.pipeline import process_and_zip


def process_uploaded_images(
  files,
  fmt: str,
  w: int,
  h: int,
  use_default: bool = False,
) -> ProcessResult:
  """Copy uploaded files to a temp dir and process them."""
  tmp = tempfile.mkdtemp()
  data: list[ImageItem] = []
  for f in files:
    src = str(f)
    dst = os.path.join(tmp, os.path.basename(src))
    shutil.copyfile(src, dst)
    data.append({
      "url": dst,
      "name": os.path.splitext(os.path.basename(dst))[0],
    })
  return process_and_zip(data, fmt, w, h, use_default=use_default)


def process_single_url_image(
  url: str,
  fmt: str,
  w: int,
  h: int,
  use_default: bool = False,
) -> ProcessResult:
  """Process a single image URL."""
  if not url.strip():
    return ProcessResult([], None, "No URL provided", None)
  return process_and_zip(
    [{"url": url.strip(), "name": "single"}],
    fmt, w, h,
    use_default=use_default,
  )