示例#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')
示例#2
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)
示例#3
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)
示例#4
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)
示例#5
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
示例#6
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
示例#7
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)
示例#8
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)