]> source.dussan.org Git - nextcloud-server.git/commitdiff
Show mimeicon when versions don't have a preview
authorRoeland Jago Douma <roeland@famdouma.nl>
Tue, 29 Nov 2016 19:35:19 +0000 (20:35 +0100)
committerRoeland Jago Douma <roeland@famdouma.nl>
Tue, 29 Nov 2016 19:35:57 +0000 (20:35 +0100)
Fixes #2413

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
apps/files_versions/js/versioncollection.js
apps/files_versions/js/versionstabview.js
apps/files_versions/lib/Storage.php

index fdb12bae0a958941b84ae5033cfda8e056a40af5..24a4ba7fc626cbbe2780067711915277e048609e 100644 (file)
@@ -81,7 +81,8 @@
                                        name: version.name,
                                        fullPath: fullPath,
                                        timestamp: revision,
-                                       size: version.size
+                                       size: version.size,
+                                       mimetype: version.mimetype
                                };
                        });
                        this._endReached = result.data.endReached;
index 35abde6d34a3abbfd96c1c9b3479a7f8b2a50e40..075e0e9cddbc1afa35d7abe28e3e389331c01cc0 100644 (file)
@@ -15,7 +15,7 @@
                '<li data-revision="{{timestamp}}">' +
                '<div>' +
                '<div class="preview-container">' +
-               '<img class="preview" src="{{previewUrl}}"/>' +
+               '<img class="preview" src="{{previewUrl}}" width="44" height="44"/>' +
                '</div>' +
                '<div class="version-container">' +
                '<div>' +
                _onAddModel: function(model) {
                        var $el = $(this.itemTemplate(this._formatItem(model)));
                        this.$versionsContainer.append($el);
+
+                       var preview = $el.find('.preview')[0];
+                       this._lazyLoadPreview({
+                               url: model.getPreviewUrl(),
+                               mime: model.get('mimetype'),
+                               callback: function(url) {
+                                       preview.src = url;
+                               }
+                       });
                        $el.find('.has-tooltip').tooltip();
                },
 
                                downloadUrl: version.getDownloadUrl(),
                                downloadIconUrl: OC.imagePath('core', 'actions/download'),
                                revertIconUrl: OC.imagePath('core', 'actions/history'),
-                               previewUrl: version.getPreviewUrl(),
                                revertLabel: t('files_versions', 'Restore'),
                                canRevert: (this.collection.getFileInfo().get('permissions') & OC.PERMISSION_UPDATE) !== 0
                        }, version.attributes);
                                return false;
                        }
                        return !fileInfo.isDirectory();
+               },
+
+               /**
+                * Lazy load a file's preview.
+                *
+                * @param path path of the file
+                * @param mime mime type
+                * @param callback callback function to call when the image was loaded
+                * @param etag file etag (for caching)
+                */
+               _lazyLoadPreview : function(options) {
+                       var self = this;
+                       var url = options.url;
+                       var mime = options.mime;
+                       var ready = options.callback;
+
+                       // get mime icon url
+                       var iconURL = OC.MimeType.getIconUrl(mime);
+                       var previewURL,
+                               urlSpec = {};
+                       ready(iconURL); // set mimeicon URL
+
+                       var img = new Image();
+                       img.onload = function(){
+                               // if loading the preview image failed (no preview for the mimetype) then img.width will < 5
+                               if (img.width > 5) {
+                                       ready(url, img);
+                               } else if (options.error) {
+                                       options.error();
+                               }
+                       };
+                       if (options.error) {
+                               img.onerror = options.error;
+                       }
+                       img.src = url;
                }
        });
 
index e7c2c8b7593224a85e774352c2ea356984fbe930..1aa19274c48cacdee04777715dca08c94165c0c9 100644 (file)
@@ -467,6 +467,7 @@ class Storage {
                                                $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
                                                $versions[$key]['name'] = $versionedFile;
                                                $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
+                                               $versions[$key]['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($versionedFile);
                                        }
                                }
                        }