Пример #1
0
    def derive_path(self, org, uuid, extension=None):
        """
        Derives the storage path of an asset, e.g. 'orgs/1/recordings/asdf-asdf-asdf-asdf-asdf-asdf.wav'
        """
        base_name = str(uuid)
        directory = os.path.join(settings.STORAGE_ROOT_DIR, str(org.pk), self.directory)

        if extension:
            return "%s/%s.%s" % (directory, base_name, extension)

        # no explicit extension so look for one with an existing file
        for ext in extension or self.extensions:
            path = "%s/%s.%s" % (directory, base_name, ext)
            if public_file_storage.exists(path):
                return path

        raise AssetFileNotFound()  # pragma: needs cover
Пример #2
0
    def derive_path(self, org, uuid, extension=None):
        """
        Derives the storage path of an asset, e.g. 'orgs/1/recordings/asdf-asdf-asdf-asdf-asdf-asdf.wav'
        """
        base_name = str(uuid)
        directory = os.path.join(settings.STORAGE_ROOT_DIR, str(org.pk),
                                 self.directory)

        if extension:
            return "%s/%s.%s" % (directory, base_name, extension)

        # no explicit extension so look for one with an existing file
        for ext in extension or self.extensions:
            path = "%s/%s.%s" % (directory, base_name, ext)
            if public_file_storage.exists(path):
                return path

        raise AssetFileNotFound()  # pragma: needs cover
Пример #3
0
    def resolve(self, user, pk):
        """
        Returns a tuple of the org, location and download filename of the identified asset. If user does not have access
        to the asset, an exception is raised.
        """
        asset = self.derive_asset(pk)

        if not user.has_org_perm(asset.org,
                                 self.permission):  # pragma: needs cover
            raise AssetAccessDenied()

        if not self.is_asset_ready(asset):
            raise AssetFileNotFound()

        path = self.derive_path(asset.org, asset.uuid)

        if not public_file_storage.exists(path):  # pragma: needs cover
            raise AssetFileNotFound()

        # create a more friendly download filename
        remainder, extension = path.rsplit(".", 1)
        filename = "%s_%s.%s" % (self.key, pk, extension)

        # if our storage backend is S3
        if settings.DEFAULT_FILE_STORAGE == "storages.backends.s3boto.S3BotoStorage":  # pragma: needs cover
            # generate our URL manually so that we can force the download name for the user
            url = public_file_storage.connection.generate_url(
                public_file_storage.querystring_expire,
                method="GET",
                bucket=public_file_storage.bucket.name,
                key=public_file_storage._encode_name(path),
                query_auth=public_file_storage.querystring_auth,
                force_http=not public_file_storage.secure_urls,
                response_headers={
                    "response-content-disposition":
                    "attachment;filename=%s" % filename
                },
            )

        # otherwise, let the backend generate the URL
        else:
            url = public_file_storage.url(path)

        return asset.org, url, filename
Пример #4
0
    def resolve(self, user, pk):
        """
        Returns a tuple of the org, location and download filename of the identified asset. If user does not have access
        to the asset, an exception is raised.
        """
        asset = self.derive_asset(pk)

        if not user.has_org_perm(asset.org, self.permission):  # pragma: needs cover
            raise AssetAccessDenied()

        if not self.is_asset_ready(asset):
            raise AssetFileNotFound()

        path = self.derive_path(asset.org, asset.uuid)

        if not public_file_storage.exists(path):  # pragma: needs cover
            raise AssetFileNotFound()

        # create a more friendly download filename
        remainder, extension = path.rsplit(".", 1)
        filename = "%s_%s.%s" % (self.key, pk, extension)

        # if our storage backend is S3
        if settings.DEFAULT_FILE_STORAGE == "storages.backends.s3boto.S3BotoStorage":  # pragma: needs cover
            # generate our URL manually so that we can force the download name for the user
            url = public_file_storage.connection.generate_url(
                public_file_storage.querystring_expire,
                method="GET",
                bucket=public_file_storage.bucket.name,
                key=public_file_storage._encode_name(path),
                query_auth=public_file_storage.querystring_auth,
                force_http=not public_file_storage.secure_urls,
                response_headers={"response-content-disposition": "attachment;filename=%s" % filename},
            )

        # otherwise, let the backend generate the URL
        else:
            url = public_file_storage.url(path)

        return asset.org, url, filename