Esempio n. 1
0
 def restore(self, credential):
     user_did, app_did = check_auth_and_vault(VAULT_ACCESS_WR)
     credential_info = self.auth.get_backup_credential_info(credential)
     self.client.check_backup_status(user_did, True)
     self.client.execute_restore(
         user_did, credential_info,
         self.client.get_access_token(credential, credential_info))
Esempio n. 2
0
    def upload_file(self, path):
        if not path:
            raise InvalidParameterException()

        user_did, app_did = check_auth_and_vault(VAULT_ACCESS_WR)
        self.upload_file_by_did(user_did, app_did, path)
        return {'name': path}
Esempio n. 3
0
 def backup(self, credential, is_force):
     user_did, _ = check_auth_and_vault(VAULT_ACCESS_R)
     credential_info = self.auth.get_backup_credential_info(credential)
     if not is_force:
         self.check_remote_backup_in_progress(user_did)
     req = self.save_request(user_did, credential, credential_info)
     BackupExecutor(user_did, self, req, is_force=is_force).start()
Esempio n. 4
0
 def __get_collection(self, collection_name, vault_permission):
     user_did, app_did = check_auth_and_vault(vault_permission)
     col = cli.get_user_collection(user_did, app_did, collection_name)
     if not col:
         raise CollectionNotFoundException(
             msg=f'The collection {collection_name} does not found.')
     return user_did, app_did, col
Esempio n. 5
0
 def delete_collection(self, collection_name):
     user_did, app_did = check_auth_and_vault(VAULT_ACCESS_DEL)
     cli.delete_collection(user_did,
                           app_did,
                           collection_name,
                           is_check_exist=False)
     update_used_storage_for_mongodb_data(
         user_did, get_mongo_database_size(user_did, app_did))
Esempio n. 6
0
    def get_hash(self, path):
        if not path:
            raise InvalidParameterException()

        user_did, app_did = check_auth_and_vault(VAULT_ACCESS_WR)
        data, err = query_hash(user_did, app_did, path)
        if err:
            raise FileNotFoundException(
                f'Failed getting file hash code: {str(err)}')
        return {'name': path, 'algorithm': 'SHA256', 'hash': data['SHA256']}
Esempio n. 7
0
 def restore(self, credential, is_force):
     user_did, _ = check_auth_and_vault(VAULT_ACCESS_R)
     credential_info = self.auth.get_backup_credential_info(credential)
     if not is_force:
         self.check_remote_backup_in_progress(user_did)
     self.save_request(user_did,
                       credential,
                       credential_info,
                       is_restore=True)
     RestoreExecutor(user_did, self).start()
Esempio n. 8
0
 def list_folder(self, path):
     user_did, app_did = check_auth_and_vault(VAULT_ACCESS_WR)
     full_path = self._get_file_full_path(user_did, app_did, path)
     if not full_path.exists() or not full_path.is_dir():
         raise FileNotFoundException(msg='Folder does not exist.')
     files = os.listdir(full_path.as_posix())
     return {
         'value':
         list(map(lambda f: self._get_file_info(full_path, f), files))
     }
Esempio n. 9
0
    def handle_transaction(self, transaction_id, is_download=False):
        check_auth_and_vault(
            VAULT_ACCESS_R if is_download else VAULT_ACCESS_WR)

        # check by transaction id
        row_id, target_did, target_app_did = self.parse_transaction_id(
            transaction_id)
        col_filter = {"_id": ObjectId(row_id)}
        trans = cli.find_one(target_did, target_app_did,
                             SCRIPTING_SCRIPT_TEMP_TX_COLLECTION, col_filter)
        if not trans:
            raise BadRequestException("Cannot find the transaction by id.")

        # executing uploading or downloading
        data = None
        logging.info(
            f'handle transaction by id: is_ipfs={self.is_ipfs}, '
            f'is_download={is_download}, file_name={trans["document"]["file_name"]}'
        )
        if self.is_ipfs:
            if is_download:
                data = self.ipfs_files.download_file_with_path(
                    target_did, target_app_did, trans['document']['file_name'])
            else:
                self.ipfs_files.upload_file_with_path(
                    target_did, target_app_did, trans['document']['file_name'])
        else:
            if is_download:
                data = self.get_files().download_file_by_did(
                    target_did, target_app_did, trans['document']['file_name'])
            else:
                self.get_files().upload_file_by_did(
                    target_did, target_app_did, trans['document']['file_name'])

        # recalculate the storage usage of the database
        cli.delete_one(target_did, target_app_did,
                       SCRIPTING_SCRIPT_TEMP_TX_COLLECTION, col_filter)
        update_used_storage_for_mongodb_data(
            target_did, get_mongo_database_size(target_did, target_app_did))

        # return the content of the file
        return data
