Ejemplo n.º 1
0
    def __rm_data_item(self, user_id, user_ref_id, default_pan_id, item_id):
        if not default_pan_id:
            pan_acc = auth_service.default_pan_account(user_id)
            if pan_acc:
                default_pan_id = pan_acc.id
        if not default_pan_id:
            return {"state": -2, "err": LOGIC_ERR_TXT['need_pann_acc']}

        _client_data_item: ClientDataItem = ClientDataDao.get_data_item_by_id(
            item_id, user_ref_id)
        if _client_data_item:
            pan_acc: PanAccounts = auth_service.get_pan_account(
                default_pan_id, user_id)
            if not pan_acc:
                logger.error("PanAccount not exists![{}],user_id:{}".format(
                    default_pan_id, user_id))
                return {"state": -2, "err": LOGIC_ERR_TXT['need_pann_acc']}
            jsonrs = restapi.del_file(pan_acc.access_token,
                                      _client_data_item.path)
            if "errno" in jsonrs and jsonrs["errno"]:
                errmsg = jsonrs.get("errmsg", "")
                if not errmsg:
                    errmsg = "clear failed!"
                return {"state": -1, "errmsg": errmsg}
            else:
                ClientDataDao.del_data_item_by_id(_client_data_item.id)
                return {"state": 0}
        else:
            return {"state": -3, "err": LOGIC_ERR_TXT['not_exists']}
Ejemplo n.º 2
0
    def test_async_task(self, user_id, default_pan_id):
        import time
        import random
        key_prefix = "client:ready:"
        if not default_pan_id:
            pan_acc = auth_service.default_pan_account(user_id)
            if pan_acc:
                default_pan_id = pan_acc.id
        if not default_pan_id:
            return {"state": -2, "err": LOGIC_ERR_TXT['need_pann_acc']}

        def final_do():
            pass

        def to_do(key, rs_key):
            _result = {'state': 0}
            time.sleep(random.randint(1, 10))
            # 文件已迁出,等待迁入
            async_service.update_state(key_prefix, user_id, {
                "state": 0,
                "pos": 1
            })
            time.sleep(random.randint(1, 10))
            # 文件已迁出,等待迁入, 目录结构已存在
            async_service.update_state(key_prefix, user_id, {
                "state": 0,
                "pos": 2
            })
            rv = random.randint(1, 10)
            # print("rv:", rv)
            test_err = rv <= 6
            if test_err:
                _result['err'] = LOGIC_ERR_TXT['rename_fail']
                _result['state'] = -4
            else:
                time.sleep(random.randint(1, 10))
                # 迁出后, 获取信息
                async_service.update_state(key_prefix, user_id, {
                    "state": 0,
                    "pos": 3
                })
                _result['item'] = {}
                _result['item']['id'] = 'a'
                _result['pos'] = 4
            time.sleep(random.randint(1, 10))
            return _result

        result = {'state': 0, 'pos': 0}
        async_service.init_state(key_prefix, user_id, {"state": 0, "pos": 0})
        async_rs = async_service.async_checkout_client_item(
            key_prefix, user_id, to_do, final_do)
        if async_rs['state'] == 'block':
            result['state'] = -11
            result['err'] = LOGIC_ERR_TXT['sys_lvl_down']

        return result
Ejemplo n.º 3
0
    def get(self):
        path = self.request.path
        # print(path)
        if path.endswith("/list"):
            parent = self.get_argument("parent", default='55')
            item_list = DataDao.query_data_item_by_parent(int(parent), True)
            params = {"list": item_list}
            # print("params:", params)
            # for item in item_list:
            #     print(item.filename)
            self.render('list.html', **params)
        elif path.endswith("/fload"):
            source = self.get_argument("source", "")
            node_id = self.get_argument("id")
            # parent_path = self.get_argument("path")
            # if not parent_path.endswith("/"):
            #     parent_path = "%s/" % parent_path
            logger.info("fload node_id:{},source:{}".format(node_id, source))
            # parent_id = 55
            params = []
            if not '#' == node_id:
                # if "shared" == source:
                #     params = pan_service.query_shared_file_list(parent_id, self.request.user_id)
                if "assets" == source:
                    if 'assets_0' == node_id:
                        params = ProductDao.query_assets_by_ref_id_for_tree(
                            self.ref_id)
                elif "free" == source:
                    if 'free_0' == node_id:
                        params = pan_service.query_root_list()
                elif "self" == source:
                    if 'self_0' == node_id:
                        if not self.default_pan_id:
                            pan_acc = auth_service.default_pan_account(
                                self.user_id)
                            self.default_pan_id = pan_acc.id
                        if self.default_pan_id:
                            params = pan_service.query_client_root_list(
                                self.default_pan_id)
                    else:
                        node_id_val = decrypt_id(node_id)
                        parent_id = int(node_id_val)
                        params = pan_service.query_client_sub_list(
                            parent_id, self.ref_id)
                elif "empty" == source:
                    pass
                else:
                    node_id_val = decrypt_id(node_id)
                    parent_id = int(node_id_val)
                    params = pan_service.query_file_list(parent_id)
            else:
                # params = pan_service.query_root_list(self.request.user_id)
                params.append({
                    "id": "free_0",
                    "text": PAN_TREE_TXT['free_root'],
                    "data": {
                        "source": "free"
                    },
                    "children": True,
                    "icon": "folder"
                })
                params.append({
                    "id": "assets_0",
                    "text": PAN_TREE_TXT['buy_root'],
                    "data": {
                        "source": "assets"
                    },
                    "children": True,
                    "icon": "folder"
                })
                params.append({
                    "id": "self_0",
                    "text": PAN_TREE_TXT['self_root'],
                    "data": {
                        "source": "self"
                    },
                    "children": True,
                    "icon": "folder"
                })
                params.append({
                    "id": "empty_0",
                    "text": PAN_TREE_TXT['empty_root'],
                    "data": {
                        "source": "empty"
                    },
                    "children": False,
                    "icon": "file"
                })

            self.to_write_json(params)
        elif path.endswith("/search"):
            params = {}
            self.render('search.html', **params)
        elif path.endswith("/load"):
            kw = self.get_body_argument("kw")
            source = self.get_body_argument("source")
            print("kw:", kw)
            print("source:", source)
            kw = kw.replace(' ', '%')
            page = self.get_body_argument("page")
            size = 100
            offset = int(page) * size
            sp: SearchParams = SearchParams.build_params(offset, size)
            sp.add_must(value=kw)
            es_dao_fun = es_dao_local
            if source:
                sp.add_must(field='source', value=source)
                es_dao_fun = es_dao_share
                # es_dao_fun = es_dao_dir
            es_body = build_query_item_es_body(sp)
            print("es_body:", json.dumps(es_body))
            es_result = es_dao_fun().es_search_exec(es_body)
            hits_rs = es_result["hits"]
            total = hits_rs["total"]
            datas = [_s["_source"] for _s in hits_rs["hits"]]

            # print("es_result:", es_result)
            # item_list = DataDao.query_file_list_by_keyword(kw, offset=offset, limit=size)
            # objs = [object_to_dict(o, FIELDS) for o in item_list]
            # has_next = len(objs) == size
            has_next = offset + size < total
            rs = {"data": datas, "has_next": has_next}
            # print("rs:", rs)
            self.to_write_json(rs)
        elif path.endswith("/finfo"):
            # item_id = self.get_argument("id")
            item_fuzzy_id = self.get_argument("id")
            item_id = int(decrypt_id(item_fuzzy_id))
            params = pan_service.query_file(item_id)
            self.to_write_json(params)
        elif path.endswith("/readydownload"):
            fs_id = self.get_argument("fs_id")
            print("readydownload fs_id:", fs_id)
            params, share_log, data_item = pan_service.share_folder(fs_id)
            # sub_params = []
            min_size = 6000
            # min_size = 60
            if data_item.size > min_size:
                sub_params = pan_service.sub_account_transfer(share_log)
                result = {"subs": sub_params}
            else:
                result = {"master": params}
            # result = {"master": params, "subs": sub_params}
            self.to_write_json(result)
        elif path.endswith("/check_transfer"):
            transfer_log_id = self.get_argument("id")
            rs = {}
            print("transfer_log_id:", transfer_log_id)
            if transfer_log_id:
                t = pan_service.recheck_transfer_d_link(int(transfer_log_id))
                if t:
                    rs = t
            self.to_write_json(rs)
        elif path.endswith("/check_shared_log"):
            shared_log_id = self.get_argument("id")
            rs = {}
            print("shared_log_id:", shared_log_id)
            if shared_log_id:
                t = pan_service.recheck_shared_d_link(int(shared_log_id))
                if t:
                    rs = t
            self.to_write_json(rs)
        elif path.endswith("/sync_used"):
            pan_account_ids_str = self.get_argument("ids")
            used_str = self.get_argument("useds")
            if pan_account_ids_str and used_str:
                _ids = pan_account_ids_str.split(",")
                useds = used_str.split(",")
                params = []
                ul = len(useds)
                for i in range(len(_ids)):
                    _id = _ids[i]
                    if i < ul:
                        used = useds[i]
                        params.append({'id': int(_id), 'used': int(used)})

                if params:
                    DataDao.update_pan_account_used(params)

            self.to_write_json({})
        elif path.endswith("/dlink"):
            item_id = self.get_argument("id")
            params = pan_service.query_file(item_id)
            self.render('dlink.html', **params)
        elif path.endswith("/manage"):
            pan_id = self.get_argument("panid", "0")
            params = {'pan_id': pan_id}
            self.render('ftree.html', **params)
        elif path.endswith("/helptokens"):
            res = pan_service.pan_accounts_dict()
            self.to_write_json(res)
        elif path.endswith("/syncallnodes"):
            item_fuzzy_id = self.get_argument("id", None)
            item_id = int(decrypt_id(item_fuzzy_id))
            pan_id = self.get_argument('panid', "0")
            logger.info("syncallnodes pan_id:{}".format(pan_id))
            pan_id = int(pan_id)
            recursion = self.get_argument("recursion")
            if recursion == "1":
                recursion = True
            else:
                recursion = False
            if not item_id:
                if pan_id:
                    root_item: DataItem = sync_pan_service.fetch_root_item(
                        pan_id)
                    logger.info('root_item:{}'.format(
                        DataItem.to_dict(root_item)))
                    if root_item:
                        item_id = root_item.id
                    else:
                        item_id = sync_pan_service.new_root_item(
                            self.request.user_id, pan_id)
                else:
                    item_id = 55
            item_id = int(item_id)
            rs = sync_pan_service.sync_from_root(item_id, recursion, pan_id,
                                                 self.request.user_id)
            self.to_write_json(rs)
        elif path.endswith("/synccommunity"):
            # print("in...:")
            bd = self.request.body
            data_obj = json.loads(bd)
            print('/synccommunity payload:', self.request.user_id)
            open_service.sync_community_item_to_es(self.request.user_id,
                                                   data_obj)
            self.to_write_json({'state': 0})
            pass
        elif path.endswith("/syncstate"):
            self.release_db = False
            pan_id = self.get_argument('panid', "0")
            dir_item_id = sync_pan_service.check_sync_state(
                pan_id, self.request.user_id)
            if dir_item_id:
                self.to_write_json({'state': 1, 'item': dir_item_id})
            else:
                self.to_write_json({'state': 0})
