Esempio n. 1
0
def weasy_fetcher(url, **kwargs):
    """
    Definição de URLs relativas;
    Para acessar imagens do MEDIA, utilizar media:
    Para os do STATIC, utilizar static:

    Ex:
    url('media:{{papelaria.papel_timbrado_paisagem_a4}}');

    """
    if url.startswith('static:'):
        url = url[len('static:'):]
        file_path = os.path.join(settings.STATIC_ROOT, url)
        if isReadableFile(file_path):
            return weasyprint.default_url_fetcher('file://' + file_path)
    elif url.startswith('media:'):
        url = url[len('media:'):]
        # Normalizando a URL;
        # Removendo o sufixo de MEDIA_URL
        url_path = os.path.normpath(url.replace(settings.MEDIA_URL, '', 1)).split(os.sep)
        # Normalizando o MEDIA_ROOT para path absoluto e juntando com a URL
        file_path = os.path.join(os.path.abspath(settings.MEDIA_ROOT), *url_path)

        if isReadableFile(file_path):
            return weasyprint.default_url_fetcher('file:///' + file_path.replace(os.sep, '/'))

    return weasyprint.default_url_fetcher(url)
Esempio n. 2
0
def bika_url_fetcher(url):
    """Basically the same as the default_url_fetcher from WeasyPrint,
    but injects the __ac cookie to make an authenticated request to the 
    resource.
    """
    from weasyprint import VERSION_STRING
    from weasyprint.compat import Request
    from weasyprint.compat import urlopen_contenttype

    request = api.get_request()
    __ac = request.cookies.get("__ac", "")

    if request.get_header("HOST") in url:
        result, mime_type, charset = urlopen_contenttype(
            Request(url,
                    headers={
                        'Cookie': "__ac={}".format(__ac),
                        'User-Agent': VERSION_STRING,
                        'Authorization': request._auth,
                    }))
        return dict(file_obj=result,
                    redirected_url=result.geturl(),
                    mime_type=mime_type,
                    encoding=charset)

    return default_url_fetcher(url)
Esempio n. 3
0
def django_url_fetcher(url):
    mime_type, encoding = mimetypes.guess_type(url)
    url_path = urlparse(url).path
    data = {
        'mime_type': mime_type,
        'encoding': encoding,
        'filename': basename(url_path),
    }

    minio_scheme = 'https' if settings.AWS_S3_SECURE_URLS else 'http'
    minio_url_prefix = f"{minio_scheme}://{settings.AWS_S3_CUSTOM_DOMAIN}"

    if url.startswith(minio_url_prefix):  # media file from minio
        url = url.replace(
            minio_url_prefix,
            settings.AWS_S3_ENDPOINT_URL + settings.AWS_STORAGE_BUCKET_NAME)

    elif url_path.startswith(settings.MEDIA_URL):  # media file from filesystem
        path = url_path.replace(settings.MEDIA_URL, settings.MEDIA_ROOT)
        data['file_obj'] = default_storage.open(path)
        return data

    elif url_path.startswith(settings.STATIC_URL):
        path = url_path.replace(settings.STATIC_URL, '')
        data['file_obj'] = open(find(path), 'rb')
        return data

    # fall back to weasyprint default fetcher
    return weasyprint.default_url_fetcher(url)
Esempio n. 4
0
    def _my_fetcher(url):
        """Fetch svg/png image files in sunmoontide/graphics/ for html source url
        references of the form: '<img src="graph:nameofimage.svg">'
        File extensions must be 'png' or 'svg'.
        """
        if url.startswith('graph:'):
            try:
                g = pkgutil.get_data('cal_pages',
                                     'graphics/{}'.format(url[6:]))
            except Exception as e:
                print('Could not find a graphic for the About the Calendar \
    page. Expected: sunmoontide/graphics/{}'.format(url[6:]))
                raise IOError(e)

            if url.endswith('png'):
                mt = 'image/png'
            elif url.endswith('svg'):
                mt = 'image/svg+xml'
            else:
                raise IOError('Unknown file type referenced in \
    infopages/about.html - {} - Could not fetch this URL. Local image files must \
    have `.svg` or `.png` extensions.'.format(url))

            return dict(string=BytesIO(g).read(), mime_type=mt)

        else:
            return weasyprint.default_url_fetcher(url)
