U
    i                     @   s   d dl Z d dlZd dlmZ e je je je je jeddZ	ddddZ
eeeeef dd	d
ZeddddZeedddZdS )    N)TupleZuploads	portfolio)returnc                   C   s   t jtdd d S )NT)exist_ok)osmakedirs_UPLOAD_DIR r	   r	   ?/var/www/html/me.goteku.com/backend/app/helpers/file_storage.py_ensure_dir   s    r   )	file_dataoriginal_filenamer   c              	   C   sh   t   tj|d  }t j | }ttj	t
|d}||  W 5 Q R X d| }||fS )zSave bytes to local storage.

    Returns:
        (stored_filename, view_url)
        stored_filename: UUID-based filename on disk, e.g. "abc123.pdf"
        view_url: path to the public serving endpoint, e.g. "/api/portfolio/attachments/abc123.pdf"
       wbz/api/portfolio/attachments/)r   r   pathsplitextloweruuiduuid4hexopenjoinr   write)r   r   extstored_filenamefhview_urlr	   r	   r
   	save_file   s    
r   )r   r   c                 C   s<   d| ksd| krdS t jt| }t j|r8t | dS )z4Delete a file from local storage (no-op if missing)./z..N)r   r   r   r   existsremove)r   r   r	   r	   r
   delete_file"   s
    r!   c                 C   s   t jt| S )z:Return the absolute filesystem path for a stored filename.)r   r   r   r   )r   r	   r	   r
   get_full_path,   s    r"   )r   r   typingr   r   r   dirnameabspath__file__r   r   bytesstrr   r!   r"   r	   r	   r	   r
   <module>   s   "