Ejemplo n.º 4
0
    def check_transfer_file(self, user_id, user_ref_id, default_pan_id,
                            item_id, pids, tag):
        ctx = self
        key_prefix = "client:ready:"
        if not default_pan_id:
            pan_acc = auth_service.default_pan_account(user_id)
            if pan_acc:
                default_pan_id = pan_acc.id
        if not default_pan_id:
            return {"state": -2, "err": LOGIC_ERR_TXT['need_pann_acc']}

        def final_do():
            pass

        def to_do(key, rs_key):
            _result = {'state': 0}
            data_item: DataItem = DataDao.get_data_item_by_id(item_id)
            _rs, share_log = open_service.build_shared_log(data_item)
            if not share_log:
                if 'state' in _rs:
                    _result['state'] = _rs['state']
                if 'err' in _rs:
                    _result['state'] = -9
                    _result['err'] = _rs['err']
            else:
                if share_log.is_black == 1:
                    _result['state'] = -9
                    _result['err'] = share_log.err
                else:
                    _result['item'] = {
                        "link": share_log.link,
                        "pass": share_log.password
                    }
                    _result['pos'] = 1
            # copy

            return _result

        st = self.check_file_authorized(user_ref_id, item_id, pids, tag)
        result = {'state': 0}
        if st == 0:
            if "self" == tag:
                # 找不到文件 search
                return {"state": -5, "err": LOGIC_ERR_TXT['not_exists']}
            else:
                async_service.init_state(key_prefix, user_id, {
                    "state": 0,
                    "pos": 0
                })
                async_rs = async_service.async_checkout_client_item(
                    key_prefix, user_id, to_do, final_do)
                if async_rs['state'] == 'block':
                    result['state'] = -11
                    result['err'] = LOGIC_ERR_TXT['sys_lvl_down']
        else:
            err_msg = LOGIC_ERR_TXT['unknown']
            if -10 == st:
                err_msg = LOGIC_ERR_TXT['ill_data']
            elif -2 == st:
                err_msg = LOGIC_ERR_TXT['need_access']
            result = {"state": st, "err": err_msg}
        return result
