Пример #1
0
def create_folder(username, path, name, foldername):
    from pathlib import Path
    from os import mkdir
    from flask import current_app
    from diskcloud.libs.response import gen_error_res
    from diskcloud.libs.time import now_time_str
    from diskcloud.libs.mysql import insert_execute, db_commit, db_rollback

    path = Path(path, name).as_posix()
    foldername = generate_name(username, path, foldername, 0)
    if insert_execute(
            'insert into storage(username, path, name, modify_time, type) values(%s, %s, %s, %s, %s)',
        (username, path, foldername, now_time_str(), 1)):
        path = Path(current_app.config['FILES_FOLDER'], 'user', username, path,
                    foldername).as_posix()
        try:
            mkdir(path)
        except:
            db_rollback()
            return gen_error_res('fail to create folder', 500)
        db_commit()
        return ('', 200)
    else:
        db_rollback()
        return gen_error_res('向数据库内插入数据失败', 500)
Пример #2
0
def create_file(username, path, name, filename):
    from pathlib import Path
    from flask import current_app
    from diskcloud.libs.response import gen_error_res
    from diskcloud.libs.time import now_time_str
    from diskcloud.libs.mysql import insert_execute, db_commit, db_rollback

    path = Path(path, name).as_posix()
    filename = generate_name(username, path, filename, 0)
    if insert_execute(
            'insert into storage(username, path, name, size, modify_time, type) values(%s, %s, %s, %s, %s, %s)',
        (username, path, filename, 0, now_time_str(), 0)):
        try:
            path = Path(current_app.config['FILES_FOLDER'], 'user', username,
                        path, filename).as_posix()
            file = open(path, 'a+')
            file.close()
        except:
            db_rollback()
            return gen_error_res('创建文件失败', 500)
        db_commit()
        return ('', 200)
    else:
        db_rollback()
        return gen_error_res('向数据库内插入数据失败', 500)
Пример #3
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)
Пример #4
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
Пример #5
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)
Пример #6
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)
Пример #7
0
    def Register(self):
        from os import mkdir
        from pathlib import Path
        from diskcloud.libs.string import hash_sha3
        from diskcloud.libs.session import create_session
        from diskcloud.libs.mysql import select_execute, insert_execute, db_commit, db_rollback

        if current_app.config['CAN_REGISTER']:
            username = request.json.get('username')
            password = request.json.get('password')
            email = request.json.get('email')
            pw_hashed = hash_sha3(password)

            result = select_execute(
                'select password from user where username = %s', (username, ))
            if len(result) == 0:
                result = select_execute(
                    'select password from user where email = %s', (email, ))
                if len(result) == 0:
                    result = insert_execute(
                        'insert into user(username, password, email) values(%s, %s, %s)',
                        (username, pw_hashed, email))
                    if result:
                        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()
                        try:
                            mkdir(user_path)
                            mkdir(trash_can_path)
                            mkdir(tar_path)
                        except FileExistsError:
                            pass
                        except:
                            raise
                        db_commit()
                        create_session('username', username)
                        return ('', 200)
                    else:
                        db_rollback()
                        return gen_error_res('数据库插入数据失败', 500)
                return gen_error_res('该email已被使用', 403)
            return gen_error_res('该用户名已被使用', 403)
        return gen_error_res('注册功能暂不开放', 403)
Пример #8
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)
Пример #9
0
def empty_trash_can(username):
    from diskcloud.libs.mysql import delete_execute, db_commit, db_rollback
    from diskcloud.libs.response import gen_error_res
    from pathlib import Path
    from flask import current_app

    def delete_file(path):
        for i in path.iterdir():
            if i.is_file():
                i.unlink()
            elif i.is_dir():
                delete_file(i)

    def delete_folder(path):
        try:
            next(path.iterdir())
        except StopIteration:
            path.rmdir()
            return
        for i in path.iterdir():
            if i.is_dir():
                delete_folder(i)
        path.rmdir()

    def empty(path):
        delete_file(path)
        delete_folder(path)
        path.mkdir()

    if delete_execute(
            'delete from storage where username = %s and trash_can = %s',
        (username, 1)):
        trash_can_path = Path(current_app.config['FILES_FOLDER'], 'trash_can',
                              username)
        try:
            empty(trash_can_path)
        except:
            db_rollback()
            return gen_error_res('清空回收站失败', 500)
        db_commit()
        return ('', 200)
    else:
        db_rollback()
        return gen_error_res('更新数据库失败', 500)
