forked from typecho/typecho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-upload-js.php
193 lines (170 loc) · 6.68 KB
/
file-upload-js.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php if(!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php
if (isset($post) && $post instanceof Typecho_Widget && $post->have()) {
$fileParentContent = $post;
} else if (isset($page) && $page instanceof Typecho_Widget && $page->have()) {
$fileParentContent = $page;
}
?>
<script src="<?php $options->adminUrl('javascript/filedrop.js?v=' . $suffixVersion); ?>"></script>
<script>
$(document).ready(function() {
var errorWord = '<?php $val = function_exists('ini_get') ? trim(ini_get('upload_max_filesize')) : 0;
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
$val = number_format(ceil($val / (1024 *1024)));
_e('附件上传失败, 请确认附件尺寸没有超过 %s 并且服务器附件目录可以写入', "{$val}Mb"); ?>',
loading = $('<img src="<?php $options->adminUrl('images/ajax-loader.gif'); ?>" style="display:none" />')
.appendTo(document.body);
function fileUploadStart (file, id) {
$('<li id="' + id + '" class="loading">'
+ file + '</li>').prependTo('#file-list');
}
function fileUploadComplete (id, url, data) {
var li = $('#' + id).removeClass('loading').data('cid', data.cid)
.data('url', data.url)
.data('image', data.isImage)
.html('<input type="hidden" name="attachment[]" value="' + data.cid + '" />'
+ '<a class="file" target="_blank" href="<?php $options->adminUrl('media.php'); ?>?cid='
+ data.cid + '">' + data.title + '</a> ' + data.bytes
+ ' <a class="insert" href="#"><?php _e('插入'); ?></a>'
+ ' <a class="delete" href="#">×</a>')
.effect('highlight', '#AACB36', 1000);
attachInsertEvent(li);
attachDeleteEvent(li);
}
$('.upload-file').fileUpload({
url : '<?php $options->index('/action/upload'
. (isset($fileParentContent) ? '?cid=' . $fileParentContent->cid : '')); ?>',
types : '<?php
$attachmentTypes = $options->allowedAttachmentTypes;
$attachmentTypesCount = count($attachmentTypes);
for ($i = 0; $i < $attachmentTypesCount; $i ++) {
echo '*.' . $attachmentTypes[$i];
if ($i < $attachmentTypesCount - 1) {
echo ';';
}
}
?>',
typesError : '<?php _e('附件 %s 的类型不被支持'); ?>',
onUpload : fileUploadStart,
onError : function (id) {
$('#' + id).remove();
alert(errorWord);
},
onComplete : fileUploadComplete
});
$('#upload-panel').filedrop({
url : '<?php $options->index('/action/upload'
. (isset($fileParentContent) ? '?cid=' . $fileParentContent->cid : '')); ?>',
allowedfileextensions : [<?php
$attachmentTypes = $options->allowedAttachmentTypes;
$attachmentTypesCount = count($attachmentTypes);
for ($i = 0; $i < $attachmentTypesCount; $i ++) {
echo '".' . $attachmentTypes[$i] . '"';
if ($i < $attachmentTypesCount - 1) {
echo ',';
}
}
?>],
maxfilesize : <?php
$val = function_exists('ini_get') ? trim(ini_get('upload_max_filesize')) : 0;
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
echo ceil($val / (1024 * 1024));
?>,
queuefiles : 5,
error: function(err, file) {
switch(err) {
case 'BrowserNotSupported':
alert('<?php _e('浏览器不支持拖拽上传'); ?>');
break;
case 'TooManyFiles':
alert('<?php _e('一次上传的文件不能多于%d个', 25); ?>');
break;
case 'FileTooLarge':
alert('<?php $val = function_exists('ini_get') ? trim(ini_get('upload_max_filesize')) : 0;
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
$val = number_format(ceil($val / (1024 *1024)));
_e('附件尺寸不能超过 %s', "{$val}Mb"); ?>');
break;
case 'FileTypeNotAllowed':
// The file type is not in the specified list 'allowedfiletypes'
break;
case 'FileExtensionNotAllowed':
alert('<?php _e('附件 %s 的类型不被支持'); ?>'.replace('%s', file.name));
break;
default:
break;
}
},
dragOver : function () {
$(this).addClass('drag');
},
dragLeave : function () {
$(this).removeClass('drag');
},
drop : function () {
$(this).removeClass('drag');
},
uploadOpened : function (i, file, len) {
fileUploadStart(file.name, 'drag-' + i);
},
uploadFinished : function (i, file, response) {
fileUploadComplete('drag-' + i, response[0], response[1]);
}
});
$('#file-list li').each(function () {
attachInsertEvent(this);
attachDeleteEvent(this);
});
function attachInsertEvent (el) {
$('.insert', el).click(function () {
var p = $(this).parents('li');
Typecho.insertFileToEditor(p.data('url'), p.data('image'));
return false;
});
}
function attachDeleteEvent (el) {
var file = $('a.file', el).text();
$('.delete', el).click(function () {
if (confirm('<?php _e('确认要删除附件 %s 吗?'); ?>'.replace('%s', file))) {
var cid = $(this).parents('li').data('cid');
$.post('<?php $options->index('/action/contents-attachment-edit'); ?>',
{'do' : 'delete', 'cid' : cid},
function () {
$(el).fadeOut(function () {
$(this).remove();
});
});
}
return false;
});
}
});
</script>