Thomas Richardson commited on
Commit
201ecf7
·
unverified ·
2 Parent(s): a33612478967a7

Merge pull request #208 from ttt246/feature/email_attach_194

Browse files
Brain/src/rising_plugin/gmail/email_plugin.py CHANGED
@@ -219,12 +219,12 @@ class EmailPlugin:
219
 
220
  messages.append(
221
  {
222
- "From": from_address,
223
- "To": to_address,
224
- "Date": date,
225
- "CC": cc,
226
- "Subject": subject,
227
- "Message Body": body,
228
  }
229
  )
230
 
@@ -232,12 +232,12 @@ class EmailPlugin:
232
  if not messages:
233
  messages.append(
234
  {
235
- "From": "",
236
- "To": "",
237
- "Date": "",
238
- "CC": "",
239
- "Subject": "",
240
- "Message Body": "There are no Emails",
241
  }
242
  )
243
  return json.dumps(messages)
@@ -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
 
219
 
220
  messages.append(
221
  {
222
+ "from": from_address,
223
+ "to": to_address,
224
+ "date": date,
225
+ "cc": cc,
226
+ "subject": subject,
227
+ "body": body,
228
  }
229
  )
230
 
 
232
  if not messages:
233
  messages.append(
234
  {
235
+ "from": "",
236
+ "to": "",
237
+ "date": "",
238
+ "cc": "",
239
+ "subject": "",
240
+ "body": "There are no Emails",
241
  }
242
  )
243
  return json.dumps(messages)
 
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
@@ -30,12 +30,12 @@ def construct_blueprint_email_api() -> APIRouter:
30
 
31
  @generator.response(
32
  status_code=200, schema={"message": "message", "result": [{
33
- "From": "testfrom@test.com",
34
- "To": "test@gmail.com",
35
- "Date": "Tue, 04 Jul 2023 12:55:19 +0000",
36
- "CC": "",
37
- "Subject": "subject",
38
- "Message Body": "message"
39
  }]}
40
  )
41
 
@@ -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
 
30
 
31
  @generator.response(
32
  status_code=200, schema={"message": "message", "result": [{
33
+ "from": "testfrom@test.com",
34
+ "to": "test@gmail.com",
35
+ "date": "Tue, 04 Jul 2023 12:55:19 +0000",
36
+ "cc": "",
37
+ "subject": "subject",
38
+ "body": "message"
39
  }]}
40
  )
41
 
 
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()