def post(self): content = request.json.get('content', None) file_name = request.json.get('fileName', None) if not content or not file_name: return jsonify(code=-1, msg="参数不对") filename = secure_filename( g.user.username + datetime.datetime.now().strftime('_%H-%M-%S-%f_') + file_name).lower() local_path = os.path.join(Cf.upload_path, filename) # file.save(local_path) file = open(local_path, encoding="utf-8", mode="w") file.write(content) file.close() # 写入数据库 from app.database_models import Code code = Code() code.user_id = g.user.id code.local_path = local_path # 根据后缀判断语言 suffix = os.path.splitext(filename)[-1] from app.code_manager import CodeType if suffix == '.py': code.code_type = CodeType.PYTHON3 elif suffix == '.c': code.code_type = CodeType.C elif suffix == '.cpp': code.code_type = CodeType.CPP elif suffix == '.java': code.code_type = CodeType.JAVA else: code.code_type = CodeType.FILE session = AppUtils.get_session() try: session.add(code) session.commit() return jsonify(code=ResponseCode.OK_RESPONSE, data={'id': code.id, 'url': AppUtils.get_network_url(os.path.join(Cf.upload_path, filename))}) finally: session.close()
def get_download_url(self): from app_utils import AppUtils return AppUtils.get_network_url(self.local_path)