示例#1
0
文件: views.py 项目: Sevncz/faces
def script_down(id):
    ip = request.remote_addr
    action_log = ActionLog(
        params='%s' % (id),
        action='/api/v1.0/script/down/%s' % id,
        description='脚本下载',
        ip=ip,
        user_id=g.user.id
    )

    script = Script.query.filter_by(id=id).first()

    rtn_stream = ''
    script_name = ''
    script_ext = ''
    try:
        if script and os.path.exists(script.file.encode('utf-8')):
            with open(script.file.encode('utf-8')) as file_:
                rtn_stream = file_.read()

            action_log.status = '200'
            db.session.add(action_log)
            db.session.commit()

            script_name = script.name.encode('utf-8').decode('latin-1')
            script_ext = script.ext
    except:
        traceback.print_exc()
        db.session.rollback()

    response = make_response(rtn_stream)
    response.headers['Content-Type'] = "application/octet-stream"
    response.headers['Content-Disposition'] = "attachment; filename=" + script_name + '.' + script_ext

    return response
示例#2
0
def action_log(req,act):
    request =req
    log =ActionLog(act)
    log.client_ip =request.remote_addr
    log.action_time =get_localtime()

    db.session.add(log)
    db.session.commit()
示例#3
0
文件: views.py 项目: Sevncz/faces
def script_create():
    data = request.form
    if not data or 'category_name' not in data:
        return jsonify(status='error', msg='参数错误')

    category_name = data.get('category_name')
    file = request.files['formData']

    ip = request.remote_addr

    action_log = ActionLog(
        params=category_name,
        action='/api/v1.0/script/create',
        description='脚本上传',
        ip=ip,
        user_id=g.user.id
    )

    try:
        if category_name:

            if check_category_sync(g.user.id, category_name):

                # category_name = category_name.decode('string-escape')
                category = Category.query.filter_by(name=category_name, user_id=g.user.id).first()
                if not category:
                    category = Category(
                        name=category_name,
                        user_id=g.user.id
                    )
                    db.session.add(category)
                    db.session.flush()

        else:

            abort(400)

        if file and allowed_file(file.filename):
            # 分离目录和文件名
            sourcename_l = os.path.split(file.filename)
            if len(sourcename_l) == 2:
                sourcename = sourcename_l[1]
            elif len(sourcename_l) == 1:
                sourcename = sourcename_l[0]
            else:
                abort(400)

            # 分离扩展名
            filename, ext = os.path.splitext(sourcename)
            filename = secure_filename(filename)

            if len(filename) > 500:
                db.session.rollback()
                return jsonify(status='error', msg='文件名过长')

            # 查询是否有重复文件
            script = Script.query.filter_by(name=filename, user_id=g.user.id). \
                outerjoin(Category).filter(Category.name == category_name).first()

            if not script:

                if check_script_sync(filename, g.user.id):

                    dist = os.path.join(app.config['UPLOAD_FOLDER'], md5(str(g.user.id)), category.name)
                    dist = dist.encode("utf-8")
                    if not os.path.exists(dist):
                        os.makedirs(dist)
                    # 检查是否有重名文件
                    # rename, path = rename_file(dist, filename)
                    path = os.path.join(dist, filename).encode('utf-8')
                    file.save(path)
                    m = hashlib.md5()
                    m.update(file.read())
                    md5_sec = m.hexdigest()
                    # 保存script
                    script = Script(
                        name=filename,
                        ext=ext[1:],
                        file=path,
                        user_id=g.user.id,
                        category_id=category.id,
                        md5=md5_sec
                    )
                    db.session.add(script)

            else:
                with open(script.file.encode('utf-8'), 'w') as file_:
                    file_.write(file.read())
                script.update_time = datetime.datetime.now()

            action_log.status = '200'
            db.session.add(action_log)
            db.session.commit()

        else:
            abort(400)

    except:
        traceback.print_exc()
        db.session.rollback()
        return jsonify(status='error', msg='保存文件失败,数据已回滚')

    return jsonify(status='success', msg='上传成功')