Esempio n. 10
0
    def set_script(self, script_name):
        user_did, app_did = check_auth_and_vault(VAULT_ACCESS_WR)

        json_data = request.get_json(force=True, silent=True)
        Script.validate_script_data(json_data)

        result = self.__upsert_script_to_database(script_name, json_data,
                                                  user_did, app_did)
        update_used_storage_for_mongodb_data(
            user_did, get_mongo_database_size(user_did, app_did))
        return result
Esempio n. 11
0
    def get_properties(self, path):
        if not path:
            raise InvalidParameterException()

        user_did, app_did = check_auth_and_vault(VAULT_ACCESS_R)
        full_path, stat = self.get_file_stat_by_did(user_did, app_did, path)
        return {
            'name': path,
            'is_file': full_path.is_file(),
            'size': stat.st_size,
            'created': cli.timestamp_to_epoch(int(stat.st_ctime)),
            'updated': cli.timestamp_to_epoch(int(stat.st_mtime)),
        }
Esempio n. 12
0
    def delete_script(self, script_name):
        user_did, app_did = check_auth_and_vault(VAULT_ACCESS_DEL)

        col = cli.get_user_collection(user_did, app_did,
                                      SCRIPTING_SCRIPT_COLLECTION)
        if not col:
            raise NotFoundException(NotFoundException.SCRIPT_NOT_FOUND,
                                    'The script collection does not exist.')

        ret = col.delete_many({'name': script_name})
        if ret.deleted_count > 0:
            update_used_storage_for_mongodb_data(
                user_did, get_mongo_database_size(user_did, app_did))
Esempio n. 13
0
    def delete_file(self, path):
        if not path:
            raise InvalidParameterException()

        user_did, app_did = check_auth_and_vault(VAULT_ACCESS_WR)
        full_path = self._get_file_full_path(user_did, app_did, path)
        if full_path.exists():
            if full_path.is_dir():
                dir_size = get_dir_size(full_path.as_posix(), 0.0)
                shutil.rmtree(full_path)
                update_used_storage_for_files_data(user_did, -dir_size)
            else:
                file_size = os.path.getsize(full_path.as_posix())
                full_path.unlink()
                update_used_storage_for_files_data(user_did, -file_size)
Esempio n. 14
0
 def _move_file(self, src_path, dst_path, is_copy=False):
     user_did, app_did = check_auth_and_vault(VAULT_ACCESS_WR)
     full_src_path = self._get_file_full_path(user_did, app_did, src_path)
     full_dst_path = self._get_file_full_path(user_did, app_did, dst_path)
     if not full_src_path.exists():
         raise FileNotFoundException(msg='Source file does not exist.')
     if full_dst_path.exists():
         raise AlreadyExistsException(msg='Destination file exists.')
     if not is_copy:
         shutil.move(full_src_path.as_posix(), full_dst_path.as_posix())
     else:
         if full_src_path.is_file():
             shutil.copy2(full_src_path.as_posix(),
                          full_dst_path.as_posix())
             file_size = os.path.getsize(full_dst_path.as_posix())
             update_used_storage_for_files_data(user_did, file_size)
         else:
             shutil.copytree(full_src_path.as_posix(),
                             full_dst_path.as_posix())
             dir_size = get_dir_size(full_dst_path.as_posix(), 0.0)
             update_used_storage_for_files_data(user_did, dir_size)
     return {'name': dst_path}
Esempio n. 15
0
 def create_collection(self, collection_name):
     user_did, app_did = check_auth_and_vault(VAULT_ACCESS_WR)
     cli.create_collection(user_did, app_did, collection_name)
     return {'name': collection_name}
Esempio n. 16
0
    def download_file(self, path):
        if not path:
            raise InvalidParameterException()

        user_did, app_did = check_auth_and_vault(VAULT_ACCESS_R)
        return self.download_file_by_did(user_did, app_did, path)
Esempio n. 17
0
 def get_state(self):
     user_did, _ = check_auth_and_vault(VAULT_ACCESS_R)
     return self.client.get_state(user_did)
Esempio n. 18
0
 def place_order(self, json_body):
     user_did, app_did = check_auth_and_vault()
     subscription, plan = self._check_place_order_params(json_body)
     return self._get_order_vo(self._create_order(user_did, subscription, plan))
Esempio n. 19
0
 def get_state(self):
     user_did, _ = check_auth_and_vault(VAULT_ACCESS_R)
     return self.get_remote_backup_state(user_did)