U
    nËjµ  ã                   @   sæ   d dl mZmZmZmZmZmZmZmZm	Z	 d dl
mZ d dlmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ G dd„ deƒZG dd„ dƒZedƒZeedœdd„ZdS )é    )	ÚAnyÚBinaryIOÚCallableÚDictÚIterableÚOptionalÚTypeÚTypeVarÚcast)ÚDoc)Ú
CoreSchemaÚGetJsonSchemaHandlerÚJsonSchemaValue)ÚURL)ÚAddress)ÚFormData)ÚHeaders)ÚQueryParams)ÚState)Ú
UploadFile)Ú	Annotatedc                       s¤  e Zd ZU dZeeedƒf ed< eee	 edƒf ed< eee
 edƒf ed< eeedƒf ed	< eee	 ed
ƒf ed< eeedƒf ddœ‡ fdd„Zd0ee
edƒf edœ‡ fdd„Zee
edƒf ddœ‡ fdd„Zddœ‡ fdd„Zeed  eedef  dœdd „ƒZeed  eed!œd"d#„ƒZeeed d$œd%d&„ƒZeee	ef dd'œd(d)„ƒZeeeed*œd+d,„ƒZeee eegef ed-œd.d/„ƒZ‡  Z S )1r   aA  
    A file uploaded in a request.

    Define it as a *path operation function* (or dependency) parameter.

    If you are using a regular `def` function, you can use the `upload_file.file`
    attribute to access the raw standard Python file (blocking, not async), useful and
    needed for non-async code.

    Read more about it in the
    [FastAPI docs for Request Files](https://fastapi.tiangolo.com/tutorial/request-files/).

    ## Example

    ```python
    from typing import Annotated

    from fastapi import FastAPI, File, UploadFile

    app = FastAPI()


    @app.post("/files/")
    async def create_file(file: Annotated[bytes, File()]):
        return {"file_size": len(file)}


    @app.post("/uploadfile/")
    async def create_upload_file(file: UploadFile):
        return {"filename": file.filename}
    ```
    z,The standard Python file object (non-async).ÚfilezThe original file name.ÚfilenamezThe size of the file in bytes.ÚsizezThe headers of the request.Úheadersz2The content type of the request, from the headers.Úcontent_typezA
                The bytes to write to the file.
                N)ÚdataÚreturnc                 ƒ   s   t ƒ  |¡I dH S )zÆ
        Write some bytes to the file.

        You normally wouldn't use this from a file you read in a request.

        To be awaitable, compatible with async, this is run in threadpool.
        N)ÚsuperÚwrite)Úselfr   ©Ú	__class__© ú:/tmp/pip-unpacked-wheel-yktl02hi/fastapi/datastructures.pyr   J   s    zUploadFile.writeéÿÿÿÿzL
                The number of bytes to read from the file.
                )r   r   c                 ƒ   s   t ƒ  |¡I dH S )z|
        Read some bytes from the file.

        To be awaitable, compatible with async, this is run in threadpool.
        N)r   Úread)r    r   r!   r#   r$   r&   ^   s    zUploadFile.readzO
                The position in bytes to seek to in the file.
                )Úoffsetr   c                 ƒ   s   t ƒ  |¡I dH S )z¾
        Move to a position in the file.

        Any next read or write will be done from that position.

        To be awaitable, compatible with async, this is run in threadpool.
        N)r   Úseek)r    r'   r!   r#   r$   r(   p   s    zUploadFile.seek©r   c                 ƒ   s   t ƒ  ¡ I dH S )zm
        Close the file.

        To be awaitable, compatible with async, this is run in threadpool.
        N)r   Úclose©r    r!   r#   r$   r*   „   s    zUploadFile.close.)Úclsr   c                 c   s   | j V  d S ©N)Úvalidate)r,   r#   r#   r$   Ú__get_validators__Œ   s    zUploadFile.__get_validators__)r,   Úvr   c                 C   s    t |tƒstdt|ƒ› �ƒ‚|S ©NzExpected UploadFile, received: )Ú
isinstanceÚStarletteUploadFileÚ
ValueErrorÚtype)r,   r0   r#   r#   r$   r.   �   s    
zUploadFile.validate)Ú_UploadFile__input_valueÚ_r   c                 C   s&   t |tƒstdt|ƒ› �ƒ‚tt|ƒS r1   )r2   r3   r4   r5   r
   r   )r,   r6   r7   r#   r#   r$   Ú	_validate–   s    
zUploadFile._validate)Úfield_schemar   c                 C   s   |  dddœ¡ d S ©NÚstringÚbinary)r5   Úformat)Úupdate)r,   r9   r#   r#   r$   Ú__modify_schema__�   s    zUploadFile.__modify_schema__)Úcore_schemaÚhandlerr   c                 C   s
   dddœS r:   r#   )r,   r@   rA   r#   r#   r$   Ú__get_pydantic_json_schema__¡   s    z'UploadFile.__get_pydantic_json_schema__)ÚsourcerA   r   c                 C   s   ddl m} || jƒS )Né   )Ú"with_info_plain_validator_function)Z
_compat.v2rE   r8   )r,   rC   rA   rE   r#   r#   r$   Ú__get_pydantic_core_schema__§   s    z'UploadFile.__get_pydantic_core_schema__)r%   )!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Ú__annotations__r   ÚstrÚintr   Úbytesr   r&   r(   r*   Úclassmethodr   r   r   r   r/   r.   r8   r   r?   r   r   r   rB   rF   Ú__classcell__r#   r#   r!   r$   r      sx   
!ÿÿÿÿÿÿö ÷ÿÿÿöÿÿÿö" þ þr   c                   @   s<   e Zd ZdZedœdd„Zedœdd„Zeedœd	d
„Z	dS )ÚDefaultPlaceholderz·
    You shouldn't use this class directly.

    It's used internally to recognize when a default value has been overwritten, even
    if the overridden default value was truthy.
    ©Úvaluec                 C   s
   || _ d S r-   rR   )r    rS   r#   r#   r$   Ú__init__¸   s    zDefaultPlaceholder.__init__r)   c                 C   s
   t | jƒS r-   )ÚboolrS   r+   r#   r#   r$   Ú__bool__»   s    zDefaultPlaceholder.__bool__)Úor   c                 C   s   t |tƒo|j| jkS r-   )r2   rQ   rS   )r    rW   r#   r#   r$   Ú__eq__¾   s    zDefaultPlaceholder.__eq__N)
rG   rH   rI   rJ   r   rT   rU   rV   ÚobjectrX   r#   r#   r#   r$   rQ   °   s   rQ   ÚDefaultType)rS   r   c                 C   s   t | ƒS )zº
    You shouldn't use this function directly.

    It's used internally to recognize when a default value has been overwritten, even
    if the overridden default value was truthy.
    )rQ   rR   r#   r#   r$   ÚDefaultÅ   s    r[   N)Útypingr   r   r   r   r   r   r   r	   r
   Zannotated_docr   Zfastapi._compatr   r   r   Zstarlette.datastructuresr   r   r   r   r   r   r   r3   Ztyping_extensionsr   rQ   rZ   r[   r#   r#   r#   r$   Ú<module>   s   , 