Refactor file-extension checking into utility functions

This commit is contained in:
JC Brand 2018-10-16 00:22:39 +02:00
parent 0c58cb7c48
commit 82239d281f
4 changed files with 100 additions and 47 deletions

View File

@ -5,6 +5,7 @@
- Bugfix. Handler not triggered when submitting MUC password form 2nd time
- Bugfix. MUC features weren't being refreshed when saving the config form
- Don't show duplicate notification messages
- New config setting [show_images_inline](https://conversejs.org/docs/html/configuration.html#show-images-inline)
- #537 Render `xmpp:` URI as link
- #1062 Collapse multiple join/leave messages into one
- #1063 URLs in the topic / subject are not clickable
@ -17,6 +18,7 @@
- #1214 Setting `allow_contact_requests` to `false` has no effect
- #1221 Avoid creating a headlines box if we don't have anything to show inside it
- #1222 Adding a bookmark should prefill the room name
- #1228 Converse automatically visits links (to try and determine whether they're images to show inline)
## 4.0.2 (2018-10-02)

71
dist/converse.js vendored
View File

@ -81839,30 +81839,60 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
};
u.renderFileURL = function (_converse, url) {
const uri = new URI(url),
filename = uri.filename(),
lower_filename = filename.toLowerCase();
const uri = new URI(url);
if (!_.includes(["https", "http"], uri.protocol().toLowerCase()) || lower_filename.endsWith('mp3') || lower_filename.endsWith('mp4') || lower_filename.endsWith('ogg') || lower_filename.endsWith('jpg') || lower_filename.endsWith('jpeg') || lower_filename.endsWith('png') || lower_filename.endsWith('gif') || lower_filename.endsWith('m4a') || lower_filename.endsWith('webm') || lower_filename.endsWith('svg')) {
if (u.isImageURL(uri) || u.isVideoURL(uri) || u.isAudioURL(uri)) {
return url;
}
const __ = _converse.__;
const __ = _converse.__,
filename = uri.filename();
return tpl_file({
'url': url,
'label_download': __('Download file "%1$s"', decodeURI(filename))
});
};
u.isImageURL = function (url) {
const uri = new URI(url),
filename = uri.filename().toLowerCase();
u.isAudioURL = function (url) {
if (!(url instanceof URI)) {
url = new URI(url);
}
if (!_.includes(["https", "http"], uri.protocol().toLowerCase())) {
const filename = url.filename().toLowerCase();
if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
return false;
}
return filename.endsWith('jpg') || filename.endsWith('jpeg') || filename.endsWith('png') || filename.endsWith('gif') || filename.endsWith('svg');
return filename.endsWith('.ogg') || filename.endsWith('.mp3') || filename.endsWith('.m4a');
};
u.isVideoURL = function (url) {
if (!(url instanceof URI)) {
url = new URI(url);
}
const filename = url.filename().toLowerCase();
if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
return false;
}
return filename.endsWith('.mp4') || filename.endsWith('.webm');
};
u.isImageURL = function (url) {
if (!(url instanceof URI)) {
url = new URI(url);
}
const filename = url.filename().toLowerCase();
if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
return false;
}
return filename.endsWith('.jpg') || filename.endsWith('.jpeg') || filename.endsWith('.png') || filename.endsWith('.gif') || filename.endsWith('.bmp') || filename.endsWith('.tiff') || filename.endsWith('.svg');
};
u.renderImageURL = function (_converse, url) {
@ -81870,9 +81900,10 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
return u.addHyperlinks(url);
}
if (u.isImageURL(url)) {
const __ = _converse.__,
uri = new URI(url);
const uri = new URI(url);
if (u.isImageURL(uri)) {
const __ = _converse.__;
return tpl_image({
'url': url,
'label_download': __('Download image "%1$s"', decodeURI(uri.filename()))
@ -81883,9 +81914,10 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
};
u.renderMovieURL = function (_converse, url) {
if (url.endsWith('mp4') || url.endsWith('webm')) {
const __ = _converse.__,
uri = new URI(url);
const uri = new URI(url);
if (u.isVideoURL(uri)) {
const __ = _converse.__;
return tpl_video({
'url': url,
'label_download': __('Download video file "%1$s"', decodeURI(uri.filename()))
@ -81896,9 +81928,10 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
};
u.renderAudioURL = function (_converse, url) {
if (url.endsWith('mp3') || url.endsWith('m4a') || url.endsWith('ogg')) {
const __ = _converse.__,
uri = new URI(url);
const uri = new URI(url);
if (u.isAudioURL(uri)) {
const __ = _converse.__;
return tpl_audio({
'url': url,
'label_download': __('Download audio file "%1$s"', decodeURI(uri.filename()))

2
package-lock.json generated
View File

@ -2252,7 +2252,7 @@
}
},
"backbone.browserStorage": {
"version": "github:jcbrand/Backbone.browserStorage#7079bf7bf9a43474da1d48e31e3cda6c4a716382",
"version": "github:jcbrand/Backbone.browserStorage#78e4a58f7cee10cad6af4fd5f3213331a396aa5a",
"dev": true,
"requires": {
"backbone": "1.3.3",

View File

@ -318,43 +318,61 @@
};
u.renderFileURL = function (_converse, url) {
const uri = new URI(url),
filename = uri.filename(),
lower_filename = filename.toLowerCase();
if (!_.includes(["https", "http"], uri.protocol().toLowerCase()) ||
lower_filename.endsWith('mp3') || lower_filename.endsWith('mp4') || lower_filename.endsWith('ogg') ||
lower_filename.endsWith('jpg') || lower_filename.endsWith('jpeg') ||
lower_filename.endsWith('png') || lower_filename.endsWith('gif') ||
lower_filename.endsWith('m4a') || lower_filename.endsWith('webm') ||
lower_filename.endsWith('svg')) {
const uri = new URI(url);
if (u.isImageURL(uri) || u.isVideoURL(uri) || u.isAudioURL(uri)) {
return url;
}
const { __ } = _converse;
const { __ } = _converse,
filename = uri.filename();
return tpl_file({
'url': url,
'label_download': __('Download file "%1$s"', decodeURI(filename))
})
};
u.isImageURL = function (url) {
const uri = new URI(url),
filename = uri.filename().toLowerCase();
if (!_.includes(["https", "http"], uri.protocol().toLowerCase())) {
u.isAudioURL = function (url) {
if (!(url instanceof URI)) {
url = new URI(url);
}
const filename = url.filename().toLowerCase();
if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
return false;
}
return filename.endsWith('jpg') || filename.endsWith('jpeg') ||
filename.endsWith('png') || filename.endsWith('gif') ||
filename.endsWith('svg');
return filename.endsWith('.ogg') || filename.endsWith('.mp3') || filename.endsWith('.m4a');
}
u.isVideoURL = function (url) {
if (!(url instanceof URI)) {
url = new URI(url);
}
const filename = url.filename().toLowerCase();
if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
return false;
}
return filename.endsWith('.mp4') || filename.endsWith('.webm');
}
u.isImageURL = function (url) {
if (!(url instanceof URI)) {
url = new URI(url);
}
const filename = url.filename().toLowerCase();
if (!_.includes(["https", "http"], url.protocol().toLowerCase())) {
return false;
}
return filename.endsWith('.jpg') || filename.endsWith('.jpeg') ||
filename.endsWith('.png') || filename.endsWith('.gif') ||
filename.endsWith('.bmp') || filename.endsWith('.tiff') ||
filename.endsWith('.svg');
};
u.renderImageURL = function (_converse, url) {
if (!_converse.show_images_inline) {
return u.addHyperlinks(url);
}
if (u.isImageURL(url)) {
const { __ } = _converse,
uri = new URI(url);
const uri = new URI(url);
if (u.isImageURL(uri)) {
const { __ } = _converse;
return tpl_image({
'url': url,
'label_download': __('Download image "%1$s"', decodeURI(uri.filename()))
@ -364,9 +382,9 @@
};
u.renderMovieURL = function (_converse, url) {
if (url.endsWith('mp4') || url.endsWith('webm')) {
const { __ } = _converse,
uri = new URI(url);
const uri = new URI(url);
if (u.isVideoURL(uri)) {
const { __ } = _converse;
return tpl_video({
'url': url,
'label_download': __('Download video file "%1$s"', decodeURI(uri.filename()))
@ -376,9 +394,9 @@
};
u.renderAudioURL = function (_converse, url) {
if (url.endsWith('mp3') || url.endsWith('m4a') || url.endsWith('ogg')) {
const { __ } = _converse,
uri = new URI(url);
const uri = new URI(url);
if (u.isAudioURL(uri)) {
const { __ } = _converse;
return tpl_audio({
'url': url,
'label_download': __('Download audio file "%1$s"', decodeURI(uri.filename()))