o
    tf                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
 d)dd	Zd*ddZd+ddZejddG dd dZ			d,d-d d!Z	d.d/d$d%Zd0d'd(ZdS )1    )annotationsN)FileResponseStreamingResponsedatastr | bytesreturnstrc                 C  s    t | tr	|  } t|  S )zGenerate a unique identifier for a string or bytes.

    Parameters
    ----------
    string : str
        The string to hash.

    Returns
    -------
    str :
        A unique identifier for the string.
    )
isinstancer   encodehashlibmd5	hexdigest)r    r   U/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/servir/_util.pyr      s   
r   idc                 C  s   t | dd  d| S )a  Create a unique identifier for a string or bytes.

    Parameters
    ----------
    data : str | bytes
        The string or bytes to hash.
    id : str
        The identifier for the data.

    Returns
    -------
    str :
        A unique identifier for the string or bytes.
    N   -)r   )r   r   r   r   r   create_resource_identifier   s   r   pathpathlib.Pathstartintendbytesc                 C  sF   |  d}|| ||| W  d    S 1 sw   Y  d S )Nrb)openseekread)r   r   r   filer   r   r   read_file_byte_range1   s   
$r   T)frozenc                   @  s,   e Zd ZU ded< ded< eddd	Zd
S )ContentRanger   r   z
int | Noner   headerr   r   c                 C  sP   |   }td|}|std| \}}| t||r$t|dS ddS )a,  Parse 'Range' header into integer interval.

        Does not support multiple ranges.

        Parameters
        ----------
        content_range : str
            The 'Range' header.

        Returns
        -------
        tuple[int, int]
            The start and end of the byte-range.
        z^bytes=(\d+)-(\d+)?,?$z:Invalid 'Range' header. Must be of the form 'bytes=0-499'.Nr   r   )striplowerrematch
ValueErrorgroupsr   )clsr"   content_range_headerr'   Zrange_startZ	range_endr   r   r   parse_header<   s   
zContentRange.parse_headerN)r"   r   r   r!   )__name__
__module____qualname____annotations__classmethodr,   r   r   r   r   r!   7   s
   
 r!   content_rangeContentRange | None
media_type
str | NoneheadersNone | typing.Mapping[str, str]r   c           	      C  s   |   j}|sd|d }}d}i |pi }|  }n*d}|j|jp%|d }}i |p,i d| d| d| dd	}t| ||d d
}tt||d< t|g|||dS )Nr            zbytes r   /r   )zContent-RangezAccept-Rangesr#   zContent-Length)contentr4   status_coder6   )	statst_size
read_bytesr   r   r   r   lenr   )	r   r2   r4   r6   	file_sizer   r   r=   r<   r   r   r   create_streaming_file_responseZ   s0   

rC   r+    FileResponse | StreamingResponsec                 C  s0   t | }|rt|}t| ||dS t| |dS )N)r   r2   r4   )r   r4   )guess_media_typer!   r,   rC   r   )r   r+   r4   r2   r   r   r   create_file_response}   s   
rF   str | pathlib.Pathc                 C  s   t | d pdS )zGuess the media type of a file.

    Parameters
    ----------
    path : pathlib.Path
        The path to the file.

    Returns
    -------
    str
        The media type.
    r   zapplication/octet-stream)	mimetypes
guess_type)r   r   r   r   rE      s   rE   )r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )NNN)
r   r   r2   r3   r4   r5   r6   r7   r   r   )N)r   r   r+   r5   r   rD   )r   rG   r   r   )
__future__r   dataclassesr   rH   pathlibr&   typingZstarlette.responsesr   r   r   r   r   	dataclassr!   rC   rF   rE   r   r   r   r   <module>   s(    



$%