2019-10-30 09:48:24 -04:00
|
|
|
import { debounce } from "@ember/runloop";
|
2019-10-31 12:54:46 -04:00
|
|
|
import { ATTACHMENT_CSS_CLASS } from "./engines/discourse-markdown-it";
|
2019-05-28 21:00:25 -04:00
|
|
|
let _cache = {};
|
|
|
|
|
|
|
|
export function lookupCachedUploadUrl(shortUrl) {
|
|
|
|
return _cache[shortUrl] || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const MISSING = "missing";
|
|
|
|
|
|
|
|
export function lookupUncachedUploadUrls(urls, ajax) {
|
|
|
|
return ajax("/uploads/lookup-urls", {
|
|
|
|
method: "POST",
|
|
|
|
data: { short_urls: urls }
|
|
|
|
}).then(uploads => {
|
|
|
|
uploads.forEach(upload => {
|
|
|
|
cacheShortUploadUrl(upload.short_url, {
|
|
|
|
url: upload.url,
|
|
|
|
short_path: upload.short_path
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
urls.forEach(url =>
|
|
|
|
cacheShortUploadUrl(url, {
|
|
|
|
url: lookupCachedUploadUrl(url).url || MISSING,
|
|
|
|
short_path: lookupCachedUploadUrl(url).short_path || MISSING
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return uploads;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cacheShortUploadUrl(shortUrl, value) {
|
|
|
|
_cache[shortUrl] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resetCache() {
|
|
|
|
_cache = {};
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:41:39 -05:00
|
|
|
function retrieveCachedUrl($upload, dataAttribute, callback) {
|
|
|
|
const cachedUpload = lookupCachedUploadUrl($upload.data(dataAttribute));
|
|
|
|
const url =
|
|
|
|
dataAttribute === "orig-href" ? cachedUpload.short_path : cachedUpload.url;
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
$upload.removeAttr(`data-${dataAttribute}`);
|
|
|
|
if (url !== MISSING) {
|
|
|
|
callback(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 21:00:25 -04:00
|
|
|
function _loadCachedShortUrls($uploads) {
|
2020-01-22 18:41:39 -05:00
|
|
|
$uploads.each((_idx, upload) => {
|
2019-05-28 21:00:25 -04:00
|
|
|
const $upload = $(upload);
|
|
|
|
switch (upload.tagName) {
|
|
|
|
case "A":
|
2020-01-22 18:41:39 -05:00
|
|
|
retrieveCachedUrl($upload, "orig-href", url => {
|
|
|
|
$upload.attr("href", url);
|
|
|
|
|
|
|
|
// Replace "|attachment" with class='attachment'
|
|
|
|
// TODO: This is a part of the cooking process now and should be
|
|
|
|
// removed in the future.
|
|
|
|
const content = $upload.text().split("|");
|
|
|
|
if (content[1] === ATTACHMENT_CSS_CLASS) {
|
|
|
|
$upload.addClass(ATTACHMENT_CSS_CLASS);
|
|
|
|
$upload.text(content[0]);
|
2019-05-28 21:00:25 -04:00
|
|
|
}
|
2020-01-22 18:41:39 -05:00
|
|
|
});
|
2019-05-28 21:00:25 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
case "IMG":
|
2020-01-22 18:41:39 -05:00
|
|
|
retrieveCachedUrl($upload, "orig-src", url => {
|
|
|
|
$upload.attr("src", url);
|
|
|
|
});
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2020-01-22 18:41:39 -05:00
|
|
|
break;
|
|
|
|
case "SOURCE": // video tag > source tag
|
|
|
|
retrieveCachedUrl($upload, "orig-src", url => {
|
|
|
|
$upload.attr("src", url);
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2020-01-22 18:41:39 -05:00
|
|
|
if (url.startsWith(`//${window.location.host}`)) {
|
|
|
|
let hostRegex = new RegExp("//" + window.location.host, "g");
|
|
|
|
url = url.replace(hostRegex, "");
|
2019-05-28 21:00:25 -04:00
|
|
|
}
|
2020-01-22 18:41:39 -05:00
|
|
|
$upload.attr("src", window.location.origin + url);
|
|
|
|
|
|
|
|
// this is necessary, otherwise because of the src change the
|
|
|
|
// video just doesn't bother loading!
|
|
|
|
$upload.parent()[0].load();
|
|
|
|
});
|
2019-05-28 21:00:25 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _loadShortUrls($uploads, ajax) {
|
|
|
|
const urls = $uploads.toArray().map(upload => {
|
|
|
|
const $upload = $(upload);
|
|
|
|
return $upload.data("orig-src") || $upload.data("orig-href");
|
|
|
|
});
|
|
|
|
|
|
|
|
return lookupUncachedUploadUrls(urls, ajax).then(() =>
|
|
|
|
_loadCachedShortUrls($uploads)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveAllShortUrls(ajax) {
|
2020-01-22 18:41:39 -05:00
|
|
|
const attributes =
|
|
|
|
"img[data-orig-src], a[data-orig-href], source[data-orig-src]";
|
2019-05-28 21:00:25 -04:00
|
|
|
let $shortUploadUrls = $(attributes);
|
|
|
|
|
|
|
|
if ($shortUploadUrls.length > 0) {
|
|
|
|
_loadCachedShortUrls($shortUploadUrls);
|
|
|
|
|
|
|
|
$shortUploadUrls = $(attributes);
|
|
|
|
if ($shortUploadUrls.length > 0) {
|
|
|
|
// this is carefully batched so we can do a leading debounce (trigger right away)
|
2019-10-30 09:48:24 -04:00
|
|
|
return debounce(
|
2019-05-28 21:00:25 -04:00
|
|
|
null,
|
|
|
|
() => _loadShortUrls($shortUploadUrls, ajax),
|
|
|
|
450,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|