summaryrefslogtreecommitdiff
path: root/domestic/session/server_handling/download.py
blob: a51e3945f4d5f84eb0af18f955f082c92daa37e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os

from domestic.parse.error_exception_handling import *
from domestic.session.session_message import *
from domestic.utility.status_message import *
from domestic.make.make_directories import *
from domestic.global_state import *


@error_exception_handling
def download(message):
  assert message['file']

  filename = validate_dict_key(message, 'file')
  execute = validate_dict_key(message, 'execute')

  username = state['session']['username']
  make_directories([username, f'{username}/downloads'])
  root = f'{state["root"]}/{username}/downloads/{filename}'

  message['max_file_size'] = state['settings']['max-file-size']
  if execute:
    del message['execute']

  data = session_message(message, False, loading_text='downloading file...')
  download = validate_dict_key(data, 'download', False)
  
  if download:
    with open(root, 'wb') as f:
      f.write(download)
    
    if execute:
      os.startfile(root)

  status_message(data['message'], data['text_mode'])