Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
warehouse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julian
warehouse
Commits
3c47480d
Commit
3c47480d
authored
2 years ago
by
Julian
Browse files
Options
Downloads
Patches
Plain Diff
Refactor thumbnail code
parent
8750f266
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
warehouse/__init__.py
+3
-5
3 additions, 5 deletions
warehouse/__init__.py
warehouse/models.py
+10
-4
10 additions, 4 deletions
warehouse/models.py
warehouse/utils/__init__.py
+1
-10
1 addition, 10 deletions
warehouse/utils/__init__.py
with
14 additions
and
19 deletions
warehouse/__init__.py
+
3
−
5
View file @
3c47480d
...
...
@@ -6,7 +6,7 @@ from flask import Flask, render_template, request, redirect, url_for, Response,
from
requests_oauthlib
import
OAuth2Session
from
.models
import
db
,
Item
,
Location
,
Photo
from
.utils
import
render_pdf
,
print_pdf
,
qrcode_svg
,
render_markdown
,
send_thumbnail
from
.utils
import
render_pdf
,
print_pdf
,
qrcode_svg
,
render_markdown
app
=
Flask
(
__name__
,
instance_relative_config
=
True
)
app
.
config
.
from_pyfile
(
'
config.cfg
'
)
...
...
@@ -185,13 +185,11 @@ def photo_show(photo_id):
@app.route
(
'
/photos/<int:photo_id>/small
'
)
def
photo_small
(
photo_id
):
photo
=
Photo
.
query
.
get_or_404
(
photo_id
)
return
send_thumbnail
(
photo
.
image
,
130
,
100
)
return
Photo
.
query
.
get_or_404
(
photo_id
).
send_thumbnail
(
130
,
100
)
@app.route
(
'
/photos/<int:photo_id>/medium
'
)
def
photo_medium
(
photo_id
):
photo
=
Photo
.
query
.
get_or_404
(
photo_id
)
return
send_thumbnail
(
photo
.
image
,
390
,
300
)
return
Photo
.
query
.
get_or_404
(
photo_id
).
send_thumbnail
(
390
,
300
)
@app.route
(
'
/C/<code>
'
)
@app.route
(
'
/c/<code>
'
)
...
...
This diff is collapsed.
Click to expand it.
warehouse/models.py
+
10
−
4
View file @
3c47480d
import
secrets
import
os
import
io
from
flask
import
current_app
,
send_file
from
flask
import
current_app
,
send_file
,
Markup
from
flask_sqlalchemy
import
SQLAlchemy
from
PIL
import
Image
...
...
@@ -42,10 +43,15 @@ class Photo(db.Model):
path
=
os
.
path
.
join
(
current_app
.
config
[
'
UPLOAD_FOLDER
'
],
self
.
path
)
return
send_file
(
path
,
mimetype
=
self
.
mimetype
)
@property
def
image
(
self
):
def
send_thumbnail
(
self
,
max_width
,
max_height
):
path
=
os
.
path
.
join
(
current_app
.
config
[
'
UPLOAD_FOLDER
'
],
self
.
path
)
return
Image
.
open
(
path
,
formats
=
[
'
JPEG
'
])
buf
=
io
.
BytesIO
()
with
Image
.
open
(
path
,
formats
=
[
'
JPEG
'
])
as
img
:
scale
=
min
(
max_width
/
img
.
width
,
max_height
/
img
.
height
)
img
.
thumbnail
((
int
(
img
.
width
*
scale
),
int
(
img
.
height
*
scale
)))
img
.
save
(
buf
,
format
=
'
JPEG
'
,
quality
=
70
)
buf
.
seek
(
0
)
return
send_file
(
buf
,
mimetype
=
'
image/jpeg
'
)
def
token_typable
():
alphabet
=
'
23456789ABCDEFGHJKLMNPQRSTUVWX
'
# not '01OIYZ'
...
...
This diff is collapsed.
Click to expand it.
warehouse/utils/__init__.py
+
1
−
10
View file @
3c47480d
...
...
@@ -2,8 +2,7 @@ from .pdf import render_pdf
from
.ipp
import
print_pdf
from
.codes
import
qrcode_svg
import
io
from
flask
import
Markup
,
send_file
from
flask
import
Markup
import
markdown
import
bleach
...
...
@@ -17,11 +16,3 @@ def render_markdown(text, short=False):
result
=
result
.
split
(
'
<p>
'
,
1
)[
-
1
]
result
=
result
.
split
(
'
</p>
'
,
1
)[
0
]
return
Markup
(
result
)
def
send_thumbnail
(
img
,
max_width
,
max_height
):
scale
=
min
(
max_width
/
img
.
width
,
max_height
/
img
.
height
)
img
.
thumbnail
((
int
(
img
.
width
*
scale
),
int
(
img
.
height
*
scale
)))
buf
=
io
.
BytesIO
()
img
.
save
(
buf
,
format
=
'
JPEG
'
,
quality
=
70
)
buf
.
seek
(
0
)
return
send_file
(
buf
,
mimetype
=
'
image/jpeg
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment