1
|
|
|
/** |
2
|
|
|
* Nextcloud - Gallery |
3
|
|
|
* |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Olivier Paroz <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @copyright Olivier Paroz 2017 |
11
|
|
|
*/ |
12
|
|
|
/* global $, DOMPurify, OC, Gallery */ |
13
|
|
|
/** |
14
|
|
|
* A thumbnail is the actual image attached to the GalleryImage object |
15
|
|
|
* |
16
|
|
|
* @param {number} fileId |
17
|
|
|
* @param {boolean} square |
18
|
|
|
* @constructor |
19
|
|
|
*/ |
20
|
|
|
function Thumbnail (fileId, square) { |
21
|
|
|
this.square = square; |
22
|
|
|
this.fileId = fileId; |
23
|
|
|
this.image = null; |
24
|
|
|
this.loadingDeferred = new $.Deferred(); |
25
|
|
|
this.height = 200; |
26
|
|
|
this.width = 400; |
27
|
|
|
this.ratio = null; |
28
|
|
|
this.valid = true; |
29
|
|
|
this.status = 200; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
(function ($, OC, Gallery) { |
33
|
|
|
"use strict"; |
34
|
|
|
var Thumbnails = { |
35
|
|
|
map: {}, |
36
|
|
|
squareMap: {}, |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Retrieves the thumbnail linked to the given fileID |
40
|
|
|
* |
41
|
|
|
* @param {number} fileId |
42
|
|
|
* @param {boolean} square |
43
|
|
|
* |
44
|
|
|
* @returns {Thumbnail} |
45
|
|
|
*/ |
46
|
|
|
get: function (fileId, square) { |
47
|
|
|
var map = {}; |
48
|
|
|
if (square === true) { |
49
|
|
|
map = Thumbnails.squareMap; |
50
|
|
|
square = true; |
51
|
|
|
} else { |
52
|
|
|
map = Thumbnails.map; |
53
|
|
|
square = false; |
54
|
|
|
} |
55
|
|
|
if (!map[fileId]) { |
56
|
|
|
map[fileId] = new Thumbnail(fileId, square); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return map[fileId]; |
60
|
|
|
}, |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns an icon of a specific type |
64
|
|
|
* |
65
|
|
|
* -1 is for a folder |
66
|
|
|
* -404 is for a broken file icon |
67
|
|
|
* -500 is for a media type icon |
68
|
|
|
* |
69
|
|
|
* @param {number} type |
70
|
|
|
* |
71
|
|
|
* @returns {Thumbnail} |
72
|
|
|
*/ |
73
|
|
|
getStandardIcon: function (type) { |
74
|
|
|
if (!Thumbnails.squareMap[type]) { |
75
|
|
|
var icon = ''; |
76
|
|
|
// true means square |
77
|
|
|
var thumb = new Thumbnail(type, true); |
78
|
|
|
thumb.image = new Image(); |
|
|
|
|
79
|
|
|
thumb.image.onload = function () { |
80
|
|
|
thumb.loadingDeferred.resolve(thumb.image); |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
if (type === -1) { |
84
|
|
|
icon = 'filetypes/folder'; |
85
|
|
|
} |
86
|
|
|
thumb.image.src = OC.imagePath('core', icon); |
87
|
|
|
|
88
|
|
|
Thumbnails.squareMap[type] = thumb; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return Thumbnails.squareMap[type]; |
92
|
|
|
}, |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Loads thumbnails in batch, using EventSource |
96
|
|
|
* |
97
|
|
|
* @param {Array} ids |
98
|
|
|
* @param {boolean} square |
99
|
|
|
* |
100
|
|
|
* @returns {{}} |
101
|
|
|
*/ |
102
|
|
|
loadBatch: function (ids, square) { |
103
|
|
|
var map = (square) ? Thumbnails.squareMap : Thumbnails.map; |
104
|
|
|
// Prevents re-loading thumbnails when resizing the window |
105
|
|
|
ids = ids.filter(function (id) { |
106
|
|
|
return !map[id]; |
107
|
|
|
}); |
108
|
|
|
var batch = {}; |
109
|
|
|
var i, idsLength = ids.length; |
|
|
|
|
110
|
|
|
if (idsLength) { |
111
|
|
|
_.each(ids, function(id) { |
112
|
|
|
var thumb = new Thumbnail(id, square); |
113
|
|
|
thumb.image = new Image(); |
|
|
|
|
114
|
|
|
map[id] = batch[id] = thumb; |
115
|
|
|
|
116
|
|
|
thumb.image.onload = function () { |
117
|
|
|
if (square) { |
118
|
|
|
thumb.image.width = 200; |
119
|
|
|
thumb.image.height = 200; |
120
|
|
|
} |
121
|
|
|
thumb.ratio = thumb.image.width / thumb.image.height; |
122
|
|
|
thumb.image.originalWidth = 200 * thumb.ratio; |
123
|
|
|
thumb.valid = true; |
124
|
|
|
thumb.status = 200; |
125
|
|
|
thumb.loadingDeferred.resolve(thumb.image); |
126
|
|
|
console.log(thumb); |
|
|
|
|
127
|
|
|
}; |
128
|
|
|
thumb.image.onerror = function (data) { |
|
|
|
|
129
|
|
|
thumb.loadingDeferred.resolve(null); |
130
|
|
|
}; |
131
|
|
|
var width = square ? 200 : 400; |
132
|
|
|
thumb.image.src = Gallery.utility.buildGalleryUrl('preview', '/' + id, {width: width, height: 200}); |
133
|
|
|
}); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return batch; |
137
|
|
|
}, |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Sanitises SVGs |
141
|
|
|
* |
142
|
|
|
* We also fix a problem which arises when the XML contains comments |
143
|
|
|
* |
144
|
|
|
* @param imageData |
145
|
|
|
* @returns {string|*} |
146
|
|
|
* @private |
147
|
|
|
*/ |
148
|
|
|
_purifySvg: function (imageData) { |
149
|
|
|
var pureSvg = DOMPurify.sanitize(window.atob(imageData), {ADD_TAGS: ['filter']}); |
150
|
|
|
// Remove XML comment garbage left in the purified data |
151
|
|
|
var badTag = pureSvg.indexOf(']>'); |
152
|
|
|
var fixedPureSvg = pureSvg.substring(badTag < 0 ? 0 : 5, pureSvg.length); |
153
|
|
|
imageData = window.btoa(fixedPureSvg); |
154
|
|
|
|
155
|
|
|
return imageData; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
}; |
159
|
|
|
|
160
|
|
|
window.Thumbnails = Thumbnails; |
161
|
|
|
})(jQuery, OC, Gallery); |
162
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.