comparison browser_side/tools.py @ 283:0eba1c4f9c6f

browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
author souliane <souliane@mailoo.org>
date Mon, 25 Nov 2013 20:39:28 +0100
parents 2d6bd975a72d
children 52b1afd7ac3f
comparison
equal deleted inserted replaced
282:ae3ec654836d 283:0eba1c4f9c6f
18 You should have received a copy of the GNU Affero General Public License 18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 from pyjamas.ui.DragWidget import DragWidget 22 from pyjamas.ui.DragWidget import DragWidget
23 from pyjamas.ui.FileUpload import FileUpload
24 from pyjamas import Window
23 import re 25 import re
24 from nativedom import NativeDOM 26 from nativedom import NativeDOM
25 from sat_frontends.tools import xml 27 from sat_frontends.tools import xml
26 28
27 dom = NativeDOM() 29 dom = NativeDOM()
92 LiberviaDragWidget.current = self.widget 94 LiberviaDragWidget.current = self.widget
93 DragLabel.onDragStart(self, event) 95 DragLabel.onDragStart(self, event)
94 96
95 def onDragEnd(self, event): 97 def onDragEnd(self, event):
96 LiberviaDragWidget.current = None 98 LiberviaDragWidget.current = None
99
100
101 class FilterFileUpload(FileUpload):
102
103 def __init__(self, name, max_size, types=None):
104 """
105 @param name: the input element name
106 @param max_size: maximum file size in MB
107 @param types: allowed types as a list of couples (x, y, z):
108 - x: MIME content type e.g. "audio/ogg"
109 - y: file extension e.g. "*.ogg"
110 - z: description for the user e.g. "Ogg Vorbis Audio"
111 If types is None, all file format are accepted
112 """
113 FileUpload.__init__(self)
114 self.setName(name)
115 from pyjamas import DOM
116 while DOM.getElementById(name):
117 name = "%s_" % name
118 self.setID(name)
119 self._id = name
120 self.max_size = max_size
121 self.types = types
122
123 def getFileInfo(self):
124 from __pyjamas__ import JS
125 JS("var file = top.document.getElementById(this._id).files[0]; return [file.size, file.type]")
126
127 def check(self):
128 if self.getFilename() == "":
129 return False
130 (size, filetype) = self.getFileInfo()
131 if self.types and filetype not in [x for (x, y, z) in self.types]:
132 types = ["- %s (%s)" % (z, y) for (x, y, z) in self.types]
133 Window.alert('This file type is not accepted.\nAccepted file types are:\n\n%s' % "\n".join(types))
134 return False
135 if size > self.max_size * pow(2, 20):
136 Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size)
137 return False
138 return True