Esempio n. 5
0
def url_fetcher(individual, general, url, *args, **kwargs):
    if url.endswith('/individual-declaration.svg'):
        return {'file_obj': build_pie_chart(individual)}
    elif url.endswith('/general-declaration.svg'):
        return {'file_obj': build_pie_chart(general)}
    else:
        return default_url_fetcher(url, *args, **kwargs)
Esempio n. 6
0
def django_url_fetcher(url, *args, **kwargs):
    # load file:// paths directly from disk
    if url.startswith('file:'):
        mime_type, encoding = mimetypes.guess_type(url)
        url_path = urlparse(url).path
        data = {
            'mime_type': mime_type,
            'encoding': encoding,
            'filename': Path(url_path).name,
        }

        default_media_url = settings.MEDIA_URL in ('', get_script_prefix())
        if not default_media_url and url_path.startswith(settings.MEDIA_URL):
            media_root = settings.MEDIA_ROOT
            if isinstance(settings.MEDIA_ROOT, Path):
                media_root = f'{settings.MEDIA_ROOT}/'
            path = url_path.replace(settings.MEDIA_URL, media_root, 1)
            data['file_obj'] = default_storage.open(path)
            return data

        elif settings.STATIC_URL and url_path.startswith(settings.STATIC_URL):
            path = url_path.replace(settings.STATIC_URL, '', 1)
            data['file_obj'] = open(find(path), 'rb')
            return data

    # fall back to weasyprint default fetcher
    return weasyprint.default_url_fetcher(url, *args, **kwargs)
Esempio n. 7
0
def bika_url_fetcher(url):
    """Basically the same as the default_url_fetcher from WeasyPrint,
    but injects the __ac cookie to make an authenticated request to the resource.
    """
    from weasyprint import VERSION_STRING
    from weasyprint.compat import Request
    from weasyprint.compat import urlopen_contenttype

    request = api.get_request()
    __ac = request.cookies.get("__ac", "")

    if request.get_header("HOST") in url:
        result, mime_type, charset = urlopen_contenttype(
            Request(url,
                    headers={
                        'Cookie': "__ac={}".format(__ac),
                        'User-Agent': VERSION_STRING,
                        'Authorization': request._auth,
                    }))
        return dict(file_obj=result,
                    redirected_url=result.geturl(),
                    mime_type=mime_type,
                    encoding=charset)

    return default_url_fetcher(url)
Esempio n. 8
0
 def custom_fetcher(url):
     if url.startswith('img:'):
         return dict(file_obj=open(
             'faqbot/static/visa/' + url.split(':')[-1], 'rb'),
                     mime_type='image/png')
     else:
         return default_url_fetcher(url)
Esempio n. 9
0
 def _my_fetcher(url):
     """Fetch svg/png image files in sunmoontide/graphics/ for html source url
     references of the form: '<img src="graph:nameofimage.svg">'
     File extensions must be 'png' or 'svg'.
     """
     if url.startswith('graph:'):
         try:
             g = pkgutil.get_data('cal_pages', 'graphics/{}'.format(url[6:]))
         except Exception as e:
             print('Could not find a graphic for the About the Calendar \
 page. Expected: sunmoontide/graphics/{}'.format(url[6:]))
             raise IOError(e)
         
         if url.endswith('png'):
             mt = 'image/png'
         elif url.endswith('svg'):
             mt = 'image/svg+xml'
         else:
             raise IOError('Unknown file type referenced in \
 infopages/about.html - {} - Could not fetch this URL. Local image files must \
 have `.svg` or `.png` extensions.'.format(url))
 
         return dict(string = BytesIO(g).read(), mime_type = mt)
         
     else:
         return weasyprint.default_url_fetcher(url)
def my_fetcher(url):

    if url.startswith('https://getpocket.com'):
        r = requests.get(url, cookies=cookies)
        return r.text
    else:
        return weasyprint.default_url_fetcher(url)
