Skip to content

Commit 74ceb71

Browse files
committed
Add python-magic for determine the type of file. Update jQuery UI template.
1 parent 0b64fac commit 74ceb71

File tree

5 files changed

+93
-69
lines changed

5 files changed

+93
-69
lines changed
0 Bytes
Binary file not shown.

jquery_uploader/locale/ru/LC_MESSAGES/django.po

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2013-07-12 10:12+0300\n"
11+
"POT-Creation-Date: 2013-10-11 15:20+0300\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -19,57 +19,63 @@ msgstr ""
1919
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
2020
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n"
2121

22-
#: templates/jquery_uploader/bootstrap.html:15
22+
#: templates/jquery_uploader/bootstrap.html:16
2323
msgid "Add files..."
2424
msgstr "Добавить файлы..."
2525

26-
#: templates/jquery_uploader/bootstrap.html:20
26+
#: templates/jquery_uploader/bootstrap.html:21
2727
msgid "Start upload"
2828
msgstr "Начать загрузку"
2929

30-
#: templates/jquery_uploader/bootstrap.html:24
30+
#: templates/jquery_uploader/bootstrap.html:25
3131
msgid "Cancel upload"
3232
msgstr "Отменить загрузку"
3333

34-
#: templates/jquery_uploader/bootstrap.html:28
35-
#: templates/jquery_uploader/simple.html:92
34+
#: templates/jquery_uploader/bootstrap.html:29
35+
#: templates/jquery_uploader/simple.html:93
3636
msgid "Delete"
3737
msgstr "Удалить"
3838

39-
#: templates/jquery_uploader/bootstrap.html:35
40-
#: templates/jquery_uploader/jquery-ui.html:31
41-
#: templates/jquery_uploader/simple.html:25
39+
#: templates/jquery_uploader/bootstrap.html:36
40+
#: templates/jquery_uploader/jquery-ui.html:32
41+
#: templates/jquery_uploader/simple.html:26
4242
msgid "Global Progress"
4343
msgstr "Общий прогресс"
4444

45-
#: templates/jquery_uploader/bootstrap.html:45
46-
#: templates/jquery_uploader/jquery-ui.html:41
47-
#: templates/jquery_uploader/simple.html:35
45+
#: templates/jquery_uploader/bootstrap.html:46
46+
#: templates/jquery_uploader/jquery-ui.html:42
47+
#: templates/jquery_uploader/simple.html:36
4848
msgid "Selected files for download"
4949
msgstr "Выбранные файлы для загрузки"
5050

51-
#: templates/jquery_uploader/bootstrap.html:74
51+
#: templates/jquery_uploader/bootstrap.html:62
52+
#: templates/jquery_uploader/bootstrap.html:106
53+
#: templates/jquery_uploader/jquery-ui.html:58
54+
#: templates/jquery_uploader/jquery-ui.html:96
55+
#: templates/jquery_uploader/simple.html:56
56+
#: templates/jquery_uploader/simple.html:85
57+
msgid "Error"
58+
msgstr "Ошибка"
59+
60+
#: templates/jquery_uploader/bootstrap.html:75
61+
#: templates/jquery_uploader/jquery-ui.html:69
5262
msgid "Start"
5363
msgstr "Загрузить"
5464

55-
#: templates/jquery_uploader/bootstrap.html:80
56-
#: templates/jquery_uploader/simple.html:62
65+
#: templates/jquery_uploader/bootstrap.html:81
66+
#: templates/jquery_uploader/jquery-ui.html:72
67+
#: templates/jquery_uploader/simple.html:63
5768
msgid "Cancel"
5869
msgstr "Отменить"
5970

60-
#: templates/jquery_uploader/simple.html:13
71+
#: templates/jquery_uploader/simple.html:14
6172
msgid "Add files"
6273
msgstr "Добавить файлы"
6374

64-
#: templates/jquery_uploader/simple.html:55
65-
#: templates/jquery_uploader/simple.html:84
66-
msgid "Error"
67-
msgstr "Ошибка"
68-
69-
#: templates/jquery_uploader/simple.html:57
75+
#: templates/jquery_uploader/simple.html:58
7076
msgid "Ready for loading"
7177
msgstr "Готов к загрузке"
7278

73-
#: templates/jquery_uploader/simple.html:86
79+
#: templates/jquery_uploader/simple.html:87
7480
msgid "Loaded"
7581
msgstr "Загружен"

