Exemple #1
0
    def on_event(self, event, payload):
        if event == "plugin_backup_backup_created":
            # Helper function for human readable sizes
            def _convert_size(size_bytes):
                if size_bytes == 0:
                    return "0B"
                size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB",
                             "YB")
                i = int(math.floor(math.log(size_bytes, 1024)))
                p = math.pow(1024, i)
                s = round(size_bytes / p, 2)
                return "%s %s" % (s, size_name[i])

            now = datetime.now()

            davoptions = {
                'webdav_hostname': self._settings.get(["server"]),
                'webdav_login': self._settings.get(["username"]),
                'webdav_password': self._settings.get(["password"]),
                'webdav_timeout': self._settings.get(["timeout"]),
            }

            backup_path = payload["path"]
            backup_name = payload["name"]
            self._logger.info("Backup " + backup_path +
                              " created, will now attempt to upload to " +
                              davoptions["webdav_hostname"])

            davclient = Client(davoptions)
            davclient.verify = self._settings.get(["verify_certificate"])
            check_space = self._settings.get(["check_space"])
            upload_path = now.strftime(self._settings.get(["upload_path"]))
            upload_path = ospath.join("/", upload_path)

            if self._settings.get(["upload_name"]):
                upload_name = now.strftime(self._settings.get(
                    ["upload_name"])) + ospath.splitext(backup_path)[1]
            else:
                upload_name = backup_name
            self._logger.debug("Filename for upload: " + upload_name)

            upload_file = ospath.join("/", upload_path, upload_name)
            upload_temp = ospath.join("/", upload_path, upload_name + ".tmp")

            self._logger.debug("Upload location: " + upload_file)

            # Check actual connection to the WebDAV server as the check command will not do this.
            if check_space:
                self._logger.debug("Attempting to check free space.")
                try:
                    # If the resource was not found
                    dav_free = davclient.free()
                    if dav_free < 0:
                        # If we get a negative free size, this server is not returning correct value.
                        check_space = False
                        self._logger.warning(
                            "Free space on server: " +
                            _convert_size(dav_free) +
                            ", it appears your server does not support reporting size correctly but it's still a proper way to check connectivity."
                        )
                    else:
                        self._logger.info("Free space on server: " +
                                          _convert_size(dav_free))
                except RemoteResourceNotFound as exception:
                    self._logger.error(
                        "Resource was not found, something is probably wrong with your settings."
                    )
                    return
                except ResponseErrorCode as exception:
                    # Write error and exit function
                    status = HTTPStatus(exception.code)
                    error_switcher = {
                        400: "Bad request",
                        401: "Unauthorized",
                        403: "Forbidden",
                        404: "Not found",
                        405: "Method not allowed",
                        408: "Request timeout",
                        500: "Internal error",
                        501: "Not implemented",
                        502: "Bad gateway",
                        503: "Service unavailable",
                        504: "Gateway timeout",
                        508: "Loop detected",
                    }
                    if (exception.code == 401):
                        http_error = "HTTP error 401 encountered, your credentials are most likely wrong."
                    else:
                        http_error = "HTTP error encountered: " + str(
                            status.value) + " " + error_switcher.get(
                                exception.code, status.phrase)
                    self._logger.error(http_error)
                    return
                except WebDavException as exception:
                    self._logger.error(
                        "An unexpected WebDAV error was encountered: " +
                        exception.args)
                    raise
            else:
                self._logger.debug(
                    "Not checking free space, just try to check the WebDAV root."
                )
                # Not as proper of a check as retrieving size, but it's something.
                if davclient.check("/"):
                    self._logger.debug("Server returned WebDAV root.")
                else:
                    self._logger.error(
                        "Server did not return WebDAV root, something is probably wronkg with your settings."
                    )
                    return

            backup_size = ospath.getsize(backup_path)
            self._logger.info("Backup file size: " +
                              _convert_size(backup_size))

            if check_space and (backup_size > dav_free):
                self._logger.error("Unable to upload, size is" +
                                   _convert_size(backup_size) +
                                   ", free space is " +
                                   _convert_size(dav_free))
                return
            else:
                # Helper function to recursively create paths
                def _recursive_create_path(path):
                    # Append leading / for preventing abspath issues
                    path = ospath.join("/", path)
                    if davclient.check(path):
                        self._logger.debug("Directory " + path + " was found.")
                        return True
                    else:
                        if path != "/":
                            self._logger.debug(
                                "Directory " + path +
                                " was not found, checking parent.")
                            if _recursive_create_path(
                                    ospath.abspath(ospath.join(path, ".."))):
                                davclient.mkdir(path)
                                self._logger.debug("Directory " + path +
                                                   " has been created.")
                                return True
                        else:
                            self._logger.error(
                                "Could not find WebDAV root, something is probably wrong with your settings."
                            )
                            return False

                if _recursive_create_path(upload_path):
                    self._logger.debug("Uploading " + backup_path + " to " +
                                       upload_temp)
                    davclient.upload_sync(remote_path=upload_temp,
                                          local_path=backup_path)
                    self._logger.debug("Moving " + upload_temp + " to " +
                                       upload_file)
                    davclient.move(remote_path_from=upload_temp,
                                   remote_path_to=upload_file)
                    self._logger.info(
                        "Backup has been uploaded successfully to " +
                        davoptions["webdav_hostname"] + " as " + upload_file)
                else:
                    self._logger.error(
                        "Something went wrong trying to check/create the upload path."
                    )
