I’ve been reading the django docs and posts here on stackoverflow but still not sure how to. So far this is my code:
forms.py:
def validate_file(file):
# Validate if no file submitted
if not file:
#raise ValidationError("No file submitted")
raise ValidationError("Error")
# Check file size (5 GB limit)
max_size = 5 * 1024 * 1024 * 1024 # 5 GB in bytes
if file.size > max_size:
#raise ValidationError("The maximum file size that can be uploaded is 5GB")
raise ValidationError("Error")
# Define allowed file types and their corresponding MIME types
allowed_types = {
# Audio formats
'wav': ['audio/wav', 'audio/x-wav'],
'mp3': ['audio/mpeg', 'audio/mp3'],
'mpga': ['audio/mpeg'],
'aac': ['audio/aac'],
...
class UploadFileForm(forms.Form):
file = forms.FileField(validators=[validate_file])
views.py:
def transcribe_Submit(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
uploaded_file = request.FILES['file']
session_id = str(uuid.uuid4())
request.session['session_id'] = session_id
try:
transcribed_doc, created = TranscribedDocument.objects.get_or_create(id=session_id)
transcribed_doc.audio_file = uploaded_file
transcribed_doc.save()
...
modesl.py:
class TranscribedDocument(models.Model):
id = models.CharField(primary_key=True, max_length = 40)
audio_file = models.FileField(max_length = 140, upload_to='example_upload_url/', null= True)
...
This is what I’ve been working with but I’ve been reading that it is better to handle uploads as chunks so that large files don’t overwhelm the website.
The website uploads to directly DigitalOcean Spaces with no files stored on the server and the maximum file upload size allowed by my website is 10GB if that is important.
4