Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
TedImg - img.tedomum.net
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
3
Issues
3
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
TeDomum
TedImg - img.tedomum.net
Commits
d315f80e
Commit
d315f80e
authored
Dec 06, 2015
by
kaiyou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First working version for uploads
parent
6f848150
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
84 additions
and
11 deletions
+84
-11
runserver.py
runserver.py
+4
-2
tedimg/images.py
tedimg/images.py
+32
-4
tedimg/templates/error.html
tedimg/templates/error.html
+11
-0
tedimg/templates/index.html
tedimg/templates/index.html
+3
-3
tedimg/templates/show.html
tedimg/templates/show.html
+4
-0
tedimg/views.py
tedimg/views.py
+30
-2
No files found.
runserver.py
View file @
d315f80e
...
...
@@ -2,8 +2,10 @@ from tedimg import app
from
flask
import
send_from_directory
app
.
config
.
update
(
STORAGE_PATH
=
"./tedimg/static/images"
,
WEB_PATH
=
"/static/images"
FULL_STORAGE
=
"./tedimg/static/images"
,
THUMB_STORAGE
=
"./tedimg/static/images/thumb"
,
FULL_WEB
=
"static/images"
,
THUMB_WEB
=
"static/images/thumb"
)
app
.
run
(
debug
=
True
)
tedimg/images.py
View file @
d315f80e
from
tedimg
import
app
from
PIL
import
Image
from
os
import
path
import
os
import
binascii
def
get_image
(
name
):
def
get_image
(
root
,
name
):
""" Try and get basic image attributes.
"""
filename
=
path
.
basename
(
name
)
return
path
.
join
(
app
.
config
[
"WEB_PATH"
],
filename
)
filename
=
os
.
path
.
basename
(
name
)
return
(
root
+
os
.
path
.
join
(
app
.
config
[
"FULL_WEB"
],
filename
),
root
+
os
.
path
.
join
(
app
.
config
[
"THUMB_WEB"
],
filename
))
def
image_from_file
(
file_storage
):
""" Try and read the uploaded file.
"""
image
=
Image
.
open
(
file_storage
)
return
image
def
image_from_url
(
url
):
""" Try and download an image from the given url.
"""
def
save_with_thumbnail
(
image
,
filename
):
dest
=
"."
while
os
.
path
.
exists
(
os
.
path
.
join
(
app
.
config
[
"FULL_STORAGE"
],
dest
)):
filename
,
ext
=
os
.
path
.
splitext
(
filename
)
random
=
binascii
.
hexlify
(
os
.
urandom
(
3
)).
decode
(
'utf8'
)
dest
=
"%s-%s%s"
%
(
filename
,
random
,
ext
)
image
.
save
(
os
.
path
.
join
(
app
.
config
[
"FULL_STORAGE"
],
dest
))
thumb_size
=
app
.
config
[
"THUMB_SIZE"
]
image
.
thumbnail
((
thumb_size
,
thumb_size
))
image
.
save
(
os
.
path
.
join
(
app
.
config
[
"THUMB_STORAGE"
],
dest
))
return
dest
tedimg/templates/error.html
0 → 100644
View file @
d315f80e
{% extends "base.html" %}
{% block content %}
<div
class=
"section no-pad-bot"
id=
"index-banner"
>
<div
class=
"container"
>
<h1
class=
"red-text"
>
Upload failed!
</h1>
<h2>
{{ message }}
</h2>
<br><br>
</div>
</div>
{% endblock %}
tedimg/templates/index.html
View file @
d315f80e
...
...
@@ -2,11 +2,11 @@
{% block banner_content %}
<h1
class=
"header center orange-text"
>
Upload your image!
</h1>
<form
id=
"upload"
action=
"#
"
>
<form
method=
"post"
id=
"upload"
action=
"/upload"
enctype=
"multipart/form-data
"
>
<div
class=
"file-field input-field"
>
<div
class=
"btn light-blue"
>
<span>
File
</span>
<input
type=
"file"
>
<input
type=
"file"
name=
"file"
>
</div>
<div
class=
"file-path-wrapper"
>
<input
class=
"file-path validate"
type=
"text"
placeholder=
"Upload your image"
>
...
...
@@ -15,7 +15,7 @@
<div
class=
"input-field"
>
<i
class=
"material-icons prefix"
>
public
</i>
<input
id=
"icon_prefix"
type=
"text"
class=
"validate"
placeholder=
"Or paste your url"
>
<input
id=
"icon_prefix"
type=
"text"
class=
"validate"
name=
"url"
placeholder=
"Or paste your url"
>
</div>
</form>
{% endblock %}
tedimg/templates/show.html
View file @
d315f80e
...
...
@@ -4,6 +4,10 @@
<h1
class=
"header center orange-text"
>
Upload successful!
</h1>
<div
class=
"row center"
>
<a
href=
"{{ image }}"
class=
"btn-large waves-effect waves-light light-blue"
>
Direct link
</a>
<a
href=
"{{ thumb }}"
class=
"btn-large waves-effect waves-light light-blue"
>
Thumbnail
</a>
</div>
<div
class=
"row center"
>
<img
src=
"{{ thumb }}"
>
</div>
{% endblock %}
...
...
tedimg/views.py
View file @
d315f80e
from
tedimg
import
app
,
images
import
flask
import
os
@
app
.
route
(
'/'
)
...
...
@@ -10,5 +11,32 @@ def index():
@
app
.
route
(
'/show/<path:path>'
)
def
show
(
path
):
image
=
images
.
get_image
(
path
)
return
flask
.
render_template
(
"show.html"
,
image
=
image
)
root
=
flask
.
url_for
(
"index"
,
_external
=
True
)
image
,
thumb
=
images
.
get_image
(
root
,
path
)
return
flask
.
render_template
(
"show.html"
,
image
=
image
,
thumb
=
thumb
,
thumb_size
=
app
.
config
[
"THUMB_SIZE"
]
)
@
app
.
route
(
'/upload'
,
methods
=
[
'POST'
])
def
upload
():
uploaded
=
flask
.
request
.
files
[
'file'
]
url
=
flask
.
request
.
form
[
'url'
]
# Get an image object from the uploaded image or URL
try
:
if
uploaded
:
image
=
images
.
image_from_file
(
uploaded
)
filename
=
os
.
path
.
basename
(
uploaded
.
filename
)
elif
url
:
image
=
images
.
image_from_file
(
uploaded
)
filename
=
os
.
path
.
basename
(
uploaded
.
filename
)
else
:
return
flask
.
render_template
(
"error.html"
,
message
=
"Missing image."
)
except
Exception
as
error
:
raise
return
flask
.
render_template
(
"error.html"
,
message
=
"Could not read your image."
)
# Save the image to a local file
result
=
images
.
save_with_thumbnail
(
image
,
filename
)
return
flask
.
redirect
(
"/show/"
+
result
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment