Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
929160e1
Commit
929160e1
authored
12 years ago
by
Vik Paruchuri
Browse files
Options
Downloads
Patches
Plain Diff
Add in ability to validate images and image paths (urls)
parent
5d4ad4b8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
common/lib/xmodule/xmodule/open_ended_image_submission.py
+59
-1
59 additions, 1 deletion
common/lib/xmodule/xmodule/open_ended_image_submission.py
with
59 additions
and
1 deletion
common/lib/xmodule/xmodule/open_ended_image_submission.py
+
59
−
1
View file @
929160e1
from
PIL
import
Image
import
urlparse
TRUSTED_IMAGE_DOMAINS
=
[
'
wikipedia.com
'
,
...
...
@@ -6,6 +7,12 @@ TRUSTED_IMAGE_DOMAINS = [
'
wikipedia.org
'
]
ALLOWABLE_IMAGE_SUFFIXES
=
[
'
jpg
'
,
'
png
'
,
'
gif
'
]
MAX_IMAGE_DIM
=
150
MAX_COLORS_TO_COUNT
=
16
MAX_COLORS
=
5
...
...
@@ -25,5 +32,56 @@ class ImageProperties(object):
else
:
colors
=
len
(
colors
)
return
colors
<=
MAX_COLORS
def
get_skin_ratio
(
self
):
im
=
self
.
image
skin
=
sum
([
count
for
count
,
rgb
in
im
.
getcolors
(
im
.
size
[
0
]
*
im
.
size
[
1
])
if
rgb
[
0
]
>
60
and
rgb
[
1
]
<
(
rgb
[
0
]
*
0.85
)
and
rgb
[
2
]
<
(
rgb
[
0
]
*
0.7
)
and
rgb
[
1
]
>
(
rgb
[
0
]
*
0.4
)
and
rgb
[
2
]
>
(
rgb
[
0
]
*
0.2
)])
bad_color_val
=
float
(
skin
)
/
float
(
im
.
size
[
0
]
*
im
.
size
[
1
])
if
bad_color_val
>
.
4
:
is_okay
=
False
else
:
is_okay
=
True
return
is_okay
def
run_tests
(
self
):
image_is_okay
=
self
.
count_colors
()
and
self
.
get_skin_ratio
()
return
image_is_okay
class
URLProperties
(
object
):
def
__init__
(
self
,
url_string
):
self
.
url_string
=
url_string
def
check_if_parses
(
self
):
success
=
False
try
:
self
.
parsed_url
=
urlparse
.
urlparse
(
url_string
)
success
=
True
except
:
pass
return
success
def
check_suffix
(
self
):
good_suffix
=
False
for
suffix
in
ALLOWABLE_IMAGE_SUFFIXES
:
if
self
.
url_string
.
endswith
(
suffix
)
good_suffix
=
True
break
return
good_suffix
def
run_tests
(
self
):
url_is_okay
=
self
.
check_suffix
()
and
self
.
check_if_parses
()
return
url_is_okay
def
run_url_tests
(
url_string
):
url_properties
=
URLProperties
(
url_string
)
return
url_properties
.
run_tests
()
def
run_image_tests
(
image
):
image_properties
=
ImageProperties
(
image
)
return
image_properties
.
run_tests
()
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