Mercurial > libervia-web
annotate browser_side/file_tools.py @ 370:30d03d9f07e4
browser_side: refactorization of the file tools.py:
- tools.py renamed to html_tools.py
- method setPresenceStyle moved to contact.py
- classes DragLabel and LiberviaDragWidget moved to base_widget.py
- class FilterFileUpload moved to file_tools.py
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 23 Feb 2014 17:01:03 +0100 |
parents | browser_side/tools.py@187126b63170 |
children | 933bce4cb816 |
rev | line source |
---|---|
31
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
4 # Libervia: a Salut à Toi frontend |
340 | 5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org> |
31
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
10 # (at your option) any later version. |
31
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
15 # GNU Affero General Public License for more details. |
31
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
332
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
31
cb07078f8d6f
browser_side: added naive html sanitize method
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
283
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
20 from pyjamas.ui.FileUpload import FileUpload |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
21 from pyjamas import Window |
370
30d03d9f07e4
browser_side: refactorization of the file tools.py:
souliane <souliane@mailoo.org>
parents:
363
diff
changeset
|
22 from pyjamas import DOM |
283
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
23 |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
24 |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
25 class FilterFileUpload(FileUpload): |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
26 |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
27 def __init__(self, name, max_size, types=None): |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
28 """ |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
29 @param name: the input element name |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
30 @param max_size: maximum file size in MB |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
31 @param types: allowed types as a list of couples (x, y, z): |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
32 - x: MIME content type e.g. "audio/ogg" |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
33 - y: file extension e.g. "*.ogg" |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
34 - z: description for the user e.g. "Ogg Vorbis Audio" |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
35 If types is None, all file format are accepted |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
36 """ |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
37 FileUpload.__init__(self) |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
38 self.setName(name) |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
39 while DOM.getElementById(name): |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
40 name = "%s_" % name |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
41 self.setID(name) |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
42 self._id = name |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
43 self.max_size = max_size |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
44 self.types = types |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
45 |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
46 def getFileInfo(self): |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
47 from __pyjamas__ import JS |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
48 JS("var file = top.document.getElementById(this._id).files[0]; return [file.size, file.type]") |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
49 |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
50 def check(self): |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
51 if self.getFilename() == "": |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
52 return False |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
53 (size, filetype) = self.getFileInfo() |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
54 if self.types and filetype not in [x for (x, y, z) in self.types]: |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
55 types = ["- %s (%s)" % (z, y) for (x, y, z) in self.types] |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
56 Window.alert('This file type is not accepted.\nAccepted file types are:\n\n%s' % "\n".join(types)) |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
57 return False |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
58 if size > self.max_size * pow(2, 20): |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
59 Window.alert('This file is too big!\nMaximum file size: %d MB.' % self.max_size) |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
60 return False |
0eba1c4f9c6f
browser_side (plugins radiocol, xep-0054): check for file size or type before uploading
souliane <souliane@mailoo.org>
parents:
279
diff
changeset
|
61 return True |