jquery_uploader/templates/jquery_uploader/bootstrap.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<td class="name">
6060
[%=file.name%]
6161
[% if (file.error) { %]
62-
<div><span class="label label-important">Error</span> [%=file.error%]</div>
62+
<div><span class="label label-important">{% trans 'Error' %}</span> [%=file.error%]</div>
6363
[% } %]
6464
</td>
6565
<td class="size">
@@ -103,7 +103,7 @@
103103
<a href="[%=file.url%]" title="[%=file.name%]" class="[%=file.thumbnail_url?'gallery':''%]" download="[%=file.name%]">[%=file.name%]</a>
104104
</p>
105105
[% if (file.error) { %]
106-
<div><span class="label label-important">Error</span> [%=file.error%]</div>
106+
<div><span class="label label-important">{% trans 'Error' %}</span> [%=file.error%]</div>
107107
[% } %]
108108
</td>
109109
<td>

jquery_uploader/templates/jquery_uploader/jquery-ui.html

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,29 @@
4949
<script id="template-upload" type="text/x-tmpl">
5050
[% for (var i=0, file; file=o.files[i]; i++) { %]
5151
<tr class="template-upload fade">
52-
<td class="preview"><span class="fade"></span></td>
53-
<td class="name"><span>[%=file.name%]</span></td>
54-
<td class="size"><span>[%=o.formatFileSize(file.size)%]</span></td>
55-
[% if (file.error) { %]
56-
<td class="error" colspan="2">Error [%=file.error%]</td>
57-
[% } else if (o.files.valid && !i) { %]
58-
<td>
59-
<div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="bar" style="width:0%;"></div></div>
60-
</td>
61-
<td>[% if (!o.options.autoUpload) { %]
62-
<button class="start">Start</button>
63-
[% } %]</td>
64-
[% } else { %]
65-
<td colspan="2"></td>
66-
[% } %]
67-
<td>[% if (!i) { %]
68-
<button class="cancel">Cancel</button>
69-
[% } %]</td>
52+
<td class="preview">
53+
<span class="fade"></span>
54+
</td>
55+
<td class="name">
56+
[%=file.name%]
57+
[% if (file.error) { %]
58+
<div>{% trans 'Error' %} [%=file.error%]</div>
59+
[% } %]
60+
</td>
61+
<td class="size">
62+
<p>[%=o.formatFileSize(file.size)%]</p>
63+
[% if (!o.files.error) { %]
64+
<div class="progress" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"></div>
65+
[% } %]
66+
</td>
67+
<td>
68+
[% if (!o.files.error && !i && !o.options.autoUpload) { %]
69+
<button class="start">{% trans 'Start' %}</button>
70+
[% } %]
71+
[% if (!i) { %]
72+
<button class="cancel">{% trans 'Cancel' %}</button>
73+
[% } %]
74+
</td>
7075
</tr>
7176
[% } %]
7277
</script>
@@ -76,23 +81,26 @@
7681
<script id="template-download" type="text/x-tmpl">
7782
[% for (var i=0, file; file=o.files[i]; i++) { %]
7883
<tr class="template-download fade">
79-
[% if (file.error) { %]
80-
<td></td>
81-
<td class="name"><span>[%=file.name%]</span></td>
82-
<td class="size"><span>[%=o.formatFileSize(file.size)%]</span></td>
83-
<td class="error" colspan="2">>Error [%=file.error%]</td>
84-
[% } else { %]
85-
<td class="preview">[% if (file.thumbnail_url) { %]
86-
<a href="[%=file.url%]" title="[%=file.name%]" data-gallery="gallery" download="[%=file.name%]"><img src="[%=file.thumbnail_url%]"></a>
87-
[% } %]</td>
88-
<td class="name">
89-
<a href="[%=file.url%]" title="[%=file.name%]" data-gallery="[%=file.thumbnail_url&&'gallery'%]" download="[%=file.name%]">[%=file.name%]</a>
90-
</td>
91-
<td class="size"><span>[%=o.formatFileSize(file.size)%]</span></td>
92-
<td colspan="2"></td>
93-
[% } %]
9484
<td>
95-
<button class="delete" data-type="[%=file.delete_type%]" data-url="[%=file.delete_url%]"[% if (file.delete_with_credentials) { %] data-xhr-fields='{"withCredentials":true}'[% } %]>Delete</button>
85+
<span class="preview">
86+
[% if (file.thumbnail_url) { %]
87+
<a href="[%=file.url%]" title="[%=file.name%]" class="gallery" download="[%=file.name%]" data-gallery><img src="[%=file.thumbnail_url%]"></a>
88+
[% } %]
89+
</span>
90+
</td>
91+
<td>
92+
<p class="name">
93+
<a href="[%=file.url%]" title="[%=file.name%]" download="[%=file.name%]" [%=file.thumbnail_url?'data-gallery':''%]>[%=file.name%]</a>
94+
</p>
95+
[% if (file.error) { %]
96+
<div><span class="error">{% trans 'Error' %}</span> [%=file.error%]</div>
97+
[% } %]
98+
</td>
99+
<td>
100+
<span class="size">[%=o.formatFileSize(file.size)%]</span>
101+
</td>
102+
<td>
103+
<button class="delete" data-type="[%=file.deleteType%]" data-url="[%=file.deleteUrl%]"[% if (file.deleteWithCredentials) { %] data-xhr-fields='{"withCredentials":true}'[% } %]>Delete</button>
96104
<input type="checkbox" name="delete" value="1" class="toggle">
97105
<!-- Fields to work properly with the uploader -->
98106
<input type="hidden" name="jquery_uploads" value="[%=file.url%]">
@@ -143,16 +151,19 @@
143151
tmpl.regexp = /([\s'\\])(?![^%]*%\])|(?:\[%(=|#)([\s\S]+?)%\])|(\[%)|(%\])/g;
144152

145153
// Initialize the jQuery File Upload widget:
146-
// Uncomment the following to send cross-domain cookies:
147154
$("#fileupload").fileupload({
148155
maxFileSize: {{ max_file_size }},
149156
previewMaxHeight: {{ preview_max_height }},
150157
previewMaxWidth: {{ preview_max_width }},
158+
159+
// Uncomment the following to send cross-domain cookies:
151160
//xhrFields: {withCredentials: true},
152161
});
153162

154163
});
155164
</script>
156165

157166
<!-- The XDomainRequest Transport is included for cross-domain file deletion for IE8+ -->
158-
<!--[if gte IE 8]><script src="js/cors/jquery.xdr-transport.js"></script><![endif]-->
167+
<!--[if gte IE 8]>
168+
<script src="{% get_static_prefix %}jquery_uploader/js/cors/jquery.xdr-transport.js"></script>
169+
<![endif]-->

jquery_uploader/utils.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import magic
23
import os
34
from PIL import Image
45

@@ -13,23 +14,29 @@ def thumb_name(full_name):
1314
""" Generate thumb file name """
1415
file_name = os.path.basename(full_name)
1516
name, ext = os.path.splitext(file_name)
16-
thumb = '%s_t%s' % (name, ext)
17+
thumb = '%s_t.jpeg' % name
1718
return thumb
1819

1920

2021
def get_thumb(full_name):
2122
""" Generate and save thumbnail. Return thumbnail url """
2223

24+
thumb_url = None
2325
thumb = thumb_name(full_name)
2426
thumb_full = os.path.join(uploader_root, thumb)
2527

26-
if not os.path.exists(thumb_full):
27-
im = Image.open(full_name)
28-
thumb_image = im.copy()
29-
thumb_image.thumbnail(uploader_size, Image.ANTIALIAS)
30-
thumb_image.save(thumb_full, 'JPEG', quality=90)
31-
32-
thumb_url = os.path.join(uploader_url, thumb)
28+
mimes_images = ('image/png', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/x-ms-bmp', 'image/bmp', 'image/vnd.microsoft.icon')
29+
mime = magic.from_file(full_name, mime=True)
30+
if mime in mimes_images:
31+
if not os.path.exists(thumb_full):
32+
im = Image.open(full_name)
33+
thumb_image = im.copy()
34+
if thumb_image.mode != 'RGB':
35+
thumb_image = thumb_image.convert('RGB')
36+
thumb_image.thumbnail(uploader_size, Image.ANTIALIAS)
37+
thumb_image.save(thumb_full, 'JPEG', quality=80)
38+
39+
thumb_url = os.path.join(uploader_url, thumb)
3340

3441
return thumb_url
3542

0 commit comments

Comments
 (0)