a
    d                     @   st   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlmZ ddl	m
Z
 dd Zd	d
 ZdddZdddZdS )    N)download_url_to_fileget_dir)tqdm)urlparse   )
sizeof_fmtc           	      C   s   t  }d}d| i}|j||dd}t|}|rH||d< |j||dd}|j||dddid}d	|jv rt|jd	 d
d }nd}t||| dS )zDownload files from google drive.

    Ref:
    https://stackoverflow.com/questions/25010369/wget-curl-large-file-from-google-drive  # noqa E501

    Args:
        file_id (str): File id.
        save_path (str): Save path.
    z*https://docs.google.com/uc?export=downloadidT)paramsstreamZconfirmRangez	bytes=0-2)r	   r
   headerszContent-Range/r   N)requestsSessiongetget_confirm_tokenr   intsplitsave_response_content)	Zfile_idZ	save_pathsessionURLr	   responsetokenZresponse_file_size	file_size r   d/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/utils/download_util.pydownload_file_from_google_drive   s    
r   c                 C   s*   | j  D ]\}}|dr
|  S q
d S )NZdownload_warning)cookiesitems
startswith)r   keyvaluer   r   r   r   *   s    

r      c           	      C   s   |d ur(t t|| dd}t|}nd }t|dv}d}| |D ]F}||7 }|d ur~|d |dt| d|  |rF|| qF|d ur|	  W d    n1 s0    Y  d S )Nchunk)totalunitwbr   r   z	Download z / )
r   mathceilr   openiter_contentupdateset_descriptionwriteclose)	r   Zdestinationr   
chunk_sizeZpbarZreadable_file_sizefZdownloaded_sizer#   r   r   r   r   1   s    

r   Tc                 C   s   |du rt  }tj|d}tj|dd t| }tj|j}|durL|}tjtj||}tj|st	d|  d| d t
| |d|d |S )	aP  Load file form http url, will download models if necessary.

    Ref:https://github.com/1adrianb/face-alignment/blob/master/face_alignment/utils.py

    Args:
        url (str): URL to be downloaded.
        model_dir (str): The path to save the downloaded model. Should be a full path. If None, use pytorch hub_dir.
            Default: None.
        progress (bool): Whether to show the download progress. Default: True.
        file_name (str): The downloaded file name. If None, use the file name in the url. Default: None.

    Returns:
        str: The path to the downloaded file.
    NZcheckpointsT)exist_okzDownloading: "z" to 
)Zhash_prefixprogress)r   ospathjoinmakedirsr   basenameabspathexistsprintr   )urlZ	model_dirr3   	file_nameZhub_dirpartsfilenameZcached_filer   r   r   load_file_from_urlF   s    r@   )Nr"   )NTN)r'   r4   r   Z	torch.hubr   r   r   urllib.parser   miscr   r   r   r   r@   r   r   r   r   <module>   s   
