Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Postal Owl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
The Postal Owl
Postal Owl
Commits
9a44de7e
Commit
9a44de7e
authored
2 years ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
We wrap Flask to store uploaded files in our temporary directory
Heavily inspired by a similar hack in OSMO.
parent
e44ee585
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
owl/__init__.py
+31
-0
31 additions, 0 deletions
owl/__init__.py
with
31 additions
and
0 deletions
owl/__init__.py
+
31
−
0
View file @
9a44de7e
...
...
@@ -7,6 +7,7 @@ import logging
import
os
from
sqlalchemy
import
select
from
sqlalchemy.orm
import
joinedload
import
tempfile
import
werkzeug.exceptions
import
owl.assets
as
assets
...
...
@@ -14,6 +15,35 @@ import owl.config as config
import
owl.db
as
db
# We wrap Flask to make it store uploaded files in a directory of our choice.
# This enables us to hard-link the uploaded files to their final place.
# To achieve that, we subclass Request to make it use a subclassed FormDataParser,
# which calls a custom stream factory.
def
owl_stream_factory
(
total_content_length
,
filename
,
content_type
,
content_length
=
None
):
return
tempfile
.
NamedTemporaryFile
(
dir
=
os
.
path
.
join
(
app
.
instance_path
,
'
tmp
'
),
prefix
=
'
upload-
'
)
class
FormDataParser
(
werkzeug
.
formparser
.
FormDataParser
):
def
__init__
(
self
,
stream_factory
=
None
,
charset
=
'
utf-8
'
,
errors
=
'
replace
'
,
max_form_memory_size
=
None
,
max_content_length
=
None
,
cls
=
None
,
silent
=
True
):
super
().
__init__
(
owl_stream_factory
,
charset
,
errors
,
max_form_memory_size
,
max_content_length
,
cls
,
silent
)
class
Request
(
flask
.
wrappers
.
Request
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
self
.
form_data_parser_class
=
FormDataParser
class
OwlFlask
(
Flask
):
"""
...
...
@@ -106,6 +136,7 @@ static_dir = os.path.abspath('static')
# Application object
app
=
OwlFlask
(
__name__
,
static_folder
=
static_dir
,
instance_path
=
data_dir
)
app
.
config
.
from_object
(
config
)
app
.
request_class
=
Request
db
.
flask_db
=
SQLAlchemy
(
app
,
metadata
=
db
.
metadata
)
...
...
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