Exemplo n.º 1
0
def generate_id(username, path, name, id_life):
    from diskcloud.libs.mysql import select_execute, update_execute, db_commit, db_rollback
    from diskcloud.libs.string import generate_random_str
    from diskcloud.libs.time import strftime, now_time, now_time_str
    from datetime import timedelta
    from pathlib import Path

    # set expire time string
    if id_life != 0:
        expire_time = now_time() + timedelta(hours=id_life)
        expire_time_str = strftime(expire_time)
    else:
        expire_time_str = 'permanent'

    result_sel = select_execute(
        "select expire_time,sid from storage where username = %s and path = %s and name = %s and trash_can = %s",
        (username, path, name, 0))
    if result_sel[0][0] != None and result_sel[0][1] != None:
        result_life = valid_expire_time(result_sel[0][0])
        if result_life == 'permanent':
            return generate_id_return(True, result_sel[0][1])
        elif result_life:
            result_update = update_execute(
                'update storage set share_time = %s, expire_time = %s where username = %s and path = %s and name = %s and trash_can = %s',
                (now_time_str(), expire_time_str, username, path, name, 0))
            if result_update:
                db_commit()
                return generate_id_return(True, result_sel[0][1])
            db_rollback()
            return generate_id_return(False, 'Fail to update expired share.')
        else:
            sid = generate_random_str(8)
            result_update = update_execute(
                'update storage set share_time = %s, expire_time = %s, sid = %s where username = %s and path = %s and name = %s and trash_can = %s',
                (now_time_str(), expire_time_str, sid, username, path, name,
                 0))
            if result_update:
                db_commit()
                return generate_id_return(True, sid)
            db_rollback()
            return generate_id_return(False, 'Fail to generate a share id')
    sid = generate_random_str(8)
    result_update = update_execute(
        'update storage set share = %s, share_time = %s, expire_time = %s, sid = %s where username = %s and path = %s and name = %s and trash_can = %s',
        (1, now_time_str(), expire_time_str, sid, username, path, name, 0))
    if result_update:
        db_commit()
        return generate_id_return(True, sid)
    db_rollback()
    return generate_id_return(False, 'Fail to generate a share id')
Exemplo n.º 2
0
def untrash_can(username, path, name, is_file):
    from diskcloud.libs.mysql import select_execute, update_execute, db_commit, db_rollback
    from diskcloud.libs.response import gen_error_res
    from diskcloud.libs.time import now_time_str
    from pathlib import Path
    from flask import current_app
    from shutil import move

    def is_empty(generator):
        try:
            i = next(generator)
        except StopIteration:
            return True
        return False

    def walk(username, path, af_path):
        result = select_execute(
            'select name, type from storage where username = %s and path = %s and trash_can = %s',
            (username, path, 1))
        update_result = True
        for i in range(len(result)):
            update_result = update_result and update_execute(
                'update storage set path = %s, trash_can = %s, trash_can_time = %s where username = %s and path = %s and name = %s and trash_can = %s',
                (af_path, 0, now_time_str(), username, path, result[i][0], 1))
            if result[i][1] == 1:
                update_result = update_result and walk(
                    username,
                    Path(path, result[i][0]).as_posix(),
                    Path(af_path, result[i][0]).as_posix())
        return update_result

    af_name = generate_name(username, path, name, 0)
    result = update_execute(
        'update storage set name = %s, trash_can = %s, trash_can_time = %s where username = %s and path = %s and name = %s and trash_can = %s',
        (af_name, 0, None, username, path, name, 1))

    if not is_file:
        result = result and walk(username,
                                 Path(path, name).as_posix(),
                                 Path(path, af_name).as_posix())
    if result:
        bf_path = Path(current_app.config['FILES_FOLDER'], 'trash_can',
                       username, path, name)
        af_path = Path(current_app.config['FILES_FOLDER'], 'user', username,
                       path, af_name)
        try:
            move(bf_path.as_posix(), af_path.as_posix())
            if bf_path.parent.resolve() != Path(
                    current_app.config['FILES_FOLDER'], 'trash_can',
                    username).resolve() and is_empty(bf_path.parent.iterdir()):
                bf_path.parent.rmdir()
        except:
            db_rollback()
            return gen_error_res('无法还原,原文件夹可能更名或移动了位置', 500)
        db_commit()
        return ('', 200)
    db_rollback()
    return gen_error_res('更新数据库失败', 500)
Exemplo n.º 3
0
def set_cookie_id(username):
    import secrets
    from diskcloud.libs.mysql import update_execute, db_commit, db_rollback

    cookie_id = secrets.token_hex(32)
    result = update_execute("update user set cookie_id = %s where username = %s", (cookie_id, username))
    if result is True:
        db_commit()
        return cookie_id
    db_rollback()
    return False
