Example #1
0
    def copy_fle(self, fle, dst_dir):

        new_fle = copy.copy(fle)

        # Clean Name
        dst_name = fle['name']
        dst_name = util.clean_path(dst_name)
        dst_name = util.secure_path(dst_name)

        # Clean Dir
        dst_dir = util.clean_path(dst_dir)
        dst_dir = util.secure_path(dst_dir)

        # Setup Paths
        src_path = os.path.abspath("{:s}".format(new_fle['path']))
        dst_path = os.path.abspath(os.path.join(dst_dir, dst_name))
        dst_dir = os.path.dirname(dst_path)
        msg = "envmod_local: Copying '{:s}' to '{:s}'".format(src_path, dst_path)
        logger.info(self._format_msg(msg))

        # Setup Dst
        if not os.path.exists(dst_dir):
            os.makedirs(dst_dir)

        # Copy
        shutil.copy(src_path, dst_path)
        new_fle['name'] = dst_name
        new_fle['path'] = dst_path

        # Return
        return new_fle
Example #2
0
    def copy_fle(self, fle, dst_dir):

        new_fle = copy.copy(fle)

        # Clean Name
        dst_name = fle['name']
        dst_name = util.clean_path(dst_name)
        dst_name = util.secure_path(dst_name)

        # Clean Dir
        dst_dir = util.clean_path(dst_dir)
        dst_dir = util.secure_path(dst_dir)

        # Setup Paths
        src_path = os.path.abspath("{:s}".format(new_fle['path']))
        dst_path = os.path.abspath(os.path.join(dst_dir, dst_name))
        dst_dir = os.path.dirname(dst_path)
        msg = "envmod_local: Copying '{:s}' to '{:s}'".format(
            src_path, dst_path)
        logger.info(self._format_msg(msg))

        # Setup Dst
        if not os.path.exists(dst_dir):
            os.makedirs(dst_dir)

        # Copy
        shutil.copy(src_path, dst_path)
        new_fle['name'] = dst_name
        new_fle['path'] = dst_path

        # Return
        return new_fle
Example #3
0
    def from_new(cls, data, **kwargs):
        """New Constructor"""

        # Extract Args
        src_path = kwargs.pop('src_path', None)
        if not src_path:
            raise TypeError("src_path required")

        # Set Schema
        schema = set(_FILE_SCHEMA)
        kwargs['schema'] = schema

        # Create New Object
        data = copy.copy(data)

        # Setup Name
        name = data.get('name', None)
        if not name:
            name = os.path.basename(src_path)
        name = util.clean_path(name)
        name = util.secure_path(name)
        name = os.path.normpath(name)
        if not name:
            raise ValueError("Valid filename required")
        data['name'] = name

        # Setup Blank Path
        data['path'] = ""

        # Get Type
        typ = mimetypes.guess_type(name)
        data['type'] = str(typ[0])
        data['encoding'] = str(typ[1])

        # Call Parent
        fle = super(File, cls).from_new(data, **kwargs)

        # Set Path
        dst_path = os.path.abspath(
            os.path.join(config.FILESTORAGE_PATH, repr(fle)))
        fle['path'] = dst_path

        # Save File
        try:
            shutil.copy(src_path, dst_path)
        except IOError:
            # Clean up on failure
            fle.delete(force=True)
            raise

        # Return File
        return fle
Example #4
0
    def from_new(cls, data, **kwargs):
        """New Constructor"""

        # Extract Args
        src_path = kwargs.pop('src_path', None)
        if not src_path:
            raise TypeError("src_path required")

        # Set Schema
        schema = set(_FILE_SCHEMA)
        kwargs['schema'] = schema

        # Create New Object
        data = copy.copy(data)

        # Setup Name
        name = data.get('name', None)
        if not name:
            name = os.path.basename(src_path)
        name = util.clean_path(name)
        name = util.secure_path(name)
        name = os.path.normpath(name)
        if not name:
            raise ValueError("Valid filename required")
        data['name'] = name

        # Setup Blank Path
        data['path'] = ""

        # Get Type
        typ = mimetypes.guess_type(name)
        data['type'] = str(typ[0])
        data['encoding'] = str(typ[1])

        # Call Parent
        fle = super(File, cls).from_new(data, **kwargs)

        # Set Path
        dst_path = os.path.abspath(os.path.join(config.FILESTORAGE_PATH, repr(fle)))
        fle['path'] = dst_path

        # Save File
        try:
            shutil.copy(src_path, dst_path)
        except IOError:
            # Clean up on failure
            fle.delete(force=True)
            raise

        # Return File
        return fle