Added file type icons as thumbnails for non-images
authorMarcel Werk <burntime@woltlab.com>
Tue, 21 Mar 2017 20:04:20 +0000 (21:04 +0100)
committerMarcel Werk <burntime@woltlab.com>
Tue, 21 Mar 2017 20:04:20 +0000 (21:04 +0100)
Closes #2239

com.woltlab.wcf/templates/attachments.tpl
com.woltlab.wcf/templates/messageFormAttachments.tpl
wcfsetup/install/files/acp/templates/attachmentList.tpl
wcfsetup/install/files/js/WCF.Attachment.js
wcfsetup/install/files/lib/data/attachment/Attachment.class.php
wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php
wcfsetup/install/files/lib/data/media/ViewableMedia.class.php
wcfsetup/install/files/lib/util/FileUtil.class.php

index c378ff92199b3b3e290647849fee898170ccdbb2..01b0d47a21fa9ac3b60c0ff0f01552513ace1286 100644 (file)
@@ -61,7 +61,7 @@
                                        {foreach from=$attachmentList->getGroupedObjects($objectID) item=attachment}
                                                {if $attachment->showAsFile() && !$attachment->isEmbedded()}
                                                        <li class="box32" data-attachment-id="{@$attachment->attachmentID}">
-                                                               <a href="{link controller='Attachment' object=$attachment}{/link}"><span class="icon icon32 fa-paperclip"></span></a>
+                                                               <a href="{link controller='Attachment' object=$attachment}{/link}"><span class="icon icon32 fa-{@$attachment->getIconName()}"></span></a>
                                                                
                                                                <div>
                                                                        <p><a href="{link controller='Attachment' object=$attachment}{/link}">{$attachment->filename}</a></p>
index f45ac57be8074fa572c7b10ac3329eeeb7ad2f6e..a6ccc608c2bb1fcab616ac6dc08ce5bd14667b23 100644 (file)
@@ -5,7 +5,7 @@
                                {if $attachment->tinyThumbnailType}
                                        <img src="{link controller='Attachment' object=$attachment}tiny=1{/link}" alt="" class="attachmentTinyThumbnail">
                                {else}
-                                       <span class="icon icon64 fa-paperclip"></span>
+                                       <span class="icon icon64 fa-{@$attachment->getIconName()}"></span>
                                {/if}
                                
                                <div>
index a25d5f6a16d69429bf88a510b3cf54e31a05bed6..30b1fd6438b17ccd60480a909a0788d7c63e8109 100644 (file)
                                {foreach from=$objects item=attachment}
                                        <tr class="jsAttachmentRow">
                                                <td class="columnIcon">
-                                                       <span class="icon icon16 fa-times jsDeleteButton jsTooltip pointer" title="{lang}wcf.global.button.delete{/lang}" data-object-id="{@$attachment->attachmentID}" data-confirm-message="{lang}wcf.attachment.delete.sure{/lang}"></span>
+                                                       <span class="icon icon24 fa-times jsDeleteButton jsTooltip pointer" title="{lang}wcf.global.button.delete{/lang}" data-object-id="{@$attachment->attachmentID}" data-confirm-message="{lang}wcf.attachment.delete.sure{/lang}"></span>
                                                        
                                                        {event name='rowButtons'}
                                                </td>
                                                                        {if $attachment->tinyThumbnailType}
                                                                                <img src="{link controller='Attachment' id=$attachment->attachmentID}tiny=1{/link}" class="attachmentTinyThumbnail" alt="">
                                                                        {else}
-                                                                               <span class="icon icon64 fa-paperclip"></span>
+                                                                               <span class="icon icon64 fa-{@$attachment->getIconName()}"></span>
                                                                        {/if}
                                                                </a>
                                                                
index 9a6246860238ef23a2e63dc294e44691d0c27f2e..7cd17ff71b47a8c40554956d75eea517de8eace3 100644 (file)
@@ -350,7 +350,7 @@ WCF.Attachment.Upload = WCF.Upload.extend({
                                }
                                // show file icon
                                else {
-                                       $li.children('.fa-spinner').removeClass('fa-spinner').addClass('fa-paperclip');
+                                       $li.children('.fa-spinner').removeClass('fa-spinner').addClass('fa-' + attachmentData.iconName);
                                }
                                
                                // update attachment link
index b4b3e231f4f6d389412809f32b413da1e8006ba6..f7139cd048fb1bad87dfd0f445ab540a9fb3f8f3 100644 (file)
@@ -219,6 +219,19 @@ class Attachment extends DatabaseObject implements IRouteController, IThumbnailF
                return !$this->showAsImage();
        }
        
