4

I have a custom content type, built with dexterity. In the schema (The schema is listed below), I use 'plone.namedfile.field.NamedFile' for attachements/uploads.

I would like to restrict uploads so that only mp3 files can be attached to my content type. What is the best approach for achieving this?

Here is the full schema/model for my content type:

<model xmlns="http://namespaces.plone.org/supermodel/schema">
<schema>
<field name="date" type="zope.schema.Date">
<description />
<title>Date</title>
</field>
<field name="speaker" type="zope.schema.TextLine">
<description />
<title>Speaker</title>
</field>
<field name="service" type="zope.schema.Choice">
<description />
<title>Service</title>
<values>
<element>1st Service</element>
<element>2nd Service</element>
</values>
</field>
<field name="audio_file" type="plone.namedfile.field.NamedFile">
<description />
<title>Audio File</title>
</field>
</schema>
</model>

I shall begin my search here: http://plone.org/products/dexterity/documentation/manual/developer-manual/reference/default-value-validator-adaptors

SteveM
  • 6,058
  • 1
  • 16
  • 20
David Bain
  • 2,439
  • 1
  • 17
  • 19

1 Answers1

3

I've decided to use javascript for my first line of validation. I've based my solution on information found at <input type="file"> limit selectable files by extensions

Based on the advice my script looks something like this:

$(document).ready( function() {

function checkFile(event) {
        var fileElement = document.getElementById("form-widgets-audio_file-input");
        var fileExtension = "";
        if (fileElement.value.lastIndexOf(".") > 0) {
            fileExtension = fileElement.value.substring(fileElement.value.lastIndexOf(".") + 1, fileElement.value.length);
        }
        if (fileExtension == "mp3") {
            return true;
        }
        else {
            alert("You must select a mp3 file for upload");
            return false;
        }
    }

$("form#form").bind("submit",checkFile);

});

This is half the solution, next I'll need to add validation on the server side.

Community
  • 1
  • 1
David Bain
  • 2,439
  • 1
  • 17
  • 19
  • Have you found a way for server-side validation, by now? – Ida Mar 27 '13 at 08:22
  • An example for server-side-validation of audio-files can be found here: http://stackoverflow.com/questions/14264737/django-python-how-to-read-a-file-and-validate-that-it-is-an-audio-file – Ida Mar 27 '13 at 09:31
  • That link looks like the right direction. Once I get it working, I'll add a note here as the final answer. – David Bain Aug 24 '13 at 02:04
  • That'll be awesome, I'm very courious to hear about that :) Upvoted your quest. – Ida Sep 17 '13 at 10:37