-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
52 lines (40 loc) · 1.34 KB
/
__init__.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
from flask import jsonify
from werkzeug.utils import secure_filename
from openedoo.core.libs import Blueprint, request
from .utils import random_char
UPLOAD_FOLDER = '/modules/module_media/uploads/'
module_media = Blueprint('module_media', __name__,
static_folder='uploads', static_url_path='/static')
def add_extension(mime_type):
return '.' + mime_type.split('/')[1]
def get_media_path(name):
cwd = os.getcwd()
return cwd+UPLOAD_FOLDER+name
def save_file(request):
mime_type = request.content_type
rand_name = random_char(length=33)
file_extension = add_extension(mime_type)
file_name = secure_filename(rand_name+file_extension)
file_path = get_media_path(file_name)
with open(file_path, 'w+b') as f:
f.write(request.get_data())
file_info = {
'type': mime_type,
'path': file_path,
'name': file_name,
'extension': file_extension
}
return file_info
@module_media.route('/', methods=['POST'])
def index():
saved_file = save_file(request)
host_name = request.host
file_link = '{host}/media/static/{f_name}'
file_link = file_link.format(host=host_name, f_name=saved_file['name'])
resp = {
'type': saved_file['type'],
'name': saved_file['name'],
'link': file_link
}
return jsonify(resp)