Esempio n. 11
0
def url_fetcher(url):
    """
    :type url: str
    """
    if url.startswith("file:///static/"):
        url = url.replace("file:///static/", "file://%s/" % get_base_url())

    return default_url_fetcher(url)
Esempio n. 12
0
 def attachment_fetcher(url):
     if url.startswith('cid:'):
         cid = url[4:]
         attachment = attachments[cid]
         return dict(string=base64.b64decode(attachment["data"]),
                     mime_type=attachment["type"])
     else:
         return weasyprint.default_url_fetcher(url)
Esempio n. 13
0
def url_fetcher(url):
    if url.startswith('assets://'):
        url = url[len('assets://'):]
        filepath = os.path.join(getattr(settings, 'ASSETS_ROOT', './assets/'), url)
        with open(filepath) as asset:
            contents = asset.read()
        return dict(string=contents, mime_type=mimetypes.guess_type(filepath)[0])
    else:
        return weasyprint.default_url_fetcher(url)
Esempio n. 14
0
    def fetcher(self, zpt, url):
        """
        Internal fetcher for WeasyPrint to access in-report resources such as images, css and js
        :param zpt: ReportFile
        :param url: url of te resource to fetch
        :return:
        """
        if url.startswith("http"):
            return default_url_fetcher(url)

        fallback = url
        # support for both file:// and relative urls
        if url.startswith('file://'):
            url = url[7:]

        if zpt.exists(url):
            return {'string': zpt.get(url).read()}

        return default_url_fetcher(fallback)
Esempio n. 15
0
    def url_fetcher(self, url):
        """Fetches internal URLs by path and not via an external request.

        N.B. Multiple calls to this method might exhaust the available threads
             of the server, which causes a hanging instance.
        """
        if url.startswith("data"):
            logger.info("Data URL, delegate to default URL fetcher...")
            return default_url_fetcher(url)

        logger.info("Fetching URL '{}' for WeasyPrint".format(url))

        # get the pyhsical path from the URL
        request = api.get_request()
        host = request.get_header("HOST")
        path = "/".join(request.physicalPathFromURL(url))

        # fetch the object by sub-request
        portal = api.get_portal()
        context = portal.restrictedTraverse(path, None)

        if context is None or host not in url:
            logger.info("External URL, delegate to default URL fetcher...")
            return default_url_fetcher(url)

        logger.info("Local URL, fetching data by path '{}'".format(path))

        # get the data via an authenticated subrequest
        response = subrequest(path)

        # Prepare the return data as required by WeasyPrint
        string = response.getBody()
        filename = url.split("/")[-1]
        mime_type = mimetypes.guess_type(url)[0]
        redirected_url = url

        return {
            "string": string,
            "filename": filename,
            "mime_type": mime_type,
            "redirected_url": redirected_url,
        }
Esempio n. 16
0
 def fetcher(url):
     if url == 'weasyprint-custom:foo/%C3%A9_%e9_pattern':
         return {'string': pattern_png, 'mime_type': 'image/png'}
     elif url == 'weasyprint-custom:foo/bar.css':
         return {
             'string': 'body { background: url(é_%e9_pattern)',
             'mime_type': 'text/css'}
     elif url == 'weasyprint-custom:foo/bar.no':
         return {
             'string': 'body { background: red }',
             'mime_type': 'text/no'}
     else:
         return default_url_fetcher(url)
Esempio n. 17
0
def local_fetcher(url, root_dir=None, template_dir=None, cwd=None):
    if url.startswith(f'file://{cwd}'):
        rel_path = os.path.relpath(url[7:], cwd)
        new_path = os.path.join(
            os.path.abspath(root_dir),
            rel_path,
        )
        url = f'file://{new_path}'
    elif url.startswith(f'file://{os.path.abspath(os.path.join(root_dir, template_dir))}'):
        count = url.count(template_dir)
        if url.count(template_dir) > 1:
            url = url.replace(f'{template_dir}/', '', count - 1)
    return default_url_fetcher(url)
