o
    g                     @   s   d dl mZ d dlmZmZ d dlmZmZ d dlZd dl	Z	d dl
Z
d dlZd dlZzd dlmZ W n eyA   d dlmZ Y nw G dd deZdS )	    )BaseAdapter)urlparseunquote)ResponsecodesN)BytesIO)StringIOc                       s.   e Zd Zd fdd	Zdd Zdd Z  ZS )	FileAdapterTc                    s   t t|   || _d S N)superr	   __init___set_content_length)selfset_content_length	__class__ F/home/ubuntu/webapp/venv/lib/python3.10/site-packages/requests_file.pyr      s   
zFileAdapter.__init__c              
   K   sP  |j dvrtd|j  t|j}|jr|jdkrtdt }||_zdd |jdD }|r@|d s@|	d |r@|d r5t
d	d
 |D rSttjttj|r|d dsc|d dr|	d}|dru|dd d }|r|d s|	d |r|d r{nd}|tj tjj|  }|rtj|stjtjj|g|R   }t|d|_|jj|j_W nM ty } z@|jtjkrtj|_n|jtjkrtj|_ntj|_t| t!"d}	t#|	|_| j$rt%|	|j&d< |jj|j_W Y d}~|S d}~ww tj'|_|j|_t(|j) }
t*+|
j,r&| j$r&|
j-|j&d< |S )zWraps a file, described in request, in a Response object.

        :param request: The PreparedRequest` being "sent".
        :returns: a Response object containing the file
        )GETHEADzInvalid request method %s	localhostz5file: URLs with hostname components are not permittedc                 S   s   g | ]}t |qS r   )r   .0pr   r   r   
<listcomp>0   s    z$FileAdapter.send.<locals>.<listcomp>/r   c                 s   s    | ]}t j|v V  qd S r
   )ossepr   r   r   r   	<genexpr>8   s    z#FileAdapter.send.<locals>.<genexpr>|:N rbFzContent-Length).method
ValueErrorr   urlnetlocr   requestpathsplitpopanyIOErrorerrnoENOENTr   strerrorendswithr   join
splitdriveioopenrawcloserelease_connEACCESr   	forbiddenstatus_code	not_foundbad_requeststrencodelocalegetpreferredencodingr   r   lenheadersokfstatfilenostatS_ISREGst_modest_size)r   r(   kwargs	url_partsresp
path_parts
path_driver)   eresp_str	resp_statr   r   r   send   sh   









zFileAdapter.sendc                 C   s   d S r
   r   )r   r   r   r   r7   x   s   zFileAdapter.close)T)__name__
__module____qualname__r   rS   r7   __classcell__r   r   r   r   r	      s    cr	   )requests.adaptersr   requests.compatr   r   requestsr   r   r.   r   rG   r@   r4   r   ImportErrorr   r	   r   r   r   r   <module>   s    