Ejemplo n.º 5
0
    def check_copy_file(self, user_id, user_ref_id, default_pan_id, item_id,
                        pids, tag):
        ctx = self
        key_prefix = "client:ready:"
        if not default_pan_id:
            pan_acc = auth_service.default_pan_account(user_id)
            if pan_acc:
                default_pan_id = pan_acc.id
        if not default_pan_id:
            return {"state": -2, "err": LOGIC_ERR_TXT['need_pann_acc']}

        def final_do():
            pass

        def to_do(key, rs_key):
            _result = {'state': 0}
            data_item: DataItem = DataDao.get_data_item_by_id(item_id)
            _client_data_item: ClientDataItem = ClientDataDao.get_data_item_by_source_fs_id(
                data_item.fs_id, user_ref_id)
            if _client_data_item:
                if _client_data_item.pin == 0:
                    pan_acc: PanAccounts = auth_service.get_pan_account(
                        default_pan_id, user_id)
                    pan_acc = auth_service.check_pan_token_validation(pan_acc)
                    # 文件已迁出,等待迁入, 目录结构已存在
                    async_service.update_state(key_prefix, user_id, {
                        "state": 0,
                        "pos": 2
                    })
                    old_name = _client_data_item.filename
                    ftype = guess_file_type(old_name)
                    if not ftype:
                        idx = old_name.rfind('.')
                        if idx > 0:
                            ftype = old_name[idx + 1:]
                    new_name = "%s.%s" % (_client_data_item.source_fs_id,
                                          ftype)
                    parent_dir = "/%s" % PAN_ROOT_DIR['name']
                    new_path = "%s/%s" % (parent_dir, new_name)
                    _jsonrs = restapi.file_rename(pan_acc.access_token,
                                                  _client_data_item.path,
                                                  new_name)
                    # jsonrs = restapi.file_search(pan_acc.access_token, key=fs_id, parent_dir=parent_dir)
                    # print("search new file jsonrs:", jsonrs)
                    if "errno" in _jsonrs and _jsonrs["errno"] == 0:
                        _client_data_item.path = new_path
                        _client_data_item.filename = new_name
                        _client_data_item.pin = 1
                        ClientDataDao.update_client_item(
                            _client_data_item.id, {
                                "path": new_path,
                                "filename": new_name,
                                "pin": 1
                            })
                        _result['state'] = 0
                        _result['item'] = ctx.__build_client_item_simple_dict(
                            _client_data_item)
                        _result['item']['id'] = obfuscate_id(
                            _client_data_item.id)
                        _result['pos'] = 4
                    else:
                        _result['state'] = -4
                        _result["err"] = LOGIC_ERR_TXT['rename_fail']
                else:
                    _result['state'] = 0
                    _result['item'] = ctx.__build_client_item_simple_dict(
                        _client_data_item)
                    _result['item']['id'] = obfuscate_id(_client_data_item.id)
                    _result['pos'] = 4
            else:
                _rs, share_log = open_service.build_shared_log(data_item)
                if not share_log:
                    if 'state' in _rs:
                        _result['state'] = _rs['state']
                    if 'err' in _rs:
                        _result['state'] = -9
                        _result['err'] = _rs['err']
                else:
                    # copy
                    if share_log.is_black == 1:
                        _result['state'] = -9
                        _result['err'] = share_log.err
                    else:
                        _st, _client_data_item = self.copy_to_my_pan(
                            user_id, user_ref_id, share_log, default_pan_id)
                        _result['state'] = _st
                        if _st < 0:
                            if _st == -3:
                                _result['err'] = LOGIC_ERR_TXT['mk_top_fail']
                            elif _st == -4:
                                _result['err'] = LOGIC_ERR_TXT['rename_fail']
                        if _client_data_item:
                            _result[
                                'item'] = ctx.__build_client_item_simple_dict(
                                    _client_data_item)
                            _result['item']['id'] = obfuscate_id(
                                _client_data_item.id)
                            _result['pos'] = 4
            return _result

        st = self.check_file_authorized(user_ref_id, item_id, pids, tag)
        result = {'state': 0}
        if st == 0:
            if "self" == tag:
                client_data_item: ClientDataItem = ClientDataDao.get_data_item_by_id(
                    item_id, user_ref_id)
                if client_data_item:
                    result['state'] = 0
                    result['item'] = ctx.__build_client_item_simple_dict(
                        client_data_item)
                    result['item']['id'] = obfuscate_id(client_data_item.id)
                    result['pos'] = 4
                    return result
                else:
                    # 找不到文件 search
                    return {"state": -5, "err": LOGIC_ERR_TXT['not_exists']}
            else:
                async_service.init_state(key_prefix, user_id, {
                    "state": 0,
                    "pos": 0
                })
                async_rs = async_service.async_checkout_client_item(
                    key_prefix, user_id, to_do, final_do)
                if async_rs['state'] == 'block':
                    result['state'] = -11
                    result['err'] = LOGIC_ERR_TXT['sys_lvl_down']
        else:
            err_msg = LOGIC_ERR_TXT['unknown']
            if -10 == st:
                err_msg = LOGIC_ERR_TXT['ill_data']
            elif -2 == st:
                err_msg = LOGIC_ERR_TXT['need_access']
            result = {"state": st, "err": err_msg}

        return result