FEATURE: replace GUID in image name uploaded from iOS (#5370)

This commit is contained in:
Gerhard Schlager 2017-11-28 11:05:34 +01:00 committed by GitHub
parent f37a1d5976
commit 41e70c410c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 10 deletions

View File

@ -1,10 +1,4 @@
export function isAppleDevice() {
// IE has no DOMNodeInserted so can not get this hack despite saying it is like iPhone
// This will apply hack on all iDevices
return navigator.userAgent.match(/(iPad|iPhone|iPod)/g) &&
navigator.userAgent.match(/Safari/g) &&
!navigator.userAgent.match(/Trident/g);
}
import { isAppleDevice } from 'discourse/lib/utilities';
// we can't tell what the actual visible window height is
// because we cannot account for the height of the mobile keyboard

View File

@ -1,4 +1,4 @@
import { isAppleDevice } from 'discourse/lib/safari-hacks';
import { isAppleDevice } from 'discourse/lib/utilities';
export default function(name, opts) {
opts = opts || {};

View File

@ -285,6 +285,21 @@ function uploadTypeFromFileName(fileName) {
return isAnImage(fileName) ? 'image' : 'attachment';
}
function isGUID(value) {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
}
function imageNameFromFileName(fileName) {
const split = fileName.split('.');
const name = split[split.length-2];
if (exports.isAppleDevice() && isGUID(name)) {
return I18n.t('upload_selector.default_image_alt_text');
}
return name;
}
export function allowsImages() {
return authorizesAllExtensions() || IMAGES_EXTENSIONS_REGEX.test(authorizedExtensions());
}
@ -309,8 +324,7 @@ export function uploadLocation(url) {
export function getUploadMarkdown(upload) {
if (isAnImage(upload.original_filename)) {
const split = upload.original_filename.split('.');
const name = split[split.length-2];
const name = imageNameFromFileName(upload.original_filename);
return `![${name}|${upload.width}x${upload.height}](${upload.short_url || upload.url})`;
} else if (!Discourse.SiteSettings.prevent_anons_from_downloading_files && (/\.(mov|mp4|webm|ogv|mp3|ogg|wav|m4a)$/i).test(upload.original_filename)) {
return uploadLocation(upload.url);
@ -399,5 +413,13 @@ export function determinePostReplaceSelection({ selection, needle, replacement }
}
}
export function isAppleDevice() {
// IE has no DOMNodeInserted so can not get this hack despite saying it is like iPhone
// This will apply hack on all iDevices
return navigator.userAgent.match(/(iPad|iPhone|iPod)/g) &&
navigator.userAgent.match(/Safari/g) &&
!navigator.userAgent.match(/Trident/g);
}
// This prevents a mini racer crash
export default {};

View File

@ -1355,6 +1355,7 @@ en:
uploading: "Uploading"
select_file: "Select File"
image_link: "link your image will point to"
default_image_alt_text: image
search:
sort_by: "Sort by"

View File

@ -16,6 +16,7 @@ import {
caretRowCol,
setCaretPosition
} from 'discourse/lib/utilities';
import * as Utilities from 'discourse/lib/utilities';
QUnit.module("lib:utilities");
@ -124,6 +125,13 @@ QUnit.test("getUploadMarkdown", assert => {
assert.ok(testUploadMarkdown("important.txt") === '<a class="attachment" href="/uploads/123/abcdef.ext">important.txt</a> (42 Bytes)\n');
});
QUnit.test("replaces GUID in image alt text on iOS", assert => {
assert.equal(testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"),'![8F2B469B-6B2C-4213-BC68-57B4876365A0|100x200](/uploads/123/abcdef.ext)');
sandbox.stub(Utilities, 'isAppleDevice').returns(true);
assert.equal(testUploadMarkdown("8F2B469B-6B2C-4213-BC68-57B4876365A0.jpeg"),'![image|100x200](/uploads/123/abcdef.ext)');
});
QUnit.test("isAnImage", assert => {
_.each(["png", "jpg", "jpeg", "bmp", "gif", "tif", "tiff", "ico"], function(extension) {
var image = "image." + extension;