class WebDAV:
    """Commerce Cloud WebDAV session.

    Args:
        client (CommerceCloudClientSession): Active client session with Commerce Cloud for a bearer token.
        instance (str, optional): Optional commerce cloud instance, useful for opening clients to multiple instances using the same bearer token. Defaults to None.
        cert (str, optional): Path to TLS client certificate. Defaults to None.
        key ([type], optional): Export key for the TLS certificate. Defaults to None.
        verify (bool, optional): Verify TLS certificates, set to false for self signed. Defaults to True.
    """
    def __init__(self,
                 client,
                 instance=None,
                 cert=None,
                 key=None,
                 verify=True):
        self.client = client

        self._instance = instance or self.client.instance
        self.options = {"webdav_hostname": self._instance.rstrip("/")}
        self.verify = verify
        self.token = self.client.Token
        self.options.update({"webdav_token": self.token["access_token"]})
        self.webdav_client = Client(self.options)
        self.webdav_client.verify = self.verify
        if cert and key:
            self.cert = str(Path(cert).resolve())
            self.key = str(Path(key).resolve())
            self.options.update({"cert_path": self.cert, "key_path": self.key})

    def reauth(self):
        """Checks token expiry and re-initialises the Client if a new token is needed.
        """
        if self.token["expires_at"] < int(time.time()):
            self.client.getToken()
            self.options.update({"webdav_token": self.token["access_token"]})
            self.webdav_client = Client(self.options)

    def reconnect(self):
        """Re-initalise the Client session.
        """
        self.webdav_client = Client(self.options)

    @property
    def hostname(self):
        """Return the hostname the WebDAV client connection is connected to.

        Returns:
            str: Hostname including prefix eg https://
        """
        return self.options["webdav_hostname"]

    @property
    def netloc(self):
        """Return a urlparse netloc string of the connected hostname.

        Returns:
            str: netloc of hostname.
        """
        url = urlparse(self.options["webdav_hostname"])
        return url.netloc

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def GetInfo(self, remote_filepath: str, headers: dict = None) -> list:
        """Get properties for entity

        [extended_summary]

        Args:
            remote_filepath (str): Path to remote resource.
            headers (dict, optional): Additional headers to apply to request. Defaults to None.

        Raises:
            RetryException: Adds to retries counter on failure.

        Returns:
            list: WebDAV attribute information.
        """
        try:
            return self.webdav_client.info(remote_filepath)
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def GetDirectoryList(self,
                         filepath: str,
                         get_info: bool = False,
                         headers: dict = None) -> list:
        """Get list of files and folders in a path from WebDAV endpoint.

        [extended_summary]

        Args:
            filepath (str): Path to get directory listing for.
            get_info (bool): returns dictionary of attributes instead of file list.
            headers (dict, optional): Additional headers to apply to request. Defaults to None.

        Returns:
            list: Directory listing.
        """
        try:
            return self.webdav_client.list(filepath, get_info=get_info)
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def Upload(self, local_filepath: str, remote_filepath: str):
        """Upload file or directory recursively to WebDAV endpoint.

        [extended_summary]

        Args:
            local_filepath (str): Local path to file or directory to upload.
            remote_filepath (str): Remote path to upload to.
        """
        local_filepath = str(Path(local_filepath).resolve())
        try:
            self.webdav_client.upload_sync(remote_filepath, local_filepath)
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def StreamUpload(self, payload, remote_path: str, file_name: str):
        """Upload FileIO, StringIO, BytesIO or string to WebDAV

        [extended_summary]

        Args:
            payload: Stream payload
            remote_path (str): Remote path relative to host.
            file_name (str): Name for the file uploaded.
        """
        try:
            self.webdav_client.upload_to(payload, f"{remote_path}/{file_name}")
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def MakeDir(self, remote_path: str):
        """Make new directory at path specified.

        Args:
            remote_path (str): Path of proposed new directory.
        """
        try:
            self.webdav_client.mkdir(remote_path)
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def Move(self,
             remote_path_source: str,
             remote_path_dest: str,
             overwrite: bool = False):
        """Make new directory at path specified.

        Args:
            remote_path_source (str): Path of source resource.
            remote_path_dest (str): Path of destination resource.
            overwrite (bool): Overwrite destination resource. Defaults to False.
        """
        try:
            self.webdav_client.move(remote_path_source, remote_path_dest,
                                    overwrite)
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def Delete(self, remote_filepath: str):
        """Delete file on remote WebDAV endpoint.

        Args:
            remote_filepath (str): Location of resource to delete.
        """
        try:
            self.webdav_client.clean(remote_filepath)
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def Download(self, local_filepath: str, remote_filepath: str):
        """Download file/folder from WebDAV endpoint.

        This is a synchronous operation, and the file is downloaded in full to the local_filepath.

        Args:
            local_filepath (str): Local path to download to, including filename of file saved.
            remote_filepath (str): Remote path to file to download.
        """
        local_filepath = str(Path(local_filepath).resolve())
        try:
            self.webdav_client.download_sync(remote_filepath, local_filepath)
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def Pull(self, local_filepath: str, remote_filepath: str):
        """Sync file/folder from WebDAV endpoint to local storage.

        This downloads missing or nwer modified files from the remote to local storage.
        You can use it to do "resumeable" transfers, but the checks are slow for deeply nested files.

        Args:
            local_filepath (str): Local path to download to, including filename of file saved.
            remote_filepath (str): Remote path to file to download.
        """
        local_filepath = str(Path(local_filepath).resolve())
        try:
            self.webdav_client.pull(remote_filepath, local_filepath)
            return True
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException
        return False

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=3,
        retry_window_after_first_call_in_seconds=10,
    )
    def Push(self, local_filepath: str, remote_filepath: str):
        """Sync file/folder from local storage to WebDAV endpoint.

        This uploads missing or nwer modified files from the local to remote storage.
        You can use it to do "resumeable" transfers, but the checks are slow for deeply nested files.

        Args:
            local_filepath (str): Local path to download to, including filename of file saved.
            remote_filepath (str): Remote path to file to download.
        """
        local_filepath = str(Path(local_filepath).resolve())
        try:
            self.webdav_client.push(local_filepath, remote_filepath)
            return True
        except (NoConnection, ConnectionException, WebDavException):
            self.reauth()
            raise RetryException
        return False

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=10,
        retry_window_after_first_call_in_seconds=15,
    )
    def StreamDownload(self,
                       remote_filepath: str,
                       buffer=None,
                       decode: bool = False):
        """Download a file in chunks to a local file buffer.

        You must provide a BytesIO object or one will be created for you.

        Args:
            remote_filepath (str): Path to remote resource to download.
            buffer ([type], optional): Buffer write streamed content to.
            decode (bool, optional): Optionally try to decode downloaded file into a string. Defaults to False.

        Raises:
            RetryException: Adds to retries counter on failure.

        Returns:
            Bytes: Returns a BytesIO object for further use.
        """
        self.reauth()
        if buffer is None:
            buffer = BytesIO()
        try:
            self.webdav_client.download_from(buff=buffer,
                                             remote_path=remote_filepath)
            if decode is True:
                return buffer.getvalue().decode("utf-8")
            else:
                buffer.seek(0)
                return buffer
        except (NoConnection, ConnectionException, WebDavException):
            raise RetryException

    @retry(
        retry_on_exceptions=(RetryException),
        max_calls_total=10,
        retry_window_after_first_call_in_seconds=60,
    )
    def HashObject(self, remote_filepath: str) -> str:
        """Generate a MD5 hashsum for a remote resource.

        This is streamed into memory, hashed and discarded. Optimised for low memory but
        high bandwidth environments.

        Args:
            remote_filepath (str): Path to remote resource.

        Raises:
            RetryException: Adds to retries counter on failure.

        Returns:
            str: MDSSUM of the file requested.
        """
        self.reauth()
        try:
            sum = md5(self.StreamDownload(remote_filepath).getbuffer())
            return {
                "filepath": remote_filepath,
                "hashtype": "MD5",
                "hashsum": sum.hexdigest(),
            }

        except (NoConnection, ConnectionException, WebDavException):
            self.reconnect()
            raise RetryException

    def RecursiveFileListing(self, remote_filepath: str) -> str:
        """Recursive filetree walker, returns paths found.

        Args:
            remote_filepath (str): [description]

        Raises:
            RetryException: Adds to retries counter on failure.

        Yields:
            Iterator[str]: Yields resource paths for any files found.
        """
        @retry(
            retry_on_exceptions=(RetryException),
            max_calls_total=10,
            retry_window_after_first_call_in_seconds=60,
        )
        def get_list(self, path):
            self.reauth()
            try:
                return self.webdav_client.list(path, get_info=True)
            except (NoConnection, ConnectionException, WebDavException):
                self.reconnect()
                raise RetryException

        def get_files(self, path):
            return [x for x in get_list(self, path) if x["isdir"] is False]

        def get_dirs(self, path):
            return [
                x["path"] for x in get_list(self, path) if x["isdir"] is True
            ]

        yield from get_files(self, remote_filepath)
        for subdir in get_dirs(self, remote_filepath):
            yield from self.RecursiveFileListing(subdir)

    def RecursiveFolderListing(self, remote_filepath: str) -> str:
        """Recursive filetree walker, returns paths found.

        Args:
            remote_filepath (str): [description]

        Raises:
            RetryException: Adds to retries counter on failure.

        Yields:
            Iterator[str]: Yields resource paths for any files found.
        """
        @retry(
            retry_on_exceptions=(RetryException),
            max_calls_total=10,
            retry_window_after_first_call_in_seconds=60,
        )
        def get_list(self, path):
            self.reauth()
            try:
                return self.webdav_client.list(path, get_info=True)
            except (NoConnection, ConnectionException, WebDavException):
                self.reconnect()
                raise RetryException

        def get_dirs(self, path):
            return [
                x["path"] for x in get_list(self, path) if x["isdir"] is True
            ]

        dirlist = get_dirs(self, remote_filepath)

        yield from dirlist
        for subdir in get_dirs(self, remote_filepath):
            yield from self.RecursiveFolderListing(subdir)
