Files
order/common/libs/UploadService.py
2019-08-02 18:24:04 +08:00

41 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
from werkzeug.utils import secure_filename
from application import app,db
from common.libs.Helper import getCurrentDate
import datetime
import os,stat,uuid
from common.models.Image import Image
class UploadService():
@staticmethod
def uploadByFile( file ):
config_upload = app.config['UPLOAD']
resp = { 'code':200,'msg':'操作成功~~','data':{} }
filename = secure_filename( file.filename )
ext = filename.rsplit(".",1)[1]
if ext not in config_upload['ext']:
resp['code'] = -1
resp['msg'] = "不允许的扩展类型文件"
return resp
root_path = app.root_path + config_upload['prefix_path']
#不使用getCurrentDate创建目录为了保证其他写的可以用这里改掉服务器上好像对时间不兼容
file_dir = datetime.datetime.now().strftime("%Y%m%d")
save_dir = root_path + file_dir
if not os.path.exists( save_dir ):
os.mkdir( save_dir )
os.chmod( save_dir,stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO )
file_name = str( uuid.uuid4() ).replace("-","") + "." + ext
file.save( "{0}/{1}".format( save_dir,file_name ) )
model_image = Image()
model_image.file_key = file_dir + "/" + file_name
model_image.created_time = getCurrentDate()
db.session.add( model_image)
db.session.commit()
resp['data'] = {
'file_key': model_image.file_key
}
return resp