
    eTjR                    p    d Z ddlmZ ddlZddlZddlZddlZdgZd ZddZ	ddZ
ddZdd	Zdd
ZddZy)u  Google Drive logic for the shared jarvis-tools MCP service (Railway).

Same guarantees as the in-process Mac version, but creds come from ENV so it
runs headless on Railway and is reachable by every device:
  DRIVE_SA_JSON    the service-account key JSON (whole file, as one env var)
  DRIVE_FOLDER_ID  the "Jarvis Drive" folder id

A service account has no Drive of its own, so it only sees folders Ahmed shared
with it. Every op below is ALSO folder-jailed to DRIVE_FOLDER_ID as defence in
depth — nothing outside the shared folder is ever touched.

Note: only content ops are exposed remotely (list/read/write/delete). Local
file upload/download live in the Mac in-process tool — a remote server has no
access to a caller's local filesystem.
    )annotationsNz%https://www.googleapis.com/auth/drivec                     ddl m}  ddlm} t	        j
                  t        j                  d         }| j                  j                  |t              } |dd|d	      S )
Nr   )service_account)buildDRIVE_SA_JSON)scopesdrivev3F)credentialscache_discovery)google.oauth2r   googleapiclient.discoveryr   jsonloadsosenvironCredentialsfrom_service_account_info_SCOPES)r   r   infocredss       B/Users/ahmed/devFolder/Ultron/claude-voice/memory-service/drive.py_servicer      sQ    -/::bjj12D''AAW B E$E5II    c                 (    t         j                  d   S )NDRIVE_FOLDER_ID)r   r    r   r   
_folder_idr   #   s    ::'((r   c                   t        |      j                         }|syt               }	 | j                         j	                  |d      j                         }||j	                  d      xs g v r|S dS # t        $ r Y nw xY wd| d| d}| j                         j                  |dd	      j                         j	                  d
g       }|r|d   S dS )uC   Find a file by id or name — ONLY inside the shared folder (jail).Nzid,name,mimeType,parents)fileIdfieldsparents'z)' in parents and trashed=false and name='r	   zfiles(id,name,mimeType))qspacesr!   filesr   )strstripr   r&   getexecute	Exceptionlist)svcreffidfr$   r&   s         r   _resolver1   '   s    
c(..
C
,CIIKOO9  ;;B79 	
AEE),23q== 
C59#a@AIIKq#<  %giGR(8 
 58&$&s   AA2 0A2 2	A>=A>c                    t               } dt                d}| j                         j                  |ddd      j	                         j                  dg       }|syg }|D ]R  }|j                  d	      }|rt        |      d
z   dnd}|j                  d|d    d| d|d   d d  d|d    d	       T t        |       ddj                  |      z   S )Nr#   z' in parents and trashed=falser	   zmodifiedTime descz)files(id,name,mimeType,size,modifiedTime))r$   r%   orderByr!   r&   z(The shared Jarvis Drive folder is empty.sizei   KBu   —z- namez  (z, modifiedTime
   z) [id id]z file(s) in Jarvis Drive:

)
r   r   r&   r,   r*   r)   intappendlenjoin)r-   r$   r&   linesr0   szs         r   
list_filesrB   :   s    
*C
JL>78AIIK
G%8:  
 ')CC$ 
 9EUU6]%'Bb!Ur!F)Ct2a.?.D-E FdG9A' 	( 
 %j\45		%8HHHr   c                   t               }t        ||       }|sy|j                  dd      }|j                  d      r3|j	                         j                  |d   d      j                         }n1|j	                         j                  |d         j                         }t        |t              r|j                  d	d
      n
t        |      }|d d t        |      dkD  rdndz   }|d    d| S )N/No such file in the shared Jarvis Drive folder.mimeType zapplication/vnd.google-appsr9   
text/plain)r    rE   )r    utf-8ignorei@  u    …[truncated]r6   z:
)r   r1   r)   
startswithr&   exportr*   	get_media
isinstancebytesdecoder'   r>   )r.   r-   r0   mimedatabodysnips          r   	read_filerT   L   s    
*CcA@55R D45yy{!!4<!HPPRyy{$$AdG$4<<>-7e-D4;;w)#d)D;s4y4/?+RHDi[D6""r   c                R   ddl m} t        |       j                         } | syt	               }t        j                  |       d   xs d} |t        j                  t        |      j                  d            |d      }t        ||       }|rR|j                  d      | k(  r>|j                         j                  |d	   |
      j                          d|  d|d	    dS | t               gd}|j                         j!                  ||d	      j                         }d|  d|d	    dS )Nr   )MediaIoBaseUploadzwrite failed: need a file name.rG   rH   F)mimetype	resumabler6   r9   )r    
media_bodyzupdated z in Jarvis Drive (id z).)r6   r"   )rR   rY   r!   zcreated )googleapiclient.httprV   r'   r(   r   	mimetypes
guess_typeioBytesIOencoder1   r)   r&   updater*   r   create)	r6   contentrV   r-   rP   mediaexistingmetaouts	            r   
write_filerg   [   s   6t9??D0
*C%a(8LDbjjW)<)<W)EF&*e=ET"HHLL(D0		(4.UCKKM$4Xd^4DBGGjl^4D
))+

$5

F
N
N
PCdV0T2>>r   c                    t               }t        ||       }|sy|j                         j                  |d   ddi      j	                          d|d    dS )	NrD   r9   trashedT)r    rR   zDeleted r6   z from Jarvis Drive.)r   r1   r&   r`   r*   )r.   r-   r0   s      r   delete_filerj   m   sZ    
*CcA@IIKagY,=>FFHai[ 344r   )returnr'   )r.   r'   rk   zdict | None)r.   r'   rk   r'   )r6   r'   rb   r'   rk   r'   )__doc__
__future__r   r]   r   r[   r   r   r   r   r1   rB   rT   rg   rj   r   r   r   <module>rn      sH    # 	   	2
3J)'&I$#?$5r   