class ClientTestCase(TestCase):
    remote_path_file = 'test_dir/test.txt'
    remote_path_file2 = 'test_dir2/test.txt'
    remote_path_dir = 'test_dir'
    remote_path_dir2 = 'test_dir2'
    local_base_dir = 'tests/'
    local_file = 'test.txt'
    local_file_path = local_base_dir + 'test.txt'
    local_path_dir = local_base_dir + 'res/test_dir'

    def setUp(self):
        options = {
            'webdav_hostname': 'https://webdav.yandex.ru',
            'webdav_login': '******',
            'webdav_password': '******'
        }
        self.client = Client(options)
        if path.exists(path=self.local_path_dir):
            shutil.rmtree(path=self.local_path_dir)

    def tearDown(self):
        if path.exists(path=self.local_path_dir):
            shutil.rmtree(path=self.local_path_dir)
        if self.client.check(remote_path=self.remote_path_dir):
            self.client.clean(remote_path=self.remote_path_dir)
        if self.client.check(remote_path=self.remote_path_dir2):
            self.client.clean(remote_path=self.remote_path_dir2)

    def test_list(self):
        self._prepare_for_downloading()
        file_list = self.client.list()
        self.assertIsNotNone(file_list, 'List of files should not be None')
        self.assertGreater(file_list.__len__(), 0,
                           'Expected that amount of files more then 0')

    def test_free(self):
        self.assertGreater(
            self.client.free(), 0,
            'Expected that free space on WebDAV server is more then 0 bytes')

    def test_check(self):
        self.assertTrue(self.client.check(),
                        'Expected that root directory is exist')

    def test_mkdir(self):
        if self.client.check(remote_path=self.remote_path_dir):
            self.client.clean(remote_path=self.remote_path_dir)
        self.client.mkdir(remote_path=self.remote_path_dir)
        self.assertTrue(self.client.check(remote_path=self.remote_path_dir),
                        'Expected the directory is created.')

    @unittest.skip(
        "Yandex brakes response for file it contains property resourcetype as collection but it should "
        "be empty for file")
    def test_download_to(self):
        self._prepare_for_downloading()
        buff = BytesIO()
        self.client.download_from(buff=buff, remote_path=self.remote_path_file)
        self.assertEquals(buff.getvalue(),
                          'test content for testing of webdav client')

    @unittest.skip(
        "Yandex brakes response for file it contains property resourcetype as collection but it should "
        "be empty for file")
    def test_download(self):
        self._prepare_for_downloading()
        self.client.download(local_path=self.local_path_dir,
                             remote_path=self.remote_path_dir)
        self.assertTrue(path.exists(self.local_path_dir),
                        'Expected the directory is downloaded.')
        self.assertTrue(path.isdir(self.local_path_dir),
                        'Expected this is a directory.')
        self.assertTrue(
            path.exists(self.local_path_dir + os.path.sep + self.local_file),
            'Expected the file is downloaded')
        self.assertTrue(
            path.isfile(self.local_path_dir + os.path.sep +
                        self.local_path_file), 'Expected this is a file')

    @unittest.skip(
        "Yandex brakes response for file it contains property resourcetype as collection but it should "
        "be empty for file")
    def test_download_sync(self):
        self._prepare_for_downloading()
        os.mkdir(self.local_path_dir)

        def callback():
            self.assertTrue(
                path.exists(self.local_path_dir + os.path.sep +
                            self.local_file),
                'Expected the file is downloaded')
            self.assertTrue(
                path.isfile(self.local_path_dir + os.path.sep +
                            self.local_file), 'Expected this is a file')

        self.client.download_sync(local_path=self.local_path_dir +
                                  os.path.sep + self.local_file,
                                  remote_path=self.remote_path_file,
                                  callback=callback)
        self.assertTrue(
            path.exists(self.local_path_dir + os.path.sep + self.local_file),
            'Expected the file has already been downloaded')

    @unittest.skip(
        "Yandex brakes response for file it contains property resourcetype as collection but it should "
        "be empty for file")
    def test_download_async(self):
        self._prepare_for_downloading()
        os.mkdir(self.local_path_dir)

        def callback():
            self.assertTrue(
                path.exists(self.local_path_dir + os.path.sep +
                            self.local_file),
                'Expected the file is downloaded')
            self.assertTrue(
                path.isfile(self.local_path_dir + os.path.sep +
                            self.local_file), 'Expected this is a file')

        self.client.download_async(local_path=self.local_path_dir +
                                   os.path.sep + self.local_file,
                                   remote_path=self.remote_path_file,
                                   callback=callback)
        self.assertFalse(
            path.exists(self.local_path_dir + os.path.sep + self.local_file),
            'Expected the file has not been downloaded yet')

    def test_upload_from(self):
        self._prepare_for_uploading()
        buff = StringIO(u'test content for testing of webdav client')
        self.client.upload_to(buff=buff, remote_path=self.remote_path_file)
        self.assertTrue(self.client.check(self.remote_path_file),
                        'Expected the file is uploaded.')

    def test_upload(self):
        self._prepare_for_uploading()
        self.client.upload(remote_path=self.remote_path_file,
                           local_path=self.local_path_dir)
        self.assertTrue(self.client.check(self.remote_path_dir),
                        'Expected the directory is created.')
        self.assertTrue(self.client.check(self.remote_path_file),
                        'Expected the file is uploaded.')

    def test_upload_file(self):
        self._prepare_for_uploading()
        self.client.upload_file(remote_path=self.remote_path_file,
                                local_path=self.local_file_path)
        self.assertTrue(self.client.check(remote_path=self.remote_path_file),
                        'Expected the file is uploaded.')

    def test_upload_sync(self):
        self._prepare_for_uploading()

        def callback():
            self.assertTrue(self.client.check(self.remote_path_dir),
                            'Expected the directory is created.')
            self.assertTrue(self.client.check(self.remote_path_file),
                            'Expected the file is uploaded.')

        self.client.upload(remote_path=self.remote_path_file,
                           local_path=self.local_path_dir)

    def test_upload_async(self):
        self._prepare_for_uploading()

        def callback():
            self.assertTrue(self.client.check(self.remote_path_dir),
                            'Expected the directory is created.')
            self.assertTrue(self.client.check(self.remote_path_file),
                            'Expected the file is uploaded.')

        self.client.upload(remote_path=self.remote_path_file,
                           local_path=self.local_path_dir)

    def test_copy(self):
        self._prepare_for_downloading()
        self.client.mkdir(remote_path=self.remote_path_dir2)
        self.client.copy(remote_path_from=self.remote_path_file,
                         remote_path_to=self.remote_path_file2)
        self.assertTrue(self.client.check(remote_path=self.remote_path_file2))

    def test_move(self):
        self._prepare_for_downloading()
        self.client.mkdir(remote_path=self.remote_path_dir2)
        self.client.move(remote_path_from=self.remote_path_file,
                         remote_path_to=self.remote_path_file2)
        self.assertFalse(self.client.check(remote_path=self.remote_path_file))
        self.assertTrue(self.client.check(remote_path=self.remote_path_file2))

    def test_clean(self):
        self._prepare_for_downloading()
        self.client.clean(remote_path=self.remote_path_dir)
        self.assertFalse(self.client.check(remote_path=self.remote_path_file))
        self.assertFalse(self.client.check(remote_path=self.remote_path_dir))

    def test_info(self):
        self._prepare_for_downloading()
        result = self.client.info(remote_path=self.remote_path_file)
        self.assertEquals(result['name'], 'test.txt')
        self.assertEquals(result['size'], '41')
        self.assertTrue('created' in result)
        self.assertTrue('modified' in result)

    def test_directory_is_dir(self):
        self._prepare_for_downloading()
        self.assertTrue(self.client.is_dir(self.remote_path_dir),
                        'Should return True for directory')

    def test_file_is_not_dir(self):
        self._prepare_for_downloading()
        self.assertFalse(self.client.is_dir(self.remote_path_file),
                         'Should return False for file')

    def test_get_property_of_non_exist(self):
        self._prepare_for_downloading()
        result = self.client.get_property(remote_path=self.remote_path_file,
                                          option={'name': 'aProperty'})
        self.assertEquals(
            result, None, 'For not found property should return value as None')

    def test_set_property(self):
        self._prepare_for_downloading()
        self.client.set_property(remote_path=self.remote_path_file,
                                 option={
                                     'namespace': 'test',
                                     'name': 'aProperty',
                                     'value': 'aValue'
                                 })
        result = self.client.get_property(remote_path=self.remote_path_file,
                                          option={
                                              'namespace': 'test',
                                              'name': 'aProperty'
                                          })
        self.assertEquals(result, 'aValue', 'Property value should be set')

    def test_set_property_batch(self):
        self._prepare_for_downloading()
        self.client.set_property_batch(remote_path=self.remote_path_file,
                                       option=[{
                                           'namespace': 'test',
                                           'name': 'aProperty',
                                           'value': 'aValue'
                                       }, {
                                           'namespace': 'test',
                                           'name': 'aProperty2',
                                           'value': 'aValue2'
                                       }])
        result = self.client.get_property(remote_path=self.remote_path_file,
                                          option={
                                              'namespace': 'test',
                                              'name': 'aProperty'
                                          })
        self.assertEquals(result, 'aValue',
                          'First property value should be set')
        result = self.client.get_property(remote_path=self.remote_path_file,
                                          option={
                                              'namespace': 'test',
                                              'name': 'aProperty2'
                                          })
        self.assertEquals(result, 'aValue2',
                          'Second property value should be set')

    def _prepare_for_downloading(self):
        if not self.client.check(remote_path=self.remote_path_dir):
            self.client.mkdir(remote_path=self.remote_path_dir)
        if not self.client.check(remote_path=self.remote_path_file):
            self.client.upload_file(remote_path=self.remote_path_file,
                                    local_path=self.local_file_path)
        if not path.exists(self.local_path_dir):
            os.makedirs(self.local_path_dir)

    def _prepare_for_uploading(self):
        if not self.client.check(remote_path=self.remote_path_dir):
            self.client.mkdir(remote_path=self.remote_path_dir)
        if not path.exists(path=self.local_path_dir):
            os.makedirs(self.local_path_dir)
        if not path.exists(path=self.local_path_dir + os.sep +
                           self.local_file):
            shutil.copy(src=self.local_file_path,
                        dst=self.local_path_dir + os.sep + self.local_file)
