Skip to content

Commit

Permalink
Merge pull request #56 from TaleLin/dev
Browse files Browse the repository at this point in the history
Feat/file upload (#55)
  • Loading branch information
colorful3 authored Jun 3, 2019
2 parents 0a631e6 + 24026a0 commit ab9502b
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Flask = "==1.0.2"
Flask-SQLAlchemy = "==2.3.2"
Flask-WTF = "==0.14.2"
Flask-Cors = "==2.1.0"
Lin-CMS = "==0.1.1b1"
Lin-CMS = "==0.1.1b3"

[dev-packages]
pytest = "*"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ Lin-CMS 是林间有风团队经过大量项目实践所提炼出的一套**内

## 最新版本

核心库:0.1.1b1
核心库:0.1.1b3

示例工程:0.1.0-beta.1
示例工程:0.1.0-beta.2


### 文档地址
Expand Down Expand Up @@ -177,4 +177,4 @@ pipenv shell

## 下个版本开发计划

- [ ] 调整 jwt 机制,增强灵活性
- [ ] 系统访问日志、错误日志
2 changes: 2 additions & 0 deletions app/api/cms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def create_cms():
from .admin import admin_api
from .user import user_api
from .log import log_api
from .file import file_api
from .test import test_api
admin_api.register(cms)
user_api.register(cms)
log_api.register(cms)
file_api.register(cms)
test_api.register(cms)
return cms
20 changes: 20 additions & 0 deletions app/api/cms/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
:copyright: © 2019 by the Lin team.
:license: MIT, see LICENSE for more details.
"""
from flask import request, jsonify
from lin import login_required
from lin.redprint import Redprint

from app.extensions.file.local_uploader import LocalUploader

file_api = Redprint('file')


@file_api.route('/', methods=['POST'])
@login_required
def post_file():
files = request.files
uploader = LocalUploader(files)
ret = uploader.upload()
return jsonify(ret)
13 changes: 12 additions & 1 deletion app/api/cms/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from lin.redprint import Redprint

from app.libs.error_code import RefreshException
from app.validators.forms import LoginForm, RegisterForm, ChangePasswordForm, UpdateInfoForm
from app.validators.forms import LoginForm, RegisterForm, ChangePasswordForm, UpdateInfoForm, \
AvatarUpdateForm

user_api = Redprint('user')

Expand Down Expand Up @@ -136,6 +137,16 @@ def get_allowed_apis():
return jsonify(user)


@user_api.route('/avatar', methods=['PUT'])
@login_required
def set_avatar():
form = AvatarUpdateForm().validate_for_api()
user = get_current_user()
with db.auto_commit():
user.avatar = form.avatar.data
return Success(msg='更新头像成功')


def _register_user(form: RegisterForm):
with db.auto_commit():
# 注意:此处使用挂载到manager上的user_model,不可使用默认的User
Expand Down
2 changes: 1 addition & 1 deletion app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create_tables(app):


def create_app(register_all=True):
app = Flask(__name__)
app = Flask(__name__, static_folder='./assets')
app.config.from_object('app.config.setting')
app.config.from_object('app.config.secure')
if register_all:
Expand Down
9 changes: 9 additions & 0 deletions app/extensions/file/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 文件相关配置
FILE = {
"STORE_DIR": 'app/assets',
"SINGLE_LIMIT": 1024 * 1024 * 2,
"TOTAL_LIMIT": 1024 * 1024 * 20,
"NUMS": 10,
"INCLUDE": set([]),
"EXCLUDE": set([])
}
41 changes: 41 additions & 0 deletions app/extensions/file/local_uploader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from flask import current_app
from werkzeug.utils import secure_filename

from lin.core import File
from lin.file import Uploader


class LocalUploader(Uploader):

def upload(self):
ret = []
site_domain = current_app.config.get('SITE_DOMAIN')\
if current_app.config.get('SITE_DOMAIN') else 'http://127.0.0.1:5000'
for single in self._file_storage:
file_md5 = self._generate_md5(single.read())
single.seek(0)
exists = File.query.filter_by(md5=file_md5).first()
if exists:
ret.append({
"key": single.name,
"id": exists.id,
"url": site_domain + '/assets/' + exists.path
})
else:
absolute_path, relative_path, real_name = self._get_store_path(single.filename)
secure_filename(single.filename)
single.save(absolute_path)
file = File.create_file(
name=real_name,
path=relative_path,
extension=self._get_ext(single.filename),
size=self._get_size(single),
md5=file_md5,
commit=True
)
ret.append({
"key": single.name,
"id": file.id,
"url": site_domain + '/assets/' + file.path
})
return ret
6 changes: 6 additions & 0 deletions app/validators/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ class UpdateUserInfoForm(Form):
])


class AvatarUpdateForm(Form):
avatar = StringField('头像', validators=[
DataRequired(message='请输入头像url')
])


class BookSearchForm(Form):
q = StringField(validators=[DataRequired(message='必须传入搜索关键字')]) # 前端的请求参数中必须携带`q`

Expand Down
6 changes: 6 additions & 0 deletions code.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

10100 refresh token 获取失败

10110 文件体积过大

10120 文件数量过多

10130 文件扩展名不符合规范

20000 werkzeug 中的HTTP EXCEPTION,error_code统一为1007,前端应读取msg

## 项目使用的状态码
Expand Down
6 changes: 3 additions & 3 deletions plugin_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ def __update_setting(self, new_setting):
sub_str = 'PLUGIN_PATH = ' + self.__format_setting(final_setting)

setting_path = self.app.config.root_path + '/config/setting.py'
with open(setting_path, 'r') as f:
with open(setting_path, 'r', encoding='UTF-8') as f:
content = f.read()
pattern = 'PLUGIN_PATH = \{([\s\S]*)\}+.*?'
result = re.sub(pattern, sub_str, content)

with open(setting_path, 'w+') as f:
with open(setting_path, 'w+', encoding='UTF-8') as f:
f.write(result)

def __get_all_plugins(self):
Expand Down Expand Up @@ -255,7 +255,7 @@ def __generate_plugin_graph(self):
'.', '/').replace('app', '')
requirements_path = self.app.config.root_path + \
plugin_path + '/requirements.txt'
with open(requirements_path, 'r') as f:
with open(requirements_path, 'r', encoding='UTF-8') as f:
while True:

# 正则匹配requirements的每一行的信息
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Flask-WTF==0.14.2
idna==2.6
itsdangerous==1.1.0
Jinja2==2.10
Lin-CMS==0.1.1b1
Lin-CMS==0.1.1b3
MarkupSafe==1.1.1
pipfile==0.0.2
PyJWT==1.7.1
Expand Down

0 comments on commit ab9502b

Please sign in to comment.