Esempio n. 18
0
def image_scaling_fetcher(url, image_size):
    if url.startswith(LOCAL_PROTOCOL):
        with open(url.replace(LOCAL_PROTOCOL, ''), 'rb') as img_file:
            img = Image.open(img_file)
            return {
                'mime_type': 'image/jpeg',
                'file_obj': scale_image(img, image_size)
            }

    fetched = weasyprint.default_url_fetcher(url)
    if fetched.get('mime_type').startswith('image'):
        img = Image.open(fetched['file_obj'])
        fetched['file_obj'] = scale_image(img, image_size)
    return fetched
Esempio n. 19
0
    def fetch_url(self, url):
        """A WeasyPrint URL fetcher.

        See class documentation for how a resource is located.
        It either uses the url_fetcher of the object (if provided) or otherwise the resources dict as discussed above.

        Args:
            url (str): The URL of the resource to fetch.

        Returns:
            dict: A dict as defined in the URL fetcher docs of WeasyPrint (string or file_obj, mime_type etc.).
        """
        if self.url_fetcher is not None:
            return self.url_fetcher(url, **self.fetcher_args)
        elif url.startswith('file://'):
            if not self.allow_files:
                raise ValueError('Access denied to local file "%s"' % str(url))
            else:
                return default_url_fetcher(url, **self.fetcher_args)
        elif url.startswith('resource:'):
            key = url[9:]
            if key not in self.resources:
                raise ValueError('Resource "%s" not found' % key)
            value = self.resources[key]
            if callable(value):
                value = value(key)
            return value
        elif self.fallback_default:
            return default_url_fetcher(url, **self.fetcher_args)
        else:
            raise ValueError('Invalid url: "%s"' % url)

#import sys

#r = WeasyRenderer(filename=sys.argv[1])
#r.render(None, 'test.pdf')
Esempio n. 20
0
 def report_fetcher(url):
     logger.debug('Getting url for weasyprint {}'.format(url))
     if url.startswith('stock://'):
         imagePath = stock.getStockImagePath(url[8:])
         with open(imagePath, 'rb') as f:
             image = f.read()
         return dict(string=image, mime_type='image/png')
     elif url.startswith('image://'):
         img = ''  # Empty image
         if isinstance(images, dict):
             img = images.get(url[8:], None)
             logger.debug('Getting image {}? {}'.format(
                 url[8:], img is not None))
         return dict(string=img, mime_type='image/png')
     else:
         return default_url_fetcher(url)
Esempio n. 21
0
 def report_fetcher(url):
     logger.debug('Getting url for weasyprint {}'.format(url))
     if url.startswith('stock://'):
         imagePath = stock.getStockImagePath(url[8:])
         with open(imagePath, 'rb') as f:
             image = f.read()
         return dict(string=image,
                     mime_type='image/png')
     elif url.startswith('image://'):
         if isinstance(images, dict):
             img = images.get(url[8:], None)
             logger.debug('Getting image {}? {}'.format(url[8:], img != None))
         return dict(string=img,
                     mime_type='image/png')
     else:
         return default_url_fetcher(url)
Esempio n. 22
0
    def _url_fetcher(self, url):
        if url.startswith('static://'):
            url = url[len('static://'):]
            url = "file://" + finders.find(url)
            #url = finders.find(url)
            #print(url)

        if url.startswith('media://'):
            url = url[len('media://'):]
            url = "file://" + os.path.join(settings.MEDIA_ROOT, url)

        if url.startswith('imgpdf://'):
            url = url[len('imgpdf://'):]
            url = os.path.join(settings.DOMINIO, url)
            print(url)

        return default_url_fetcher(url)
Esempio n. 23
0
        def report_fetcher(url: str) -> typing.Dict:
            logger.debug('Getting url for weasyprint %s', url)
            if url.startswith('stock://'):
                imagePath = stock.getStockImagePath(url[8:])
                with open(imagePath, 'rb') as f:
                    image = f.read()
                return dict(string=image, mime_type='image/png')

            if url.startswith('image://'):
                img: typing.Optional[bytes] = b''  # Empty image
                if images:
                    img = images.get(url[8:])
                    logger.debug('Getting image %s? %s', url[8:], img
                                 is not None)
                return dict(string=img, mime_type='image/png')

            return default_url_fetcher(url)
Esempio n. 24
0
def django_url_fetcher(url, *args, **kwargs):
    """Helper from django-weasyprint"""
    # load file:// paths directly from disk
    if url.startswith("file:"):
        mime_type, encoding = mimetypes.guess_type(url)
        parsed_url = urlparse(url)

        data = {
            "mime_type": mime_type,
            "encoding": encoding,
            "filename": parsed_url.netloc,
        }
        # try to find in media storage
        if default_storage.exists(parsed_url.netloc):
            data["file_obj"] = default_storage.open(parsed_url.netloc)
            return data

    # fall back to weasyprint default fetcher
    return weasyprint.default_url_fetcher(url, *args, **kwargs)
Esempio n. 25
0
 def flask_url_fetcher(url):
     redirect_chain = set()
     while 1:
         if not url.startswith(url_root):
             return weasyprint.default_url_fetcher(url)
         response = client.get(url[len(url_root):], base_url=url_root)
         if response.status_code == 200:
             return dict(
                 string=response.data,
                 mime_type=response.mimetype,
                 encoding=response.charset,
                 redirected_url=url)
         # The test client can follow redirects, but do it ourselves
         # to get access to the redirected URL.
         elif response.status_code in (301, 302, 303, 305, 307):
             redirect_chain.add(url)
             url = response.location
             if url in redirect_chain:
                 raise ClientRedirectError('loop detected')
         else:
             raise ValueError('Flask-WeasyPrint got HTTP status %s for %s'
                              % (response.status, url))
Esempio n. 26
0
def django_url_fetcher(url):
    # load file:// paths directly from disk
    if url.startswith('file:'):
        mime_type, encoding = mimetypes.guess_type(url)
        url_path = parse.urlparse(url).path
        data = {
            'mime_type': mime_type,
            'encoding': encoding,
            'filename': basename(url_path),
        }

        if url_path.startswith(settings.MEDIA_URL):
            path = url_path.replace(settings.MEDIA_URL, settings.MEDIA_ROOT)
            data['file_obj'] = default_storage.open(path)
            return data

        elif url_path.startswith(settings.STATIC_URL):
            path = url_path.replace(settings.STATIC_URL, '')
            data['file_obj'] = open(find(path), 'rb')
            return data

    # fall back to weasyprint default fetcher
    return weasyprint.default_url_fetcher(url)
Esempio n. 27
0
def django_url_fetcher(url, *args, **kwargs):
    # load file:// paths directly from disk
    if url.startswith('file:'):
        mime_type, encoding = mimetypes.guess_type(url)
        url_path = urlparse(url).path
        data = {
            'mime_type': mime_type,
            'encoding': encoding,
            'filename': Path(url_path).name,
        }

        if settings.MEDIA_URL and url_path.startswith(settings.MEDIA_URL):
            path = url_path.replace(settings.MEDIA_URL, settings.MEDIA_ROOT)
            data['file_obj'] = default_storage.open(path)
            return data

        elif settings.STATIC_URL and url_path.startswith(settings.STATIC_URL):
            path = url_path.replace(settings.STATIC_URL, '')
            data['file_obj'] = open(find(path), 'rb')
            return data

    # fall back to weasyprint default fetcher
    return weasyprint.default_url_fetcher(url, *args, **kwargs)