Exemplo n.º 4
0
def unshare(username, path, name):
    from diskcloud.libs.mysql import update_execute, db_commit, db_rollback
    from diskcloud.libs.response import gen_error_res

    result = update_execute(
        'update storage set share = %s, share_time = %s, expire_time = %s, sid = %s where username = %s and path = %s and name = %s and trash_can = %s',
        (0, None, None, None, username, path, name, 0))
    if result:
        db_commit()
        return ('', 200)
    db_rollback()
    return gen_error_res('更新数据库失败', 500)
Exemplo n.º 5
0
def star(username, path, name):
    from diskcloud.libs.mysql import update_execute, db_commit, db_rollback
    from diskcloud.libs.response import gen_error_res
    from diskcloud.libs.time import now_time_str

    result = update_execute(
        'update storage set star = %s, star_time = %s where username = %s and path = %s and name = %s and trash_can = %s',
        (1, now_time_str(), username, path, name, 0))
    if result:
        db_commit()
        return ('', 200)
    db_rollback()
    return gen_error_res('更新数据库失败', 500)
Exemplo n.º 6
0
 def walk(username, path, af_path):
     result = select_execute(
         'select name, type from storage where username = %s and path = %s and trash_can = %s',
         (username, path, 1))
     update_result = True
     for i in range(len(result)):
         update_result = update_result and update_execute(
             'update storage set path = %s, trash_can = %s, trash_can_time = %s where username = %s and path = %s and name = %s and trash_can = %s',
             (af_path, 0, now_time_str(), username, path, result[i][0], 1))
         if result[i][1] == 1:
             update_result = update_result and walk(
                 username,
                 Path(path, result[i][0]).as_posix(),
                 Path(af_path, result[i][0]).as_posix())
     return update_result
Exemplo n.º 7
0
def moveto(username, bf_path, bf_name, af_path, af_name, is_file):
    from pathlib import Path
    from flask import current_app
    from diskcloud.libs.response import gen_error_res
    from diskcloud.libs.mysql import update_execute, db_commit, db_rollback

    def walk(username, path, af_path):
        result = select_execute(
            'select name, type from storage where username = %s and path = %s and trash_can = %s',
            (username, path, 0))
        update_result = True
        for i in range(len(result)):
            update_result = update_result and update_execute(
                'update storage set path = %s where username = %s and path = %s and name = %s and trash_can = %s',
                (af_path, username, path, result[i][0], 0))
            if result[i][1] == 1:
                update_result = update_result and walk(
                    username,
                    Path(path, result[i][0]).as_posix(),
                    Path(af_path, result[i][0]).as_posix())
        return update_result

    af_path = Path(af_path, af_name).as_posix()
    af_name = generate_name(username, af_path, bf_name, 0)
    result = True
    if not is_file:
        result = walk(username,
                      Path(bf_path, bf_name).as_posix(),
                      Path(af_path, af_name).as_posix())
    if result and update_execute(
            'update storage set path = %s, name = %s where username = %s and path = %s and name = %s and trash_can = %s',
        (af_path, af_name, username, bf_path, bf_name, 0)):
        bf_path = Path(current_app.config['FILES_FOLDER'], 'user', username,
                       bf_path, bf_name)
        af_path = Path(current_app.config['FILES_FOLDER'], 'user', username,
                       af_path, af_name)
        try:
            bf_path.rename(af_path)
        except:
            db_rollback()
            return gen_error_res('移动失败', 500)
        db_commit()
        return ('', 200)
    else:
        return gen_error_res('更新数据库失败', 500)
Exemplo n.º 8
0
 def walk(username, bf_path, af_path):
     result = select_execute(
         'select name, type from storage where username = %s and path = %s and trash_can = %s',
         (username, bf_path.as_posix(), 0))
     update_result = True
     for i in range(len(result)):
         af_name = generate_name(username, af_path.as_posix(), result[i][0],
                                 1)
         folder_path = Path(current_app.config['FILES_FOLDER'], 'user',
                            username, bf_path)
         update_result = update_result and update_execute(
             'update storage set path = %s, name = %s, trash_can = %s, trash_can_time = %s where username = %s and path = %s and name = %s and trash_can = %s',
             (af_path.as_posix(), af_name, 1, now_time_str(), username,
              bf_path.as_posix(), result[i][0], 0))
         if af_name != result[i][0]:
             Path(folder_path,
                  result[i][0]).rename(Path(folder_path, af_name))
         if result[i][1] == 1:
             update_result = update_result and walk(
                 username, Path(bf_path, result[i][0]),
                 Path(af_path, af_name))
     return update_result
