Esempio n. 1
0
def upload():
    u"""UEditor文件上传接口
    config 配置文件
    result 返回结果
    """
    mimetype = 'application/json'
    result = {}
    action = request.args.get('action')
    # 解析JSON格式的配置文件

    CONFIG = setting.EDITOR_CONFIG
    if action == 'config':
        # 初始化时,返回配置文件给客户端
        result = CONFIG
    elif action in ('uploadimage', 'uploadfile', 'uploadvideo',):
        # 图片、文件、视频上传
        if action == 'uploadimage':
            fieldName = CONFIG.get('imageFieldName')
        elif action == 'uploadvideo':
            fieldName = CONFIG.get('videoFieldName')
        else:
            fieldName = CONFIG.get('fileFieldName')
        if fieldName in request.files:
            try:
                request_file = request.files[fieldName]
                with db_session_cm() as session:
                    filename = image_resources.save(request_file, folder=str(current_user.id))
                    irs = ImageResource(filename, current_user.id)
                    name, suffix = os.path.splitext(request_file.filename)
                    irs.format = suffix
                    session.add(irs)
                    session.commit()
                    image = session.query(ImageResource).get(irs.id)
                    if image is None:
                        result['state'] = 'SUCCESS'
                        result['url'] = ''
                    else:
                        url = image_resources.url(image.path)
                        result['state'] = 'SUCCESS'
                        result['url'] = url
                        result['title'] = filename
                        result = json.dumps(result)
                        res = make_response(result)
                        return res
            except Exception, e:
                common_logger.error(traceback.format_exc(e))
            else:
                result['state'] = '上传接口出错'
Esempio n. 2
0
 def cover_image_url(self):
     if self.cover_image:
         url = image_resources.url(self.cover_image.path)
         return url
     else:
         return ""
Esempio n. 3
0
 def url(self):
     return image_resources.url(self.path)