Esempio n. 28
0
def render(html):
    """Convert HTML to a PDF"""

    output = io.BytesIO()
    surface = cairo.PDFSurface(output, 595, 842)
    ctx = cairo.Context(surface)
    cffictx = cairocffi.Context._from_pointer(cairocffi.ffi.cast('cairo_t **', id(ctx) + object.__basicsize__)[0], incref=True)

    html = etree.parse(io.StringIO(html), etree.HTMLParser())

    for pdf in html.xpath("//img[substring(@src, string-length(@src) - 3)=\'.pdf\']"):
        for prev in pdf.xpath("preceding-sibling::*"):
            pdf.getparent().remove(prev)
        pdfsrc = pdf.get("src")
        pdf.getparent().remove(pdf)
        section = deepcopy(html)
        for nextpdf in section.xpath("//img[substring(@src, string-length(@src) - 3)=\'.pdf\']"):
            for nextel in nextpdf.xpath("following-sibling::*"):
                nextpdf.getparent().remove(nextel)
            nextpdf.getparent().remove(nextpdf)

        html_pages = weasyprint.HTML(tree=section).render().pages
        surface.set_size(html_pages[0].width * 72 / 96.0, html_pages[0].height * 72 / 96.0)

        if pdfsrc != "blank.pdf":
            with weasyprint.default_url_fetcher(str(pdfsrc))['file_obj'] as fetch:
                pdf_pages = Poppler.Document.new_from_stream(Gio.MemoryInputStream.new_from_bytes(GLib.Bytes.new_take(fetch.read())), -1, None, None)
        else:
            pdf_pages = None
        for pageno in range(max(pdf_pages.get_n_pages() if pdf_pages else 0, len(html_pages))):
            if pdf_pages and pageno < pdf_pages.get_n_pages():
                pdf_pages.get_page(pageno).render_for_printing(ctx)
            if pageno < len(html_pages):
                html_pages[pageno].paint(cffictx, scale=72 / 96.0)
            ctx.show_page()
    surface.finish()
    return output.getbuffer()
Esempio n. 29
0
def url_fetcher(url):

    import weasyprint
    original_url = url
    url = url

    try:
        if url.startswith('assets://'):
            url = url[len('assets://'):]
            with open(os.path.join(settings.ASSETS_ROOT, url), 'rb') as asset:
                contents = asset.read()
            return dict(string=contents)
        elif url.startswith('static://'):
            url = url[len('static://'):]
            with open(finders.find(url, all=False), 'rb') as asset:
                contents = asset.read()
            return dict(string=contents)

        # TODO: untested ! test me !!!
        elif url.startswith('media://'):
            url = url[len('media://'):]
            with open(os.path.join(settings.MEDIA_ROOT, url), 'rb') as asset:
                contents = asset.read()
            return dict(string=contents)

        elif url.startswith('file:///media/'):
            url = url[len('file:///media/'):]
            with open(os.path.join(settings.MEDIA_ROOT, url), 'rb') as asset:
                contents = asset.read()
            return dict(string=contents)
    except Exception as e:
        trace('Error fetching "%s"' % original_url)
        trace(str(e))
        raise

    return weasyprint.default_url_fetcher(url)
Esempio n. 30
0
def font_fetcher(url):
    if url.startswith('fonts/'):
        font_path = finders.find(url)
        font_file = open(font_path, 'r')
        return {'file_obj': font_file}
    return default_url_fetcher(url)
Esempio n. 31
0
def django_url_fetcher(url: str):
    """Returns the file for a given URL.

    If the URL appear to be a static file, we will attempt to load it internally.
    Otherwise, it will resolve normally as an external URL.

    Relative URLs are only supported

    The default behaviour will fetch any http(s) files normally, and will
    also attempt to resolve staticfiles internally (this should mostly
    affect development scenarios, but also works if static files are served
    under a relative URL).

    Returns a dictionary with two entries: ``string``, which is the
    resources data as a string and ``mime_type``, which is the identified
    mime type for the resource.
    """
    # If the URL looks like a staticfile, try to load it as such.
    # Reading it from the storage avoids a network call in many cases (unless the
    # storage is remote, in which case this improves nothing:
    try:
        if url.startswith(staticfiles_storage.base_url):
            filename = url.replace(staticfiles_storage.base_url, "", 1)
            data = None

            path = finders.find(filename)
            if path:
                # Read static files from source (e.g.: the file that's bundled with the
                # Django app that provides it.
                # This also picks up uncollected staticfiles (useful when developing /
                # in DEBUG mode).
                with open(path, "rb") as f:
                    data = f.read()
            else:
                # File was not found by a finder. This commonly happens when running in
                # DEBUG=True with a storage that uses Manifests or alike, since the
                # filename won't match with the source file.
                # In these cases, use the _storage_ to find the file instead:
                with staticfiles_storage.open(filename) as f:
                    data = f.read()

            return {
                "mime_type": mimetypes.guess_type(url)[0],
                "string": data,
            }
    except (ValueError, FileNotFoundError):
        # Looks like this wasn't a staticfile (or maybe it was a missing one?)
        # Let it resolve as a normal URL.
        pass

    try:
        # If the URL is a relative URL, use Django's resolver to figure out how Django
        # would serve this.
        #
        # This should cover all those funky scenarios like:
        # - Custom views that serve dynamically generated files.
        # - Media files (if serving them via Django, which is not recommended).
        if url.startswith("/"):
            view, args, kwargs = resolve(url)
            kwargs["request"] = HttpRequest
            kwargs["request"].method = "GET"
            response = view(*args, **kwargs)

            return {
                "mime_type": mimetypes.guess_type(url)[0],
                "string": response.content,
            }
    except Resolver404 as e:
        raise InvalidRelativeUrl(f"No view matched `{url}`.") from e

    return default_url_fetcher(url)
