# HG changeset patch # User souliane # Date 1385408368 -3600 # Node ID 0eba1c4f9c6fea5c4b6ede617cfb059561c6fa77 # Parent ae3ec654836d4b0536eb3dfed4c8d1e5e514ef78 browser_side (plugins radiocol, xep-0054): check for file size or type before uploading diff -r ae3ec654836d -r 0eba1c4f9c6f browser_side/menu.py --- a/browser_side/menu.py Tue Dec 10 09:07:03 2013 +0100 +++ b/browser_side/menu.py Mon Nov 25 20:39:28 2013 +0100 @@ -37,6 +37,7 @@ from pyjamas import Window from jid import JID from tools import html_sanitize +from tools import FilterFileUpload from xmlui import XMLUI import panels import dialog @@ -86,8 +87,7 @@ hPanel = HorizontalPanel() hPanel.setSpacing(5) - self.file_upload = FileUpload() - self.file_upload.setName("avatar_path") + self.file_upload = FilterFileUpload("avatar_path", 2) self.vPanel.add(self.file_upload) hPanel.add(Button("Cancel", getattr(self, "onCloseBtnClick"))) @@ -112,6 +112,8 @@ print ("WARNING: no close method defined") def onSubmitBtnClick(self): + if not self.file_upload.check(): + return self.message.setHTML('Submitting, please wait...') self.upload_btn.setEnabled(False) self.submit() diff -r ae3ec654836d -r 0eba1c4f9c6f browser_side/radiocol.py --- a/browser_side/radiocol.py Tue Dec 10 09:07:03 2013 +0100 +++ b/browser_side/radiocol.py Mon Nov 25 20:39:28 2013 +0100 @@ -26,7 +26,6 @@ from pyjamas.ui.FlexTable import FlexTable from pyjamas.ui.FormPanel import FormPanel from pyjamas.ui.NamedFrame import NamedFrame -from pyjamas.ui.FileUpload import FileUpload from pyjamas.ui.Label import Label from pyjamas.ui.Button import Button from pyjamas.ui.ClickListener import ClickHandler @@ -39,6 +38,7 @@ from jid import JID from tools import html_sanitize +from tools import FilterFileUpload class MetadataPanel(FlexTable): @@ -88,8 +88,10 @@ hPanel = HorizontalPanel() hPanel.setSpacing(5) - self.file_upload = FileUpload() - self.file_upload.setName("song") + types = [('audio/ogg', '*.ogg', 'Ogg Vorbis Audio'), + ('video/ogg', '*.ogv', 'Ogg Vorbis Video'), + ('application/ogg', '*.ogx', 'Ogg Vorbis Multiplex')] + self.file_upload = FilterFileUpload("song", 5, types) hPanel.add(self.file_upload) self.upload_btn = Button("Upload song", getattr(self, "onBtnClick")) @@ -111,7 +113,10 @@ self.status.setText('') def onBtnClick(self): - self.submit() + if self.file_upload.check(): + self.submit() + self.file_upload.setFilename("") + self.status.setText('[Submitting, please wait...]') def onSubmit(self, event): pass @@ -133,8 +138,10 @@ elif result == "KO": self.status.setText('[Something went wrong during your song upload]') self.status.setStyleName('radiocol_upload_status_ko') + self._timer.schedule(5000) else: Window.alert('Submit error: %s' % result) + self.status.setText('') class Player(HTML): diff -r ae3ec654836d -r 0eba1c4f9c6f browser_side/tools.py --- a/browser_side/tools.py Tue Dec 10 09:07:03 2013 +0100 +++ b/browser_side/tools.py Mon Nov 25 20:39:28 2013 +0100 @@ -20,6 +20,8 @@ """ from pyjamas.ui.DragWidget import DragWidget +from pyjamas.ui.FileUpload import FileUpload +from pyjamas import Window import re from nativedom import NativeDOM from sat_frontends.tools import xml @@ -94,3 +96,43 @@ def onDragEnd(self, event): LiberviaDragWidget.current = None + + +class FilterFileUpload(FileUpload): + + def __init__(self, name, max_size, types=None): + """ + @param name: the input element name + @param max_size: maximum file size in MB + @param types: allowed types as a list of couples (x, y, z): + - x: MIME content type e.g. "audio/ogg" + - y: file extension e.g. "*.ogg" + - z: description for the user e.g. "Ogg Vorbis Audio" + If types is None, all file format are accepted + """ + FileUpload.__init__(self) + self.setName(name) + from pyjamas import DOM + while DOM.getElementById(name): + name = "%s_" % name + self.setID(name) + self._id = name + self.max_size = max_size + self.types = types + + def getFileInfo(self): + from __pyjamas__ import JS + JS("var file = top.document.getElementById(this._id).files[0]; return [file.size, file.type]") + + def check(self): + if self.getFilename() == "": + return False + (size, filetype) = self.getFileInfo() + if self.types and filetype not in [x for (x, y, z) in self.types]: + types = ["- %s (%s)" % (z, y) for (x, y, z) in self.types] + Window.alert('This file type is not accepted.\nAccepted file types are:\n\n%s' % "\n".join(types)) + return False + if size > self.max_size * pow(2, 20): + Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size) + return False + return True