Пример #10
0
def remove_user(username):
    from diskcloud.libs.mysql import select_execute, delete_execute, db_commit, db_rollback
    from diskcloud.libs.valid import valid_username
    from shutil import rmtree
    from pathlib import Path
    from flask import current_app

    if not valid_username(username):
        click.echo("invalid username!")
        return False
    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:
        try:
            rmtree(user_path)
            rmtree(trash_can_path)
            rmtree(tar_path)
        except FileNotFoundError:
            pass
        except:
            raise
        delete_execute('delete from storage where username = %s', (username, ))
        result = delete_execute('delete from user where username = %s',
                                (username, ))
    else:
        click.echo("this user is not exist!")
        return False
    if result:
        db_commit()
        click.echo("remove user success!")
        return True
    db_rollback()
    click.echo("remove user fail!")
    return False
Пример #11
0
def save_file(username, path, name, file):
    from pathlib import Path
    from flask import current_app
    from diskcloud.libs.response import gen_error_res
    from diskcloud.libs.time import now_time_str
    from diskcloud.libs.mysql import insert_execute, db_commit, db_rollback

    path = Path(path, name).as_posix()
    filename = generate_name(username, path, file.filename, 0)
    whole_path = Path(current_app.config['FILES_FOLDER'], 'user', username,
                      path, filename)
    whole_path_str = whole_path.as_posix()
    try:
        file.save(whole_path_str)
    except:
        return gen_error_res('保存已上传的文件失败', 500)
    if insert_execute(
            'insert into storage(username, path, name, size, modify_time, type) values(%s, %s, %s, %s, %s, %s)',
        (username, path, filename, whole_path.stat().st_size, now_time_str(),
         0)):
        db_commit()
        return True
    return gen_error_res('向数据库内插入数据失败', 500)
Пример #12
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')
Пример #13
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
Пример #14
0
def delete(username, path, name, is_file):
    from pathlib import Path
    from shutil import rmtree
    from os import remove
    from flask import current_app
    from diskcloud.libs.response import gen_error_res
    from diskcloud.libs.mysql import select_execute, delete_execute, db_commit, db_rollback

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

    def delete_db(username, path, name):
        return delete_execute(
            'delete from storage where username = %s and path = %s and name = %s and trash_can = %s',
            (username, path, name, 1))

    def delete_folder_db(username, path, name):
        if delete_db(username, path, name):
            path = Path(path, name).as_posix()
            result = select_execute(
                'select name, type from storage where username = %s and path = %s and trash_can = %s',
                (username, path, 1))
            for i in range(len(result)):
                if result[i][1] == 0:
                    if delete_db(username, path, result[i][0]) is False:
                        return False
                else:
                    if delete_folder_db(username, path, result[i][0]) is False:
                        return False
            return True
        return False

    def in_trash_can(username, path, name):
        result = select_execute(
            'select name from storage where username = %s and path = %s and name = %s and trash_can = %s',
            (username, path, name, 1))
        if len(result) != 0:
            return True
        return False

    if in_trash_can(username, path, name):
        if is_file:
            if delete_db(username, path, name):
                path = Path(current_app.config['FILES_FOLDER'], 'trash_can',
                            username, path, name)
                try:
                    remove(path.as_posix())
                    if path.parent.resolve() != Path(
                            current_app.config['FILES_FOLDER'], 'trash_can',
                            username).resolve() and is_empty(
                                path.parent.iterdir()):
                        path.parent.rmdir()
                except:
                    db_rollback()
                    return gen_error_res('删除失败', 500)
                db_commit()
                return ('', 200)
            else:
                return gen_error_res('更新数据库失败', 500)
        else:
            if delete_folder_db(username, path, name):
                path = Path(current_app.config['FILES_FOLDER'], 'trash_can',
                            username, path, name).as_posix()
                try:
                    rmtree(path)
                except:
                    db_rollback()
                    return gen_error_res('删除失败', 500)
                db_commit()
                return ('', 200)
            else:
                db_rollback()
                return gen_error_res('更新数据库失败', 500)
    return gen_error_res("不能删除,因为该文件或文件夹不在回收站中")
Пример #15
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)