Esempio n. 32
0
def my_fetcher(url):
    if url.startswith("//"):
        url = "https:" + url
    elif url.startswith("file:///"):
        url = "https:" + url[len("file:/"):]
    return weasyprint.default_url_fetcher(url)
Esempio n. 33
0
def senaite_url_fetcher(url):
    """Uses plone.subrequest to fetch an internal image resource.

    If the URL points to an external resource, the URL is handed
    to weasyprint.default_url_fetcher.

    Please see these links for details:

        - https://github.com/plone/plone.subrequest
        - https://pypi.python.org/pypi/plone.subrequest
        - https://github.com/senaite/senaite.core/issues/538

    :returns: A dict with the following keys:

        * One of ``string`` (a byte string) or ``file_obj``
          (a file-like object)
        * Optionally: ``mime_type``, a MIME type extracted e.g. from a
          *Content-Type* header. If not provided, the type is guessed from the
          file extension in the URL.
        * Optionally: ``encoding``, a character encoding extracted e.g. from a
          *charset* parameter in a *Content-Type* header
        * Optionally: ``redirected_url``, the actual URL of the resource
          if there were e.g. HTTP redirects.
        * Optionally: ``filename``, the filename of the resource. Usually
          derived from the *filename* parameter in a *Content-Disposition*
          header

        If a ``file_obj`` key is given, it is the caller’s responsibility
        to call ``file_obj.close()``.
    """

    logger.info("Fetching URL '{}' for WeasyPrint".format(url))

    # get the pyhsical path from the URL
    request = api.get_request()
    host = request.get_header("HOST")
    path = "/".join(request.physicalPathFromURL(url))

    # fetch the object by sub-request
    portal = api.get_portal()
    context = portal.restrictedTraverse(path, None)

    # We double check here to avoid an edge case, where we have the same path
    # as well in our local site, e.g. we have `/senaite/img/systems/senaite.png`,
    # but the user requested http://www.ridingbytes.com/img/systems/senaite.png:
    #
    # "/".join(request.physicalPathFromURL("http://www.ridingbytes.com/img/systems/senaite.png"))
    # '/senaite/img/systems/senaite.png'
    if context is None or host not in url:
        logger.info(
            "URL is external, passing over to the default URL fetcher...")
        return default_url_fetcher(url)

    logger.info(
        "URL is local, fetching data by path '{}' via subrequest".format(path))

    # get the data via an authenticated subrequest
    response = subrequest(path)

    # Prepare the return data as required by WeasyPrint
    string = response.getBody()
    filename = url.split("/")[-1]
    mime_type = mimetypes.guess_type(url)[0]
    redirected_url = url

    return {
        "string": string,
        "filename": filename,
        "mime_type": mime_type,
        "redirected_url": redirected_url,
    }
Esempio n. 34
0
 def _fetcher(url):
     return weasyprint.default_url_fetcher(url, timeout=1)
Esempio n. 35
0
def url_fetcher(url):
    if url.startswith("assets://"):
        url = url[len("assets://"):]
        url = "file://" + safe_join(settings.ASSETS_ROOT, url)
    return weasyprint.default_url_fetcher(url)
Esempio n. 36
0
 def url_fetcher(url, timeout=10, ssl_context=None):
     return weasyprint.default_url_fetcher(
         "file://" + url.replace("file://", settings.BASE_DIR), timeout,
         ssl_context)