Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Websites UFRPE
Wagtail Videos
Commits
c8f67197
Commit
c8f67197
authored
May 31, 2017
by
Tim Heap
Browse files
Fix long filename trimming logic
Instead of fixing anything it just entered an infinite loop.
parent
4b051e3d
Changes
2
Hide whitespace changes
Inline
Side-by-side
tests/test_admin_views.py
View file @
c8f67197
...
@@ -163,6 +163,21 @@ class TestVideoAddView(TestCase, WagtailTestUtils):
...
@@ -163,6 +163,21 @@ class TestVideoAddView(TestCase, WagtailTestUtils):
)
)
)
)
def
test_add_too_long_filename
(
self
):
video_file
=
create_test_video_file
()
name
=
'a_very_long_filename_'
+
(
'x'
*
100
)
+
'.mp4'
response
=
self
.
post
({
'title'
:
"Test video"
,
'file'
:
SimpleUploadedFile
(
name
,
video_file
.
read
(),
"video/mp4"
),
})
# Should be valid
self
.
assertEqual
(
response
.
status_code
,
302
)
video
=
Video
.
objects
.
get
()
self
.
assertEqual
(
len
(
video
.
file
.
name
),
Video
.
_meta
.
get_field
(
'file'
).
max_length
)
def
test_add_with_collections
(
self
):
def
test_add_with_collections
(
self
):
root_collection
=
Collection
.
get_first_root_node
()
root_collection
=
Collection
.
get_first_root_node
()
evil_plans_collection
=
root_collection
.
add_child
(
name
=
"Evil plans"
)
evil_plans_collection
=
root_collection
.
add_child
(
name
=
"Evil plans"
)
...
...
wagtailvideos/models.py
View file @
c8f67197
...
@@ -12,6 +12,7 @@ import tempfile
...
@@ -12,6 +12,7 @@ import tempfile
import
threading
import
threading
from
django.conf
import
settings
from
django.conf
import
settings
from
django.core.exceptions
import
SuspiciousFileOperation
from
django.core.files.base
import
ContentFile
from
django.core.files.base
import
ContentFile
from
django.core.urlresolvers
import
reverse
from
django.core.urlresolvers
import
reverse
from
django.db
import
models
from
django.db
import
models
...
@@ -126,12 +127,18 @@ class AbstractVideo(CollectionMember, index.Indexed, models.Model):
...
@@ -126,12 +127,18 @@ class AbstractVideo(CollectionMember, index.Indexed, models.Model):
def
get_upload_to
(
self
,
filename
):
def
get_upload_to
(
self
,
filename
):
folder_name
=
'original_videos'
folder_name
=
'original_videos'
filename
=
self
.
file
.
field
.
storage
.
get_valid_name
(
filename
)
filename
=
self
.
file
.
field
.
storage
.
get_valid_name
(
filename
)
max_length
=
self
.
_meta
.
get_field
(
'file'
).
max_length
# Truncate filename so it fits in the 100 character limit
# Truncate filename so it fits in the 100 character limit
# https://code.djangoproject.com/ticket/9893
# https://code.djangoproject.com/ticket/9893
while
len
(
os
.
path
.
join
(
folder_name
,
filename
))
>=
95
:
file_path
=
os
.
path
.
join
(
folder_name
,
filename
)
prefix
,
dot
,
extension
=
filename
.
rpartition
(
'.'
)
too_long
=
len
(
file_path
)
-
max_length
filename
=
prefix
[:
-
1
]
+
dot
+
extension
if
too_long
>
0
:
head
,
ext
=
os
.
path
.
splitext
(
filename
)
if
too_long
>
len
(
head
)
+
1
:
raise
SuspiciousFileOperation
(
'File name can not be shortened to a safe length'
)
filename
=
head
[:
-
too_long
]
+
ext
file_path
=
os
.
path
.
join
(
folder_name
,
filename
)
return
os
.
path
.
join
(
folder_name
,
filename
)
return
os
.
path
.
join
(
folder_name
,
filename
)
def
get_usage
(
self
):
def
get_usage
(
self
):
...
...
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