Make OpenGraph URLs absolute

This commit is contained in:
rubenwardy 2020-04-23 23:49:46 +01:00
parent e94bd9b845
commit 57e7cbfd09
3 changed files with 23 additions and 7 deletions

@ -578,13 +578,25 @@ class Package(db.Model):
screenshot = self.screenshots.filter_by(approved=True).order_by(db.asc(PackageScreenshot.id)).first()
return screenshot.getThumbnailURL(level) if screenshot is not None else None
def getMainScreenshotURL(self):
def getMainScreenshotURL(self, absolute=False):
screenshot = self.screenshots.filter_by(approved=True).order_by(db.asc(PackageScreenshot.id)).first()
return screenshot.url if screenshot is not None else None
if screenshot is None:
return None
def getDetailsURL(self):
return url_for("packages.view",
author=self.author.username, name=self.name)
if absolute:
from app.utils import abs_url
return abs_url(screenshot.url)
else:
return screenshot.url
def getDetailsURL(self, absolute=False):
if absolute:
from app.utils import abs_url_for
return abs_url_for("packages.view",
author=self.author.username, name=self.name)
else:
return url_for("packages.view",
author=self.author.username, name=self.name)
def getEditURL(self):
return url_for("packages.create_edit",

@ -10,9 +10,9 @@
<meta name="og:title" content="{{ package.title }}"/>
<meta name="og:description" content="{{ package.short_desc }}"/>
<meta name="description" content="{{ package.short_desc }}"/>
<meta name="og:url" content="{{ package.getDetailsURL() }}"/>
<meta name="og:url" content="{{ package.getDetailsURL(absolute=True) }}"/>
{% if package.getMainScreenshotURL() %}
<meta name="og:image" content="{{ package.getMainScreenshotURL() }}"/>
<meta name="og:image" content="{{ package.getMainScreenshotURL(absolute=True) }}"/>
{% endif %}
{% endblock %}

@ -21,11 +21,15 @@ from flask_login import login_user, logout_user
from .models import *
from . import app
import random, string, os, imghdr
from urllib.parse import urljoin
def abs_url_for(path, **kwargs):
scheme = "https" if app.config["BASE_URL"][:5] == "https" else "http"
return url_for(path, _external=True, _scheme=scheme, **kwargs)
def abs_url(path):
return urljoin(app.config["BASE_URL"], path)
def get_int_or_abort(v, default=None):
try:
return int(v or default)