+       /**
+        * Returns icon name for this attachment.
+        * 
+        * @return      string
+        */
+       public function getIconName() {
+               if ($iconName = FileUtil::getIconNameByFilename($this->filename)) {
+                       return 'file-' . $iconName . '-o';
+               }
+               
+               return 'paperclip';
+       }
+       
        /**
         * Returns the storage path.
         * 
index a5fbba41f0a2e31a0869cd1e59b3d46d582feed8..8f5e767f4c5f054d1ca80853a30c42be171f58b8 100644 (file)
@@ -176,7 +176,8 @@ class AttachmentAction extends AbstractDatabaseObjectAction implements ISortable
                                        'thumbnailURL' => $attachment->thumbnailType ? LinkHandler::getInstance()->getLink('Attachment', ['object' => $attachment], 'thumbnail=1') : '',
                                        'url' => LinkHandler::getInstance()->getLink('Attachment', ['object' => $attachment]),
                                        'height' => $attachment->height,
-                                       'width' => $attachment->width
+                                       'width' => $attachment->width,
+                                       'iconName' => $attachment->getIconName()
                                ];
                        }
                }
index cc6dfddbb681674b59558572c1f68cc3129e1112..3d9b9f19a6b051f7379e03ee9eb7dbb62eb527ff 100644 (file)
@@ -3,6 +3,7 @@ namespace wcf\data\media;
 use wcf\data\user\UserProfile;
 use wcf\data\DatabaseObjectDecorator;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
+use wcf\util\FileUtil;
 use wcf\util\StringUtil;
 
 /**
@@ -70,7 +71,8 @@ class ViewableMedia extends DatabaseObjectDecorator {
                        }
                }
                
-               return '<span class="icon icon'.$size.' fa-file-o"></span>';
+               $icon = FileUtil::getIconNameByFilename($this->filename);
+               return '<span class="icon icon' . $size . ' fa-file' . ($icon ? '-' . $icon : '') . '-o"></span>';
        }
        
        /**
index 43c90e58b244acc025f0758dc312add2ff265be5..a08177b8ec0c895cb5b973373f99212db0f70d0d 100644 (file)
@@ -638,6 +638,47 @@ final class FileUtil {
                return self::getMemoryLimit() == -1 || self::getMemoryLimit() > (memory_get_usage() + $neededMemory);
        }
        
+       /**
+        * Returns icon name for given filename.
+        * 
+        * @param       string          $filename
+        * @return      string
+        */
+       public static function getIconNameByFilename($filename) {
+               static $mapping = [
+                       // archive
+                       'zip' => 'archive', 'rar' => 'archive', 'tar' => 'archive', 'gz' => 'archive',
+                       // audio
+                       'mp3' => 'audio', 'ogg' => 'audio', 'wav' => 'audio',
+                       // code
+                       'php' => 'code', 'html' => 'code', 'htm' => 'code', 'tpl' => 'code', 'js' => 'code',
+                       // excel
+                       'xls' => 'excel', 'ods' => 'excel', 'xlsx' => 'excel',
+                       // image
+                       'gif' => 'image', 'jpg' => 'image', 'jpeg' => 'image', 'png' => 'image', 'bmp' => 'image',
+                       // video
+                       'avi' => 'video', 'wmv' => 'video', 'mov' => 'video', 'mp4' => 'video', 'mpg' => 'video', 'mpeg' => 'video', 'flv' => 'video',
+                       // pdf
+                       'pdf' => 'pdf',
+                       // powerpoint
+                       'ppt' => 'powerpoint', 'pptx' => 'powerpoint',
+                       // text
+                       'txt' => 'text',
+                       // word
+                       'doc' => 'word', 'docx' => 'word', 'odt' => 'word'
+               ];
+               
+               $lastDotPosition = strrpos($filename, '.');
+               if ($lastDotPosition !== false) {
+                       $extension = substr($filename, $lastDotPosition + 1);
+                       if (isset($mapping[$extension])) {
+                               return $mapping[$extension];
+                       }
+               }
+               
+               return '';
+       }
+       
        /**
         * Forbid creation of FileUtil objects.
         */