Exemplo n.º 9
0
def add_user(username, password, email, force):
    from diskcloud.libs.mysql import select_execute, update_execute, insert_execute, delete_execute, db_commit, db_rollback
    from diskcloud.libs.string import hash_sha3
    from diskcloud.libs.valid import valid_username, valid_password
    from shutil import rmtree
    from os import makedirs
    from pathlib import Path
    from flask import current_app
    from time import sleep

    if not valid_username(username):
        click.echo("invalid username!")
        return False
    elif not valid_password(password):
        click.echo("invalid password!")
        return False
    password = hash_sha3(password)
    user_path = Path(current_app.config['FILES_FOLDER'], 'user',
                     username).as_posix()
    trash_can_path = Path(current_app.config['FILES_FOLDER'], 'trash_can',
                          username).as_posix()
    tar_path = Path(current_app.config['FILES_FOLDER'], 'tar',
                    username).as_posix()
    result = select_execute('select password from user where username = %s',
                            (username, ))
    if len(result) != 0:
        if not force:
            click.echo(
                "the username already exist,if you want to delete it,please add '--force' to enforce."
            )
            return False
        else:
            try:
                rmtree(user_path)
                rmtree(trash_can_path)
                rmtree(tar_path)
                sleep(2)
                makedirs(user_path)
                makedirs(trash_can_path)
                makedirs(tar_path)
            except:
                raise
            delete_execute('delete from storage where username = %s',
                           (username, ))
            result = update_execute(
                'update user set password = %s where username = %s',
                (password, username))
    else:
        try:
            makedirs(user_path)
            makedirs(trash_can_path)
            makedirs(tar_path)
        except:
            raise
        result = insert_execute(
            'insert into user(username, password, email) values(%s, %s, %s)',
            (username, password, email))
    if result:
        db_commit()
        click.echo("create user success!")
        return True
    db_rollback()
    click.echo("create user fail!")
    return False
Exemplo n.º 10
0
def trash_can(username, path, name, is_file):
    from diskcloud.libs.mysql import select_execute, update_execute, db_commit, db_rollback
    from diskcloud.libs.response import gen_error_res
    from diskcloud.libs.time import now_time_str
    from pathlib import Path
    from flask import current_app
    from shutil import move

    def walk(username, bf_path, af_path):
        result = select_execute(
            'select name, type from storage where username = %s and path = %s and trash_can = %s',
            (username, bf_path.as_posix(), 0))
        update_result = True
        for i in range(len(result)):
            af_name = generate_name(username, af_path.as_posix(), result[i][0],
                                    1)
            folder_path = Path(current_app.config['FILES_FOLDER'], 'user',
                               username, bf_path)
            update_result = update_result and update_execute(
                'update storage set path = %s, name = %s, trash_can = %s, trash_can_time = %s where username = %s and path = %s and name = %s and trash_can = %s',
                (af_path.as_posix(), af_name, 1, now_time_str(), username,
                 bf_path.as_posix(), result[i][0], 0))
            if af_name != result[i][0]:
                Path(folder_path,
                     result[i][0]).rename(Path(folder_path, af_name))
            if result[i][1] == 1:
                update_result = update_result and walk(
                    username, Path(bf_path, result[i][0]),
                    Path(af_path, af_name))
        return update_result

    af_name = generate_name(username, path, name, 1)
    bf_path = Path(current_app.config['FILES_FOLDER'], 'user', username, path,
                   name)
    af_path = Path(current_app.config['FILES_FOLDER'], 'trash_can', username,
                   path, af_name)
    result = update_execute(
        'update storage set name = %s, trash_can = %s, trash_can_time = %s where username = %s and path = %s and name = %s and trash_can = %s',
        (af_name, 1, now_time_str(), username, path, name, 0))

    try:
        if is_file:
            af_path.parent.mkdir(parents=True, exist_ok=True)
            move(bf_path.as_posix(), af_path.as_posix())
        else:
            result = result and walk(username, Path(path, name),
                                     Path(path, af_name))

            if af_path.is_dir():
                for child in bf_path.iterdir():
                    move(child.as_posix(), af_path.as_posix())
                bf_path.rmdir()
            elif not af_path.exists():
                move(bf_path.as_posix(), af_path.as_posix())
    except:
        db_rollback()
        return gen_error_res('移动失败', 500)

    if result:
        db_commit()
        return ('', 200)
    else:
        db_rollback()
        return gen_error_res('更新数据库失败', 500)