#NEWLINE# def convert_file_to_attachment( post , filename ) : #NEWLINE# #INDENT# fullpath = os . path . join ( current_app . root_path , '_data' , post . path , 'files' , filename ) #NEWLINE# if ( not os . path . exists ( fullpath ) ) : #NEWLINE# #INDENT# print ( 'could not find' , fullpath ) #NEWLINE# return #NEWLINE# #UNINDENT# now = post . published #NEWLINE# storage_path = '{}/{:02d}/{:02d}/{}' . format ( now . year , now . month , now . day , ( ( '' . join ( ( random . choice ( ( string . ascii_letters + string . digits ) ) for _ in range ( 8 ) ) ) + '-' ) + filename ) ) #NEWLINE# ( mimetype , _ ) = mimetypes . guess_type ( filename ) #NEWLINE# attachment = Attachment ( filename = filename , mimetype = mimetype , storage_path = storage_path ) #NEWLINE# print ( attachment . disk_path ) #NEWLINE# os . makedirs ( os . path . dirname ( attachment . disk_path ) , exist_ok = True ) #NEWLINE# shutil . copy2 ( fullpath , attachment . disk_path ) #NEWLINE# post . attachments . append ( attachment )