jupiter0913 commited on
Commit
1a24c7c
·
1 Parent(s): a336124

feature(#194): create folder for saving attach file and delete it after sending email...

Browse files
Brain/src/rising_plugin/gmail/email_plugin.py CHANGED
@@ -359,9 +359,16 @@ class EmailPlugin:
359
 
360
  return email_body
361
 
362
- def write_attachment(self, filename: str, file_content: str) -> str:
 
 
 
 
 
 
 
363
  file_content = base64.b64decode(file_content).decode("utf-8")
364
- file = open(filename, "w")
365
  file.write(file_content)
366
  file.close()
367
- return filename
 
359
 
360
  return email_body
361
 
362
+ def write_attachment(self, filename: str, file_content: str) -> (str, str):
363
+ # create folder for temporarily saving attached file
364
+ milliseconds = int(time.time() * 1000)
365
+ file_path = f"Brain/assets/{milliseconds}/{filename}"
366
+ file_directory = f"Brain/assets/{milliseconds}"
367
+ os.mkdir(file_directory)
368
+
369
+ # file write
370
  file_content = base64.b64decode(file_content).decode("utf-8")
371
+ file = open(file_path, "w")
372
  file.write(file_content)
373
  file.close()
374
+ return file_path, file_directory
Brain/src/router/email_router.py CHANGED
@@ -1,5 +1,5 @@
1
  import json
2
-
3
  from fastapi import APIRouter
4
 
5
  from Brain.src.common.assembler import Assembler
@@ -119,7 +119,7 @@ def construct_blueprint_email_api() -> APIRouter:
119
  to_send=data.data.to_send,
120
  )
121
  else:
122
- file_name = email_manager.write_attachment(
123
  filename=data.data.filename, file_content=data.data.file_content
124
  )
125
 
@@ -133,6 +133,8 @@ def construct_blueprint_email_api() -> APIRouter:
133
  filename=file_name,
134
  )
135
 
 
 
136
  except Exception as e:
137
  if isinstance(e, BrainException):
138
  return e.get_response_exp()
 
1
  import json
2
+ import shutil
3
  from fastapi import APIRouter
4
 
5
  from Brain.src.common.assembler import Assembler
 
119
  to_send=data.data.to_send,
120
  )
121
  else:
122
+ file_name, file_directory = email_manager.write_attachment(
123
  filename=data.data.filename, file_content=data.data.file_content
124
  )
125
 
 
133
  filename=file_name,
134
  )
135
 
136
+ shutil.rmtree(file_directory)
137
+
138
  except Exception as e:
139
  if isinstance(e, BrainException):
140
  return e.get_response_exp()