Skip to content

Commit a39c398

Browse files
authored
Improve alt text handling (#815)
Also updates to support staging server deployment.
1 parent b9b1c83 commit a39c398

File tree

7 files changed

+56
-14
lines changed

7 files changed

+56
-14
lines changed

.github/workflows/test-deploy.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ env:
44
build_number: ${{ github.run_id }}
55

66
concurrency:
7-
group: production
7+
group: test-deploy
88
cancel-in-progress: true
99

1010
on:
@@ -92,6 +92,24 @@ jobs:
9292
docker compose -f release/docker-compose.yml down
9393
docker container prune -f
9494
95+
staging:
96+
needs: [test]
97+
runs-on: ubuntu-latest
98+
environment: staging
99+
if: success()
100+
steps:
101+
- name: Login to Docker Hub
102+
uses: docker/login-action@v3
103+
with:
104+
username: ${{ vars.DOCKERHUB_USERNAME }}
105+
password: ${{ secrets.DOCKERHUB_TOKEN }}
106+
107+
- name: Pull and tag web image
108+
run: |
109+
docker pull mltshp/mltshp-web:build-${{ env.build_number }}
110+
docker tag mltshp/mltshp-web:build-${{ env.build_number }} mltshp/mltshp-web:staging
111+
docker push mltshp/mltshp-web:staging
112+
95113
deploy:
96114
needs: [test, build-worker]
97115
runs-on: ubuntu-latest

handlers/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def finish(self, chunk=None):
4747
self.set_header('x-proc-time',"%s" % (proc_time))
4848
super(BaseHandler, self).finish(chunk)
4949

50+
def static_url(self, *args, **kwargs):
51+
base_url = ""
52+
if options.use_cdn:
53+
base_url = "https://%s" % options.cdn_host
54+
return base_url + super(BaseHandler, self).static_url(*args, **kwargs)
55+
5056
def get_current_user(self):
5157
sid = self.get_secure_cookie(SESSION_COOKIE)
5258
if sid:
@@ -68,6 +74,7 @@ def render_string(self, template_name, **kwargs):
6874
current_user_object = self.get_current_user_object()
6975
kwargs['errors'] = self._errors
7076
kwargs['settings'] = self.settings
77+
kwargs['host_banner'] = options.host_banner
7178
kwargs['app_host'] = options.app_host or self.request.host
7279
kwargs['app_scheme'] = (options.use_cdn and options.cdn_host and "https") or "http"
7380
kwargs['cdn_host'] = options.cdn_host

mltshpoptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def parse_dictionary(settings):
3030
define('best_of_user_name', default='mltshp', help="User name where Best Of images are saved.")
3131
define('superuser_list', default='', help='Comma separated list of users who are superuser accounts')
3232
define('moderator_list', default='', help='Comma separated list of users who are moderator accounts')
33+
define('host_banner', default='', help='Shown in header when present')
3334

3435
# infrastructure
3536
define('database_host', metavar="HOST", help="Hostname for database connection")

models/sharedfile.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ def get_alt_text(self, raw=False):
9797
for rendering alt text inside textarea fields.
9898
"""
9999
alt_text = self.alt_text
100-
if not alt_text:
100+
if alt_text is None:
101101
alt_text = ''
102102

103103
if not raw:
104+
alt_text = escape.xhtml_escape(alt_text)
104105
alt_text = alt_text.replace('\n', '<br>')
105106

106-
return alt_text
107+
return alt_text.strip()
107108

108109
def save(self, *args, **kwargs):
109110
"""
@@ -614,11 +615,12 @@ def thumbnail_url(self):
614615
if sourcefile.type == 'image':
615616
if self.original_id > 0:
616617
original = self.original()
617-
size = original.size
618+
if original:
619+
size = original.size
618620
else:
619621
size = self.size
620622
# Fastly I/O won't process images > 50mb, so condition for that
621-
if sourcefile.type == 'image' and options.use_fastly and size < 50_000_000:
623+
if sourcefile.type == 'image' and options.use_fastly and size > 0 and size < 50_000_000:
622624
return f"https://{options.cdn_host}/r/{self.share_key}?width=100"
623625
else:
624626
return s3_url(options.aws_key, options.aws_secret, options.aws_bucket, \
@@ -633,11 +635,11 @@ def small_thumbnail_url(self):
633635
if sourcefile.type == 'image':
634636
if self.original_id > 0:
635637
original = self.original()
636-
size = original.size
638+
if original: size = original.size
637639
else:
638640
size = self.size
639641
# Fastly I/O won't process images > 50mb, so condition for that
640-
if sourcefile.type == 'image' and options.use_fastly and size < 50_000_000:
642+
if sourcefile.type == 'image' and options.use_fastly and size > 0 and size < 50_000_000:
641643
return f"https://{options.cdn_host}/r/{self.share_key}?width=240&height=184&fit=bounds"
642644
else:
643645
return s3_url(options.aws_key, options.aws_secret, options.aws_bucket, \

setup/production/nginx.conf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ http {
7070

7171
server {
7272
listen 80;
73-
server_name mltshp.com www.mltshp.com s.mltshp.com mltshp-cdn.com;
73+
server_name mltshp.com www.mltshp.com s.mltshp.com mltshp-cdn.com stage.mltshp.com s.stage.mltshp.com stage.mltshp-cdn.com;
7474

7575
# Allow file uploads
7676
client_max_body_size 30M;
@@ -82,13 +82,17 @@ http {
8282
# Main domain shouldn't be serving /s3, mltshp-cdn.com should
8383
rewrite ^/s3/(.*)$ https://mltshp-cdn.com/s3/$1 permanent;
8484
}
85+
if ($host = 'stage.mltshp.com' ) {
86+
# Main domain shouldn't be serving /s3, mltshp-cdn.com should
87+
rewrite ^/s3/(.*)$ https://stage.mltshp-cdn.com/s3/$1 permanent;
88+
}
8589

8690
# Serve a static error page in case the app is offline
8791
error_page 502 503 504 /static/50x.html;
8892

8993
location = /r/([a-zA-Z0-9\.]+) {
9094
proxy_pass_header Surrogate-Control;
91-
valid_referers none blocked www.mltshp.com mltshp.com;
95+
valid_referers none blocked www.mltshp.com mltshp.com stage.mltshp.com;
9296
if ($invalid_referer) {
9397
return 403;
9498
}

templates/base.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
<link type="text/css" rel="stylesheet" href="{{ static_url("css/main.min.css") }}">
1111

12-
<link rel="dns-prefetch" href="//s.mltshp.com">
13-
<link rel="dns-prefetch" href="//mltshp-cdn.com">
12+
<link rel="dns-prefetch" href="//s.{{ app_host }}">{% if cdn_host %}
13+
<link rel="dns-prefetch" href="//{{ cdn_host }}">{% end %}
1414

1515
<!-- Behave more like an app when saved to home screen in iOS -->
1616
<meta name="mobile-web-app-capable" content="yes">
@@ -132,6 +132,11 @@
132132
We’re doing something cool with the database!
133133
</li>
134134
{% end %}
135+
{% if host_banner %}
136+
<li class="user-nav--item">
137+
{{ escape(host_banner) }}
138+
</li>
139+
{% end %}
135140
{% if current_user_object and not current_user_object.email_confirmed %}
136141
{% if not site_is_readonly %}
137142
<li class="user-nav--item">
@@ -150,6 +155,11 @@
150155
</li>
151156
{% else %}
152157
{% set handler_class = handler.__class__.__name__ %}
158+
{% if host_banner %}
159+
<li class="user-nav--item">
160+
{{ escape(host_banner) }}
161+
</li>
162+
{% end %}
153163
<li class="user-nav--item">
154164
<a href="/sign-in?next={{request.path}}" class="user-nav--link">sign in</a>
155165
</li>

templates/uimodules/image.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ <h1>{{escape(sharedfile.get_title())}}</h1>
9898
{% end %}
9999
</div>
100100
<div id="image-content-footer-{{sharedfile.share_key}}" class="image-content-footer">
101-
<div class="alt-text {% if can_edit %}alt-text-edit{%end%}{% if not sharedfile.get_alt_text() %} alt-text--blank{%else%} alt-text--hidden{%end%}">
101+
<div class="alt-text {% if can_edit %}alt-text-edit{%end%}{% if sharedfile.get_alt_text() == "" %} alt-text--blank{%else%} alt-text--hidden{%end%}">
102102
<div class="alt-text-toggle link link--primary">alt text</div>
103103
{% if can_edit and not site_is_readonly %}
104-
{% if not sharedfile.get_alt_text() %}
104+
{% if sharedfile.get_alt_text() == "" %}
105105
<div class="the-alt-text" role="button">add some alt text</div>
106106
{% else %}
107107
<div class="the-alt-text" role="button">{{sharedfile.get_alt_text()}}</div>
@@ -120,7 +120,7 @@ <h3>Edit alt text</h3>
120120
</div>
121121
</form>
122122
{% else %}
123-
{% if sharedfile.get_alt_text() %}
123+
{% if sharedfile.get_alt_text() != "" %}
124124
<div class="the-alt-text">{{sharedfile.get_alt_text()}}</div>
125125
{% end %}
126126
{% end %}

0 commit comments

Comments
 (0)