0

I am using Celery to strip the audio from an uploaded video file. I want to save the audio file into my model where the video file is. So this is what I am trying:

# model
class Video(models.Model):
    file = models.FileField(
        validators=[
            FileExtensionValidator(
                allowed_extensions=["mp4", "mov", "flv", "mkv", "avi"]
            )
        ]
    )
    processed_file = models.FileField(
        validators=[FileExtensionValidator(allowed_extensions=["flac"])],
        null=True,
        blank=True,
    )

# celery task 
output_audio_file = str(settings.MEDIA_ROOT) + "/" + str(video_obj.pk) + ".flac"

ffmpeg.input(video_obj.file.path).output(
    output_audio_file,
    **{
        "ac": 1,
        "ar": 16000,
        "format": "flac",
    },
).run()

dj_file = File(open(output_audio_file))
video_obj.processed_file = dj_file
video_obj.save()

This raises the Detected path traversal attempt in '/app/proj/media/49.flac' exception. I also tried with the context manager, with the video_obj.processed_file.save() method. All of which raise TypeError, AttributeError, UnicodeDecodeError and so on. I feel like the answer could be so simple but I just couldn't find it.

1 Answer 1

2

Okay it turned out simple as I thought. Since that Django saves the path in the DB I just gave the path to the FileField instead, like so:

video_obj.processed_file.name = str(video_obj.pk) + ".flac"
video_obj.save()

I don't need the MEDIA_ROOT since it is added by default. Ref: https://docs.djangoproject.com/en/3.2/topics/files/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.