media.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. include 'common.php';
  3. include 'header.php';
  4. include 'menu.php';
  5. $phpMaxFilesize = function_exists('ini_get') ? trim(ini_get('upload_max_filesize')) : 0;
  6. if (preg_match("/^([0-9]+)([a-z]{1,2})$/i", $phpMaxFilesize, $matches)) {
  7. $phpMaxFilesize = strtolower($matches[1] . $matches[2] . (1 == strlen($matches[2]) ? 'b' : ''));
  8. }
  9. $attachment = \Widget\Contents\Attachment\Edit::alloc();
  10. ?>
  11. <div class="main">
  12. <div class="body container">
  13. <?php include 'page-title.php'; ?>
  14. <div class="row typecho-page-main">
  15. <div class="col-mb-12 col-tb-8" role="main">
  16. <?php if ($attachment->attachment->isImage): ?>
  17. <p><img src="<?php $attachment->attachment->url(); ?>"
  18. alt="<?php $attachment->attachment->name(); ?>" class="typecho-attachment-photo"/></p>
  19. <?php endif; ?>
  20. <p>
  21. <?php $mime = \Typecho\Common::mimeIconType($attachment->attachment->mime); ?>
  22. <i class="mime-<?php echo $mime; ?>"></i>
  23. <a href=""><strong><?php $attachment->attachment->name(); ?></strong></a>
  24. <span><?php echo number_format(ceil($attachment->attachment->size / 1024)); ?> Kb</span>
  25. </p>
  26. <p>
  27. <input id="attachment-url" type="text" class="mono w-100"
  28. value="<?php $attachment->attachment->url(); ?>" readonly/>
  29. </p>
  30. <div id="upload-panel" class="p">
  31. <div class="upload-area"
  32. draggable="true"><?php _e('拖放文件到这里<br>或者 %s选择文件上传%s', '<a href="###" class="upload-file">', '</a>'); ?></div>
  33. <ul id="file-list"></ul>
  34. </div>
  35. </div>
  36. <div class="col-mb-12 col-tb-4 edit-media" role="form">
  37. <?php $attachment->form()->render(); ?>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. <?php
  43. include 'copyright.php';
  44. include 'common-js.php';
  45. ?>
  46. <script src="<?php $options->adminStaticUrl('js', 'moxie.js'); ?>"></script>
  47. <script src="<?php $options->adminStaticUrl('js', 'plupload.js'); ?>"></script>
  48. <script type="text/javascript">
  49. $(document).ready(function () {
  50. $('#attachment-url').click(function () {
  51. $(this).select();
  52. });
  53. $('.operate-delete').click(function () {
  54. var t = $(this), href = t.attr('href');
  55. if (confirm(t.attr('lang'))) {
  56. window.location.href = href;
  57. }
  58. return false;
  59. });
  60. $('.upload-area').bind({
  61. dragenter: function () {
  62. $(this).parent().addClass('drag');
  63. },
  64. dragover: function (e) {
  65. $(this).parent().addClass('drag');
  66. },
  67. drop: function () {
  68. $(this).parent().removeClass('drag');
  69. },
  70. dragend: function () {
  71. $(this).parent().removeClass('drag');
  72. },
  73. dragleave: function () {
  74. $(this).parent().removeClass('drag');
  75. }
  76. });
  77. function fileUploadStart(file) {
  78. $('<ul id="file-list"></ul>').appendTo('#upload-panel');
  79. $('<li id="' + file.id + '" class="loading">'
  80. + file.name + '</li>').prependTo('#file-list');
  81. }
  82. function fileUploadError(error) {
  83. var file = error.file, code = error.code, word;
  84. switch (code) {
  85. case plupload.FILE_SIZE_ERROR:
  86. word = '<?php _e('文件大小超过限制'); ?>';
  87. break;
  88. case plupload.FILE_EXTENSION_ERROR:
  89. word = '<?php _e('文件扩展名不被支持'); ?>';
  90. break;
  91. case plupload.FILE_DUPLICATE_ERROR:
  92. word = '<?php _e('文件已经上传过'); ?>';
  93. break;
  94. case plupload.HTTP_ERROR:
  95. default:
  96. word = '<?php _e('上传出现错误'); ?>';
  97. break;
  98. }
  99. var fileError = '<?php _e('%s 上传失败'); ?>'.replace('%s', file.name),
  100. li, exist = $('#' + file.id);
  101. if (exist.length > 0) {
  102. li = exist.removeClass('loading').html(fileError);
  103. } else {
  104. $('<ul id="file-list"></ul>').appendTo('#upload-panel');
  105. li = $('<li>' + fileError + '<br />' + word + '</li>').prependTo('#file-list');
  106. }
  107. li.effect('highlight', {color: '#FBC2C4'}, 2000, function () {
  108. $(this).remove();
  109. });
  110. }
  111. function fileUploadComplete(id, url, data) {
  112. var img = $('.typecho-attachment-photo');
  113. if (img.length > 0) {
  114. img.get(0).src = '<?php $attachment->attachment->url(); ?>?' + Math.random();
  115. }
  116. $('#' + id).text('<?php _e('文件 %s 已经替换'); ?>'.replace('%s', data.title))
  117. .effect('highlight', 1000, function () {
  118. $(this).remove();
  119. $('#file-list').remove();
  120. });
  121. }
  122. var uploader = new plupload.Uploader({
  123. browse_button: $('.upload-file').get(0),
  124. url: '<?php $security->index('/action/upload?do=modify&cid=' . $attachment->cid); ?>',
  125. runtimes: 'html5,flash,html4',
  126. flash_swf_url: '<?php $options->adminStaticUrl('js', 'Moxie.swf'); ?>',
  127. drop_element: $('.upload-area').get(0),
  128. filters: {
  129. max_file_size: '<?php echo $phpMaxFilesize ?>',
  130. mime_types: [{
  131. 'title': '<?php _e('允许上传的文件'); ?>',
  132. 'extensions': '<?php $attachment->attachment->type(); ?>'
  133. }],
  134. prevent_duplicates: true
  135. },
  136. multi_selection: false,
  137. init: {
  138. FilesAdded: function (up, files) {
  139. plupload.each(files, function (file) {
  140. fileUploadStart(file);
  141. });
  142. uploader.start();
  143. },
  144. FileUploaded: function (up, file, result) {
  145. if (200 == result.status) {
  146. var data = $.parseJSON(result.response);
  147. if (data) {
  148. fileUploadComplete(file.id, data[0], data[1]);
  149. return;
  150. }
  151. }
  152. fileUploadError({
  153. code: plupload.HTTP_ERROR,
  154. file: file
  155. });
  156. },
  157. Error: function (up, error) {
  158. fileUploadError(error);
  159. }
  160. }
  161. });
  162. uploader.init();
  163. });
  164. </script>
  165. <?php
  166. include 'footer.php';
  167. ?>