o
    ii                     @   s   d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
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lZd dlZeed	d
 dgZdZdd Zejddgddd Zejddgddd Zdd ZdS )    )app)Flask	send_filejsonifyrequest)CORS)service_account)buildMediaFileUpload)MediaIoBaseDownload)connect_to_databaseN*)originsz%https://www.googleapis.com/auth/drivezconfigs/credentials.jsonc                  C   s"   t jjttd} tdd| d}|S )N)scopesdrivev3)credentials)r   Credentialsfrom_service_account_fileSERVICE_ACCOUNT_FILESCOPESr	   )r   service r   4/var/www/edux/Edux_v2/controllers/file_controller.pyget_drive_service   s
   r   z/file/downloadGET)methodsc               
   C   s>  zvt d t } d}t d |  jd| dddd }tjd	| |d
g }|s6t	ddidfW S |d d }t d| |  j
|d}t dt| t }t||}d}|du rn| \}	}|d |du s_t|dd|dW S  ty }
 zt |
 tjj|
ddd t	t|
ddfW  Y d }
~
S d }
~
ww )Nzfile download method startstemplate.xlsxzBefore resultszname=''r   files(id, name))qspacesfieldszresults: %sfileserrorzFile not foundi  r   idfileidfileIdr   Fzapplication/vnd.ms-excelTmimetypeas_attachmentattachment_filename
stack_infoexc_infor$     )printr   r#   listexecuter   loggerinfogetr   	get_mediastrioBytesIOr   
next_chunkseekr   	Exceptionr$   )r   	file_nameresultsitemsfile_idr   fh
downloaderdonestatuser   r   r   file_download   sN   



rH   z/api/download/statusc            
   
   C   s  z^t d t } tjd}t d| |  j|d}t }t	||}|  j|dd
 }|d}d}|du rQ| \}}tjd	t| d
  |du s:|d t|ddddW S  ty }	 ztjj|	ddd tt|	ddfW  Y d }	~	S d }	~	ww )NzStatus Method StartsrB   zfile Idr'   zid, name)r(   r"   nameFzDownload: %dd   r   zAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetTr   r)   r-   r0   r1   )r2   r   r   argsr7   r#   r8   r:   r;   r   r4   r<   r   r5   r6   intprogressr=   r   r>   r$   r   r9   )
r   rB   file_requestrC   rD   file_metadatar?   rE   rF   rG   r   r   r   download_statusK   s6   



rP   c                 C   sz   t d |  jddd }|dg }|st d d S t dt| d |D ]}t d	|d
  d|d  d q)d S )Nu%   📂 Listing files in Google Drive...rJ   r   )pageSizer"   r#   u   📭 No files found.u   📁 Found z	 file(s):u   🗎 rI   z (ID: r%   ))r2   r#   r3   r4   r7   len)r   r@   r#   filer   r   r   list_drive_filesj   s    rU   )	flask_appr   flaskr   r   r   r   
flask_corsr   google.oauth2r   googleapiclient.discoveryr	   r
   googleapiclient.httpr   configs.databaseConnectionr   r:   osr   r   r   routerH   rP   rU   r   r   r   r   <module>   s&    
2
