Ejemplo n.º 1
0
def upload():
    """
    upload file and save file in local disk.
    http-request's form should have [file] part
    :return:
    """
    if request.files:
        try:
            file_name = request.files['file'].filename
            #无需考虑文件名重复的问题
            hex = secure_name(file_name)
            cate = auto_category(hex)
            file.save(request.files['file'], name=hex)

            try:
                f = File(name=file_name, hex=hex, category=cate)
                db.session.add(f)
                db.session.commit()

            except OperationalError:
                db.session.rollback()
                return http_response(250, 'bad', 'database not exist')

            return http_response(200, 'ok', 'file uploaded')

        except UploadNotAllowed:
            http_response(250, 'bad', 'file ext not allowed')

    return http_response(250, 'bad', 'file upload filed')
Ejemplo n.º 2
0
def file_update():
    """
    update file's name,category, hex name is unique but name(origin name) can be same
    :return:
    """
    if request.json:
        try:
            hex_name = request.json.get('hex_name')
            category = request.json.get('category', 'default')
            name = request.json.get('name')
            #对all分类去重
            if category == 'all':
                category = 'default'

            f = File.query.filter_by(hex=hex_name).first()
            if f and name and category:
                f.name = name
                f.category = category
                db.session.commit()
                return http_response(200, 'ok', 'file info updated')
            else:
                return http_response(250, 'bad', 'file not exists')

        except:
            return http_response(250, 'bad', 'update file info failed')
    else:
        return http_response(250, 'bad', 'json data required')
Ejemplo n.º 3
0
def cancel_account():
    """
    user can cancel their account, if get this api their token will be added into blacklist.
    once token has been added into blacklist, user's account will be deleted from database
    :return:
    """
    if request.json:
        try:
            #添加用户的去重
            name = request.json.get('name',None)
            get_user = User.query.filter_by(name=name).first()
            if not get_user:
                return  http_response(250, 'bad', 'user not exist')
            try:
                current_user = get_jwt_identity()
                if current_user == get_user.name:
                    try:
                        db.session.delete(get_user)
                        db.session.commit()
                    except:
                        db.session.rollback()
                    jti = get_raw_jwt()['jti']
                    blacklist.add(jti)

            except Exception:
                return http_response(250, 'bad', 'user cancel failed')
            # pass
        except Exception as e:
            print(e.args)
            return http_response(250, 'bad', 'user cancel failed')

        return http_response(460,'ok','user canceled, thanks for using pan')

    else:
        return http_response(250, 'bad', 'json data required')
Ejemplo n.º 4
0
def file_delete():
    """
    delete file by it's hex name
    if exists, also remove from disk
    :return:
    """
    if request.json:
        try:
            name = request.json['name']
            f = File.query.filter_by(hex=name).first()
            if f:
                try:
                    path = file.path(f.hex)
                    os.remove(path)
                    db.session.delete(f)
                    db.session.commit()
                except:
                    db.session.rollback()
                    return http_response(250, 'bad', 'file delete filed')

                return http_response(200, 'ok', 'file deleted')
            else:
                return http_response(250, 'bad', 'file not exists')

        except Exception as e:
            print(e.args)
            return http_response(250, 'bad', 'get file info failed')
    else:
        return http_response(250, 'bad', 'json data required')
Ejemplo n.º 5
0
def file_info():
    """
    get one file's info which contains [name,hex_name,url]
    when FILE_ACCESS is True, url is generated by FILE_SERVER_URI
    it's a sample about nginx server, see file_download for more
    :return:
    """
    try:
        name = request.args.get('name')
        f = File.query.filter_by(hex=name).first()
        if f:
            url = file.url(f.hex) if not current_app.config["FILE_ACCESS"] \
                else current_app.config["FILE_SERVER_URI"] +'/download?file=' + f.hex

            fdict = {
                "name": f.name,
                "time": f.time,
                "hex": f.hex,
                "url": url,
                "category": f.category
            }
            return http_response(200,'ok',fdict)
        else:
            return http_response(250, 'bad', 'file not exists')

    except Exception as e:
        print(e.args)
        return http_response(250,'bad','get file info failed')
Ejemplo n.º 6
0
def logout():
    try:
        # current_user = get_jwt_identity()
        jti = get_raw_jwt()['jti']
        blacklist.add(jti)

    except:
        return http_response(250, 'bad', 'user logout failed')

    return http_response(460, 'ok', 'user logout, thanks for using pan')
Ejemplo n.º 7
0
def login():
    if request.json:
        name = request.json.get('name', None)
        password = request.json.get('password', None)
        try:
            u = User.query.filter_by(name=name).first()
            if u and u.password == password:
                token = create_access_token(identity=name)
                return http_response(200, 'ok', token)
            else:
                return http_response(250, 'bad', 'user illegal')
        except:
            return http_response(500, 'bad', 'login failed')

    return http_response(250, 'bad', 'json data required')
Ejemplo n.º 8
0
def file_search():
    """
    search file in database
    :return:
    """
    try:
        word = request.args.get('word')
        fl = File.query.filter(File.name.contains(word)).all()
        if fl:
            flist = [f.info() for f in fl]
            return http_response(200,'ok',flist)

        return http_response(250,'bad','empty search results')

    except Exception as e:
        print(e.args)
        return http_response(250,'bad','get file-list failed')
Ejemplo n.º 9
0
def file_list():
    """
    return all file list
    :return:
    """
    cate = request.args.get('cate')
    if cate == 'all' or not cate:
        try:
            fl = File.query.all()
            flist = [f.info() for f in fl]
            return http_response(200, 'ok', flist)

        except Exception as e:
            print(e.args)
            return http_response(250, 'bad', 'get file_list failed')
    else:
        try:
            fl = File.query.filter_by(category=cate).all()
            flist = [f.info() for f in fl]
            return http_response(200, 'ok', flist)

        except Exception:
            return http_response(250, 'bad', 'get file_list failed')
Ejemplo n.º 10
0
def sign_up():
    if request.json:
        try:
            #添加用户的去重
            name = request.json.get('name', 'test')
            password = request.json.get('password', '123456')
            get_user = User.query.filter_by(name=name).first()
            if get_user:
                return http_response(250, 'bad', 'user already exist')
            try:
                u = User(name=name, password=password)
                db.session.add(u)
                db.session.commit()
            except:
                db.session.rollback()
                return http_response(250, 'bad', 'user added failed')
            # pass
        except:
            return http_response(250, 'bad', 'user added failed')

        return http_response(200, 'ok', 'user added success')

    else:
        return http_response(250, 'bad', 'json data required')
Ejemplo n.º 11
0
def db_del():
    db.drop_all()
    return http_response(200,'ok','database deleted')
Ejemplo n.º 12
0
def signup(request):
    handler = RequestHandler()
    result = handler.handle_user_signup(request)
    return response.http_response(201, result)
Ejemplo n.º 13
0
def notify_users(request):
    handler = RequestHandler()
    result = handler.handle_notifying_users(request)
    return response.http_response(200, result)
Ejemplo n.º 14
0
def signin(request):
    handler = RequestHandler()
    result = handler.handle_user_sigin(request)
    return response.http_response(200, transformers.user_transformer(result))
Ejemplo n.º 15
0
def db_init():
    db.create_all()
    return http_response(200, 'ok', 'database init')