Skip to content

main

Main logic for the images-upload-cli package.

upload_images async

upload_images(upload_func: Callable[[AsyncClient, bytes], Awaitable[str]], images: tuple[Path], thumbnail: bool) -> Sequence[tuple[str, str] | tuple[str, None]]

Upload images using the specified upload function and optionally generate thumbnails.

Parameters:

Name Type Description Default
upload_func Callable[[AsyncClient, bytes], Awaitable[str]]

The function used to upload the images.

required
images tuple[Path]

The paths of the images to be uploaded.

required
thumbnail bool

Indicates whether to generate thumbnails for the images.

required

Returns:

Type Description
Sequence[tuple[str, str] | tuple[str, None]]

The links to the uploaded images and their corresponding thumbnails.

Sequence[tuple[str, str] | tuple[str, None]]

The thumbnail link will be None if generation is disabled.

Source code in src/images_upload_cli/main.py
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
50
51
52
async def upload_images(
    upload_func: Callable[[AsyncClient, bytes], Awaitable[str]],
    images: tuple[Path],
    thumbnail: bool,
) -> Sequence[tuple[str, str] | tuple[str, None]]:
    """Upload images using the specified upload function and optionally generate thumbnails.

    Args:
        upload_func: The function used to upload the images.
        images: The paths of the images to be uploaded.
        thumbnail: Indicates whether to generate thumbnails for the images.

    Returns:
        The links to the uploaded images and their corresponding thumbnails.
        The thumbnail link will be `None` if generation is disabled.
    """
    links = []

    if thumbnail:
        font = get_font()

    async with AsyncClient() as client:
        for img_path in images:
            img = img_path.read_bytes()

            img_link = await upload_func(client, img)
            # If the upload fails, skip the current image and proceed with the next one.
            if not img_link:
                continue

            if thumbnail:
                thumb = make_thumbnail(img, font)  # pyright: ignore[reportPossiblyUnboundVariable]
                thumb_link = await upload_func(client, thumb)
                # If the upload fails, skip the current image and proceed with the next one.
                if not thumb_link:
                    continue
            else:
                thumb_link = None

            links.append((img_link, thumb_link))

    return links
format_link(links: Sequence[tuple[str, str] | tuple[str, None]], fmt: str) -> str

Format the image links based on the specified format.

Parameters:

Name Type Description Default
links Sequence[tuple[str, str] | tuple[str, None]]

The image links and their corresponding thumbnails.

required
fmt str

The format to use for formatting the links. Valid options are "plain", "bbcode", "html", and "markdown".

required

Returns:

Type Description
str

The formatted image links as a string.

Source code in src/images_upload_cli/main.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def format_link(links: Sequence[tuple[str, str] | tuple[str, None]], fmt: str) -> str:
    """Format the image links based on the specified format.

    Args:
        links: The image links and their corresponding thumbnails.
        fmt: The format to use for formatting the links. Valid options are "plain", "bbcode", "html", and "markdown".

    Returns:
        The formatted image links as a string.
    """
    if fmt == "plain":
        return " ".join([img_link for img_link, _ in links])

    if fmt == "bbcode":
        return " ".join(
            f"[img]{img_link}[/img]"
            if thumb_link is None
            else f"[url={img_link}][img]{thumb_link}[/img][/url]"
            for img_link, thumb_link in links
        )

    if fmt == "html":
        return " ".join(
            f'<img src="{img_link}" alt="image">'
            if thumb_link is None
            else f'<a href="{img_link}"><img src="{thumb_link}" alt="thumb"></a>'
            for img_link, thumb_link in links
        )

    if fmt == "markdown":
        return " ".join(
            f"![image]({img_link})"
            if thumb_link is None
            else f"[![thumb]({thumb_link})]({img_link})"
            for img_link, thumb_link in links
        )

    return ""