Exemplo n.º 1
0
    def create_repo_pool(self, user_account):
        errorMessage = ""
        errorCode = 0

        is_root = 1
        pool_dbo = DboPool(self.application.sql_client)
        if not pool_dbo is None:
            # each time claim, reset old data.
            pool_dbo.empty()

        # TOOD: here should begin trans. and able to rollback.
        is_pass_check, poolid = pool_dbo.add(user_account, is_root)
        if is_pass_check:
            if poolid > 0:
                localpoolname = ""
                can_edit = 1
                status = dbconst.POOL_STATUS_OWNER

                pool_subscriber_dbo = DboPoolSubscriber(
                    self.application.sql_client)
                is_pass_check = pool_subscriber_dbo.add(
                    user_account, poolid, localpoolname, can_edit, status)
                if not is_pass_check:
                    errorMessage = "Add new pool_subscriber fail"
                    errorCode = 1032
            else:
                is_pass_check = False
                errorMessage = "poolid is wrong"
                errorCode = 1031
        else:
            errorMessage = "Add new pool fail"
            errorCode = 1030

        if is_pass_check:
            if poolid > 0:
                user_home = u'%s/storagepool/%s' % (
                    options.storage_access_point, poolid)
                self._mkdir_recursive(user_home)

                from app.controller.meta_manager import MetaManager
                user_dict = {'account': user_account, 'poolid': poolid}
                metadata_manager = MetaManager(self.application.sql_client,
                                               user_dict, "")
                is_pass_check, query_result, errorMessage = metadata_manager.add_metadata(
                    is_dir=1)
                #print "query_result", query_result
                if not is_pass_check:
                    errorMessage = "add metadata in database fail"
                    errorCode = 1040

                # set server claimed.
                self.application.claimed = True

        return is_pass_check, errorMessage, errorCode
Exemplo n.º 2
0
class FileCreateFolderHandler(BaseHandler):
    metadata_manager = None

    def post(self):
        self.set_header('Content-Type', 'application/json')

        is_pass_check = True
        errorMessage = ""
        errorCode = 0

        if is_pass_check:
            is_pass_check = False
            try:
                _body = json.loads(self.request.body)
                is_pass_check = True
            except Exception:
                errorMessage = "wrong json format"
                errorCode = 1001
                pass

        path = None
        if is_pass_check:
            is_pass_check = False
            #logging.info('%s' % (str(_body)))
            if _body:
                try:
                    if 'path' in _body:
                        path = _body['path']
                    is_pass_check = True
                except Exception:
                    errorMessage = "parse json fail"
                    errorCode = 1002

        if is_pass_check:
            ret, errorMessage = self.check_path(path)
            if not ret:
                is_pass_check = False
                errorCode = 1010

        if is_pass_check:
            if len(path) == 0:
                errorMessage = "path is empty"
                errorCode = 1013
                is_pass_check = False

        if is_pass_check:
            self.metadata_manager = MetaManager(self.application.sql_client,
                                                self.current_user, path)

            if not self.metadata_manager.real_path is None:
                if os.path.exists(self.metadata_manager.real_path):
                    # path exist
                    errorMessage = "path is exist"
                    errorCode = 1020
                    is_pass_check = False
            else:
                errorMessage = "no permission"
                errorCode = 1030
                is_pass_check = False

        current_metadata = None
        if is_pass_check:
            current_metadata = self.metadata_manager.get_path()
            if not current_metadata is None:
                errorMessage = "metadata exist"
                errorCode = 1021
                is_pass_check = False

                # handle special case: when database & storage is not synced!
                # [TODO]: create server side folders but not files!
                pass

        if is_pass_check:
            logging.info('Create real path at:%s' %
                         (self.metadata_manager.real_path))
            self._createFolder(self.metadata_manager.real_path)

            # update metadata. (owner)
            is_pass_check, current_metadata, errorMessage = self.metadata_manager.add_metadata(
                is_dir=1)
            if not is_pass_check:
                #errorMessage = "add metadata in database fail"
                errorCode = 1022

        if is_pass_check:
            if not current_metadata is None:
                self.set_header("oid", current_metadata["id"])
                self.write(current_metadata)
        else:
            self.set_status(400)
            self.write(dict(error=dict(message=errorMessage, code=errorCode)))
            #logging.error('%s' % (str(dict(error=dict(message=errorMessage,code=errorCode)))))

    def _createFolder(self, directory_name):
        if not os.path.exists(directory_name):
            try:
                os.makedirs(directory_name)
            except OSError as exc:
                pass