Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

covert markdown and sdoc #298

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
VirusScanner, Statistics, CountUserActivity, CountTrafficInfo, ContentScanner,\
WorkWinxinNoticeSender, FileUpdatesSender, RepoOldFileAutoDelScanner,\
DeletedFilesCountCleaner
from seafevents.file_converter.converter_server import ConverterServer


class App(object):
Expand All @@ -17,6 +18,7 @@ def __init__(self, config, ccnet_config, seafile_config,
self._events_handler = EventsHandler(config)
self._count_traffic_task = CountTrafficInfo(config)
self._update_login_record_task = CountUserActivity(config)
self._converter_server = ConverterServer(config)

if self._bg_tasks_enabled:
self._index_updater = IndexUpdater(config)
Expand All @@ -35,6 +37,7 @@ def serve_forever(self):
self._events_handler.start()
self._update_login_record_task.start()
self._count_traffic_task.start()
self._converter_server.start()

if self._bg_tasks_enabled:
self._file_updates_sender.start()
Expand Down
3 changes: 3 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
DTABLE_WEB_SERVER = getattr(seahub_settings, 'DTABLE_WEB_SERVER', None)
SEATABLE_EX_PROPS_BASE_API_TOKEN = getattr(seahub_settings, 'SEATABLE_EX_PROPS_BASE_API_TOKEN', None)
EX_PROPS_TABLE = getattr(seahub_settings, 'EX_PROPS_TABLE', None)
SECRET_KEY = getattr(seahub_settings, 'SECRET_KEY', '')
FILE_SERVER_ROOT = getattr(seahub_settings, 'FILE_SERVER_ROOT', '')
SEAHUB_SERVER = getattr(seahub_settings, 'SERVICE_URL', '')
except ImportError:
logger.critical("Can not import seahub settings.")
raise RuntimeError("Can not import seahub settings.")
Expand Down
1 change: 1 addition & 0 deletions file_converter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
83 changes: 83 additions & 0 deletions file_converter/apis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import json
import logging
import os
import jwt
from pathlib import Path

from flask import request, Flask

from seafevents.file_converter.sdoc_converter import md2sdoc
from seafevents.file_converter.markdown_converter import sdoc2md
from seafevents.file_converter.utils import get_file_by_token, upload_file_by_token
from seafevents.app.config import SECRET_KEY

logger = logging.getLogger(__name__)
flask_app = Flask(__name__)

def check_auth_token(req):
auth = req.headers.get('Authorization', '').split()
if not auth or auth[0].lower() != 'token' or len(auth) != 2:
return False

token = auth[1]
if not token:
return False

private_key = SECRET_KEY
try:
jwt.decode(token, private_key, algorithms=['HS256'])
except (jwt.ExpiredSignatureError, jwt.InvalidSignatureError) as e:
return False

return True


@flask_app.route('/api/v1/file-convert/', methods=['POST'])
def convert_markdown_to_sdoc():
is_valid = check_auth_token(request)
if not is_valid:
return {'error_msg': 'Permission denied'}, 403
try:
data = json.loads(request.data)
except Exception as e:
logger.exception(e)
return {'error_msg': 'Bad request.'}, 400

path = data.get('path')
username = data.get('username')
doc_uuid = data.get('doc_uuid')

extension = Path(path).suffix
if extension not in ['.md', '.sdoc']:
return {'error_msg': 'path invalid.'}, 400

download_token = data.get('download_token')
upload_token = data.get('upload_token')

file_content = get_file_by_token(path, download_token).decode()

parent_dir = os.path.dirname(path)
file_name = os.path.basename(path)

if extension == '.md':
if file_content:
file_content = md2sdoc(file_content, username=username)
file_name = file_name[:-2] + 'sdoc'
else:
if file_content:
file_content = json.loads(file_content)
file_content = sdoc2md(file_content, doc_uuid=doc_uuid)
file_name = file_name[:-4] + 'md'

try:
resp = upload_file_by_token(parent_dir, file_name, upload_token, file_content)
if not resp.ok:
logger.error(resp.text)
return {'error_msg': resp.text}, 500

except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return {'error_msg': error_msg}, 500

return {'success': True}, 200
27 changes: 27 additions & 0 deletions file_converter/converter_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from threading import Thread
from gevent.pywsgi import WSGIServer

from seafevents.file_converter.apis import flask_app


class ConverterServer(Thread):

def __init__(self, config):
Thread.__init__(self)
self._parse_config(config)

self._server = WSGIServer((self._host, int(self._port)), flask_app)

def _parse_config(self, config):
if config.has_option('FILE CONVERTER', 'host'):
self._host = config.get('FILE CONVERTER', 'host')
else:
self._host = '127.0.0.1'

if config.has_option('FILE CONVERTER', 'port'):
self._port = config.getint('FILE CONVERTER', 'port')
else:
self._port = '8888'

def run(self):
self._server.serve_forever()
Loading