o
    gI                      @   s$  d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZ ddl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ZedZG dd dZG dd dZedkrej ddZ!ej"ddZ#ee!e#dZ$ddgZ%e$&e%Z'dZ(e$)e(dZ*dS dS )z*Module for handling document uploads to S3    N)ThreadPoolExecutor)Path)ListOptionalUnion)
UploadFile)Team)Project)ClientConfig)DocUploader)S3BucketStorages3c                   @   sB   e Zd Zdededee fddZddede	fdd	Z
d
d ZdS )DocUploaderV2projectteamfilesc                 C   s2   t ||d| _|| _| j| _|| _g | _|| _d S )Nr   r   )DocumentUploaderS3uploaderr   project_filesr   uploaded_filesclient)selfr   r   r    r   ?/var/www/html/XCapMarket/utils/document_loader/DocUploaderV2.py__init__   s   
zDocUploaderV2.__init__ nested_pathc                    sX   g }|D ]!}| j j||dI dH }td| |r&||j||j|jd q|| _|S )zStore files on S3)upload_filer   Nzfile uploaded)	file_namefile_url	file_type	file_size)r   upload_filesprintappendfilenamecontent_typesizer   )r   r   r   upload_resultsfileupload_resultr   r   r   store_on_s3#   s&   
	zDocUploaderV2.store_on_s3c                 C   s   | j S )zGet the uploaded files)r   )r   r   r   r   get_uploaded_files<   s   z DocUploaderV2.get_uploaded_filesNr   )__name__
__module____qualname__r	   r   r   r   r   liststrr,   r-   r   r   r   r   r      s    r   c                
   @   s   e Zd ZdZdedefddZh dZdZde	d	e
d
eee	f fddZ	ddede	de	d
ee	 fddZd de	de	d
e	fddZd de	de	d
e	fddZded
e
fddZ	d!deee	ef  de	d
ee	 fddZdS )"r   z-Class to handle document uploads to S3 bucketr   r   c                 C   s&   || _ || _tjd }t|d| _dS )a  
        Initialize DocumentUploader with project and client configuration

        Args:
            project (Project): Project instance containing project details
            client (ClientConfig, optional): Client configuration. Defaults to XCM client.
        xcap_s3_storage)bucket_nameN)r   r   osenvironr   
s3_storage)r   r   r   r4   r   r   r   r   Q   s   
zDocumentUploaderS3.__init__>   .csv.doc.gif.jpg.odt.pdf.png.ppt.rar.rtf.svg.txt.xls.zip.docx.jpeg.pptx.xlsxi  @r&   filesizereturnc                 C   sP   t |j }|| jvrdd| dfS || jkr&dd| jd d  dfS dS )z
        Validate file extension and size

        Args:
            filename (str): Name of the file
            filesize (int): Size of file in bytes

        Returns:
            tuple[bool, str]: (is_valid, error_message)
        FzFile extension z not allowedz*File size exceeds maximum allowed size of i   MB)Tr   )r   suffixlowerALLOWED_EXTENSIONSMAX_FILE_SIZE)r   r&   rK   extr   r   r   validate_filez   s   

z DocumentUploaderS3.validate_filetempr   r   temp_dirr   c           	   	      s  zv|  |j| |I dH \}}|st|tj|dd t||j }t|d4 I dH '}|	dI dH  }rL|
|I dH  |	dI dH  }s:W d  I dH  n1 I dH s\w   Y  | t||I dH W dt v rwt| S S dt v rt| w w )a  
        Handle FastAPI UploadFile upload

        Args:
            upload_file (UploadFile): FastAPI UploadFile object
            temp_dir (str): Temporary directory for file processing

        Returns:
            Optional[str]: S3 URL if successful
        NT)exist_okwbi   	temp_path)rS   r&   _get_file_size
ValueErrorr6   makedirsr   aiofilesopenreadwriteupload_file_asyncr3   localsunlink)	r   r   rU   r   is_validerrorrX   fcontentr   r   r   r#      s(   (
zDocumentUploaderS3.upload_files	file_pathc                    sL   t  }t }||| j||I dH W  d   S 1 sw   Y  dS )z2
        Asynchronously upload file to S3
        N)asyncioget_event_loopr   run_in_executor_upload_single_file)r   rg   r   looppoolr   r   r   r`      s   
$z$DocumentUploaderS3.upload_file_asyncc                 C   s|   t |j}t|d pd}t| jj}|r&| jj d| d| d| }n| jj d| d| }| j	j
||d | j	jS )zC
        Upload single file to S3 with proper content type
        r   zapplication/octet-stream/)s3_file_pathsource_file_path)r   name	mimetypes
guess_typer3   r   
project_idr   idr8   upload_file_to_s3_buckets3_file_location)r   rg   r   r   r'   project_id_strro   r   r   r   rk      s   
 z&DocumentUploaderS3._upload_single_filec                    sN   d}| dI dH  }r|t|7 }| dI dH  }s|dI dH  |S )z#Get total file size from UploadFiler   i    N)r^   lenseek)r   r   r(   chunkr   r   r   rY      s   z!DocumentUploaderS3._get_file_sizer   c                    s   g }|D ]?}zt |tr| ||I dH }n| |I dH }|| W q tyD } ztd| dt|  W Y d}~qd}~ww |S )z
        Bulk upload multiple files

        Args:
            files: List of file paths or UploadFile objects
            temp_dir: Temporary directory for processing

        Returns:
            List[str]: List of S3 URLs
        NzFailed to upload z: )	
isinstancer   r#   r`   r%   	Exceptionloggingrd   r3   )r   r   rU   uploaded_urlsr*   urler   r   r   bulk_upload   s   
zDocumentUploaderS3.bulk_uploadN)rT   r   r.   )rT   )r/   r0   r1   __doc__r	   r   r   rP   rQ   r3   inttupleboolrS   r   r   r#   r`   rk   rY   r   r   r   r   r   r   r   r   N   s6    
&
	r   __main__your_project_id)rt   z$d4d6f97c-3f5c-4f19-983d-62f754f6c9f4)team_idr   zpath/to/your/file1.pdfzpath/to/your/file2.docxzThis is some test contentztest.txt)+r   rh   r~   rr   r6   concurrent.futuresr   pathlibr   typingr   r   r   r\   fastapir   models.teamr   +services.ppt_generator.data_classes.projectr	   utils.client_checkr
   !utils.document_loader.DocUploaderr   utils.s3_storager   boto3r   r   r   r   r/   check_project_in_dbr   check_team_in_dbr   r   files_to_uploadr#   r   rf   upload_file_contentuploaded_urlr   r   r   r   <module>   s:    
5 0