Exemple #4
0
class FileBrowserWebdavStorage(StorageMixin, Storage):
    def __init__(self,
                 base_url='/',
                 url_as_download=True,
                 simple_listdir=False,
                 webdav_root='/'):
        self.base_url = base_url
        self.url_as_download = url_as_download
        self.simple_listdir = simple_listdir

        webdav_client_options = {
            'webdav_hostname':
            settings.CONTRAX_FILE_STORAGE_WEBDAV_ROOT_URL.rstrip('/'),
            'webdav_login':
            settings.CONTRAX_FILE_STORAGE_WEBDAV_USERNAME,
            'webdav_password':
            settings.CONTRAX_FILE_STORAGE_WEBDAV_PASSWORD,
        }
        self.client = Client(webdav_client_options)

        try:
            self.client.mkdir('/media')
            self.client.mkdir('/media/photo')
        except:
            pass
        self.client.webdav.root = webdav_root
        self.client.root = webdav_root

    def path(self, name):
        """
        Return a local filesystem path where the file can be retrieved using
        Python's built-in open() function. Storage systems that can't be
        accessed using open() should *not* implement this method.
        """
        # FIXME: this would be useful with self.location != ''
        # in this case use this notation:
        # 1. define self.location in __init__
        # 2. rewrite path() method to be like
        # return os.oath.join(self.location, name)
        # 3. everywhere in other sel.methods use self.path(name) instead of name attr
        return name

    def isdir(self, path):
        """
        Returns true if name exists and is a directory.
        """
        return self.client.check(path) and self.client.is_dir(path)

    def isfile(self, path):
        """
        Returns true if name exists and is a regular file.
        """
        return self.client.check(path) and not self.client.is_dir(path)

    def move(self, old_file_name, new_file_name, allow_overwrite=False):
        """
        Moves safely a file from one location to another.

        If allow_ovewrite==False and new_file_name exists, raises an exception.
        """
        return self.client.move(remote_path_from=old_file_name,
                                remote_path_to=new_file_name,
                                overwrite=allow_overwrite)

    def makedirs(self, path):
        """
        Creates all missing directories specified by name. Analogue to os.mkdirs().
        """
        return self.client.mkdir(path)

    def rmtree(self, path):
        """
        Deletes a directory and everything it contains. Analogue to shutil.rmtree().
        """
        return self.client.clean(path)

    def setpermission(self, path):
        """
        Sets file permission
        """
        pass

    def _open(self, path, mode='rb'):
        tmp = io.BytesIO()
        self.client.download_from(tmp, path)
        tmp.seek(0)
        return File(tmp)

    def _save(self, path, content):
        res = self.client.resource(path)
        res.read_from(content)
        return path

    def get_valid_name(self, name):
        """
        Return a filename, based on the provided filename, that's suitable for
        use in the target storage system.
        """
        return get_valid_filename(name)

    def delete(self, path):
        """
        Delete the specified file from the storage system.
        """
        if self.exists(path):
            self.client.clean(path)

    def exists(self, path):
        """
        Return True if a file referenced by the given name already exists in the
        storage system, or False if the name is available for a new file.
        """
        return self.client.check(path)

    def listdir(self, path):
        """
        List the contents of the specified path. Return a 2-tuple of lists:
        the first item being directories, the second item being files.
        """
        _list = self.client.list(path)

        # for API: iterating over big directory take too much time
        if self.simple_listdir:
            return _list

        # for filebrowser
        directories, files = [], []
        for entry in _list:
            entry_path = os.path.join(path, entry)
            if self.isdir(entry_path):
                directories.append(entry.rstrip('/'))
            else:
                files.append(entry)
        return directories, files

    def size(self, path):
        """
        Return the total size, in bytes, of the file specified by name.
        """
        return self.client.info(path)['size']

    def url(self, path):
        """
        Return an absolute URL where the file's contents can be accessed
        directly by a Web browser.
        """
        url = filepath_to_uri(path)
        if url is not None:
            url = url.lstrip('/')
        url = urljoin(self.base_url, url)
        if self.url_as_download and self.isfile(path):
            url += '?action=download'
        return url

    @staticmethod
    def _datetime_from_timestamp(ts, fmt):
        """
        If timezone support is enabled, make an aware datetime object in UTC;
        otherwise make a naive one in the local timezone.
        """
        dt = datetime.strptime(ts, fmt)
        if settings.USE_TZ:
            # Safe to use .replace() because UTC doesn't have DST
            return dt.replace(tzinfo=timezone.utc)
        else:
            return dt

    def get_accessed_time(self, path):
        """
        Return the last accessed time (as a datetime) of the file specified by
        name. The datetime will be timezone-aware if USE_TZ=True.
        """
        pass

    def get_created_time(self, path):
        """
        Return the creation time (as a datetime) of the file specified by name.
        The datetime will be timezone-aware if USE_TZ=True.
        """
        return self._datetime_from_timestamp(self.client.info(path)['created'],
                                             fmt='%Y-%m-%dT%H:%M:%SZ')

    def get_modified_time(self, path):
        """
        Return the last modified time (as a datetime) of the file specified by
        name. The datetime will be timezone-aware if USE_TZ=True.
        """
        return self._datetime_from_timestamp(
            self.client.info(path)['modified'], fmt='%a, %d %b %Y %H:%M:%S %Z')