Skip to content

Medias

add_media_file_for_lang(client, media, file_data_or_path, filename, mimetype, lang='en', croppedContent=False) ¤

Add a file to an existing LumApps media.

Parameters:

Name Type Description Default
client BaseClient

The BaseClient used to make httpx to the LumApps Api.

required
media Dict[str, Any]

The LumApps media on which the files have to be uploaded.

required
file_data_or_path str

The filepath (str) or the data (bytes) to upload.

required
filename str

The name of the file to upload. Once uploaded the file will appear with that name.

required
mimetype str

The mimeType fo the file to upload.

required
lang Optional[str]

The lang of the file to upload (default: "en").

'en'
croppedContent Optional[bool]

Wether to add the file to the croppedContent instead or content (default: False)

False

Returns:

Type Description
Optional[Dict[str, Any]]

The updated media if succesfull, otherwise None.

Source code in lumapps/api/helpers/medias.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def add_media_file_for_lang(
    client: BaseClient,
    media: Dict[str, Any],
    file_data_or_path: str,
    filename: str,
    mimetype: str,
    lang: Optional[str] = "en",
    croppedContent: Optional[bool] = False,
) -> Optional[Dict[str, Any]]:
    """ Add a file to an existing LumApps media.

        Args:
            client: The BaseClient used to make httpx to the LumApps Api.
            media: The LumApps media on which the files have to be uploaded.
            file_data_or_path (Union[bytes, str]): The filepath (str) or the data (bytes) to upload.
            filename: The name of the file to upload. Once uploaded the file will appear with that name.
            mimetype: The mimeType fo the file to upload.
            lang: The lang of the file to upload (default: "en").
            croppedContent (bool): Wether to add the file to the croppedContent instead or content (default: False)

        Returns:
            The updated media if succesfull, otherwise None.
    """  # noqa

    # upload the file
    uploaded_file = _upload_new_media_file_of_given_lang(
        client=client,
        file_data_or_path=file_data_or_path,
        filename=filename,
        mimetype=mimetype,
        lang=lang,
    )
    if not uploaded_file:
        return media

    # update the media
    where = "croppedContent" if croppedContent else "content"
    media[where].append(uploaded_file)
    saved: Any = client.get_call("document/update", body=media)
    return saved

create_new_media(client, file_data_or_path, doc_path, filename, mimetype, is_shared, lang='en') ¤

Upload a file and create a new media in LumApps media library.

Parameters:

Name Type Description Default
client BaseClient

The BaseClient used to make httpx to the LumApps Api.

required
file_data_or_path Union[bytes, str]

The filepath (str) or the data (bytes) to upload.

required
doc_path str

The doc path of the media to upload, this will decide where the media will go in your media library (eg: provider=<my_provider>/site=<my_site_id>/resource=<my_parent_folder_id>)

required
filename str

The name of the file to upload. Once uploaded the file will appear with that name.

required
mimetype str

The mimeType fo the file to upload.

required
is_shared bool

Wether the file is shared or not. Non shared files will only be visible by you.

required
lang Optional[str]

The lang of the file to upload (default: "en").

'en'

Exceptions:

Type Description
Exception

The data or file path type provided is not supported.

Returns:

Type Description
Optional[Dict[str, Any]]

Return the uploaded media if successfull, None otherwise.

Source code in lumapps/api/helpers/medias.py
 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def create_new_media(
    client: BaseClient,
    file_data_or_path: Union[bytes, str],
    doc_path: str,
    filename: str,
    mimetype: str,
    is_shared: bool,
    lang: Optional[str] = "en",
) -> Optional[Dict[str, Any]]:
    """ Upload a file and create a new media in LumApps media library.

        Args:
            client: The BaseClient used to make httpx to the LumApps Api.
            file_data_or_path: The filepath (str) or the data (bytes) to upload.
            doc_path: The doc path of the media to upload, this will decide where the media will go in your media library
                            (eg: provider=`<`my_provider`>`/site=`<`my_site_id`>`/resource=`<`my_parent_folder_id`>`)
            filename: The name of the file to upload. Once uploaded the file will appear with that name.
            mimetype: The mimeType fo the file to upload.
            is_shared: Wether the file is shared or not. Non shared files will only be visible by you.
            lang: The lang of the file to upload (default: "en").

        Raises:
            Exception: The data or file path type provided is not supported.

        Returns:
            Return the uploaded media if successfull, None otherwise.
    """  # noqa

    if isinstance(file_data_or_path, str):
        file_data = open(file_data_or_path, "rb")
    elif isinstance(file_data_or_path, bytes):
        file_data = file_data_or_path  # type: ignore
    else:
        raise BaseClientError(
            "File data or path type not supported: {}".format(type(file_data_or_path))
        )

    # Get upload url for the document
    body = {
        "fileName": filename,
        "lang": lang,
        "parentPath": doc_path,
        "shared": is_shared,
        "success": "/upload",
    }
    upload_infos: Any = client.get_call("document/uploadUrl/get", body=body)
    upload_url = upload_infos["uploadUrl"]

    # Upload
    files_tuple_list: Any = [("files", (filename, file_data, mimetype))]
    response = httpx.post(
        upload_url,
        headers={"Authorization": "Bearer " + client.token},
        files=files_tuple_list,  # type: ignore
    )
    doc = response.json().get("items")

    if doc:
        return doc[0]
    return None