Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate md5sum on uploads while in /tmp, deduplicate code #963

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions files/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,6 @@ def media_file_info(input_file):
ret["fail"] = True
return ret

cmd = ["md5sum", input_file]
stdout = run_command(cmd).get("out")
if stdout:
md5sum = stdout.split()[0]
else:
md5sum = ""

cmd = [
settings.FFPROBE_COMMAND,
"-loglevel",
Expand Down Expand Up @@ -460,10 +453,21 @@ def media_file_info(input_file):
ret["video_info"] = video_info
ret["audio_info"] = audio_info
ret["is_video"] = True
ret["md5sum"] = md5sum
return ret


def media_file_md5sum(input_file):
"""
Get the md5sum of a file
"""
cmd = ["md5sum", input_file]
stdout = run_command(cmd).get("out")
if stdout:
return stdout.split()[0]
else:
return ""


def calculate_seconds(duration):
# returns seconds, given a ffmpeg extracted string
ret = 0
Expand Down
9 changes: 3 additions & 6 deletions files/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ def set_media_type(self, save=True):
self.media_info = json.dumps(ret)
except TypeError:
self.media_info = ""
self.md5sum = ret.get("md5sum")
if not self.md5sum:
self.md5sum = helpers.media_file_md5sum(self.media_file.path)
self.size = helpers.show_file_size(ret.get("file_size"))
else:
self.media_type = ""
Expand Down Expand Up @@ -1123,11 +1124,7 @@ def save(self, *args, **kwargs):
size = int(stdout.strip())
self.size = helpers.show_file_size(size)
if self.chunk_file_path and not self.md5sum:
cmd = ["md5sum", self.chunk_file_path]
stdout = helpers.run_command(cmd).get("out")
if stdout:
md5sum = stdout.strip().split()[0]
self.md5sum = md5sum
self.md5sum = helpers.media_file_md5sum(self.chunk_file_path)

super(Encoding, self).save(*args, **kwargs)

Expand Down
6 changes: 2 additions & 4 deletions files/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
get_file_name,
get_file_type,
media_file_info,
media_file_md5sum,
produce_ffmpeg_commands,
produce_friendly_token,
rm_file,
Expand Down Expand Up @@ -99,10 +100,7 @@ def chunkize_media(self, friendly_token, profiles, force=True):
chunks_dict = {}
# calculate once md5sums
for chunk in chunks:
cmd = ["md5sum", chunk]
stdout = run_command(cmd).get("out")
md5sum = stdout.strip().split()[0]
chunks_dict[chunk] = md5sum
chunks_dict[chunk] = media_file_md5sum(chunk)

for profile in profiles:
if media.video_height and media.video_height < profile.resolution:
Expand Down
7 changes: 5 additions & 2 deletions uploader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.views import generic

from cms.permissions import user_allowed_to_upload
from files.helpers import rm_file
from files.helpers import media_file_md5sum, rm_file
from files.models import Media

from .fineuploader import ChunkedFineUploader
Expand Down Expand Up @@ -63,9 +63,12 @@ def form_valid(self, form):
return self.make_response({"success": True})
# create media!
media_file = os.path.join(settings.MEDIA_ROOT, self.upload.real_path)
# Precalculate md5sum on tmp file
# If running on tmpfs, this should be much faster
md5 = media_file_md5sum(media_file)
with open(media_file, "rb") as f:
myfile = File(f)
new = Media.objects.create(media_file=myfile, user=self.request.user)
new = Media.objects.create(media_file=myfile, user=self.request.user, md5sum=md5)
rm_file(media_file)
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, self.upload.file_path))
return self.make_response({"success": True, "media_url": new.get_absolute_url()})
Expand Down
Loading