2019-10-30 21:48:24 +08:00
|
|
|
import { debounce } from "@ember/runloop";
|
2019-11-01 00:54:46 +08:00
|
|
|
import { ATTACHMENT_CSS_CLASS } from "./engines/discourse-markdown-it";
|
2019-05-29 09:00:25 +08:00
|
|
|
let _cache = {};
|
|
|
|
|
|
|
|
export function lookupCachedUploadUrl(shortUrl) {
|
|
|
|
return _cache[shortUrl] || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const MISSING = "missing";
|
|
|
|
|
|
|
|
export function lookupUncachedUploadUrls(urls, ajax) {
|
2020-03-03 12:44:01 +08:00
|
|
|
urls = _.compact(urls);
|
|
|
|
if (urls.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-29 09:00:25 +08:00
|
|
|
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-03-04 07:11:08 +08:00
|
|
|
function retrieveCachedUrl($upload, siteSettings, dataAttribute, callback) {
|
2020-01-23 07:41:39 +08:00
|
|
|
const cachedUpload = lookupCachedUploadUrl($upload.data(dataAttribute));
|
2020-03-04 07:11:08 +08:00
|
|
|
const url = getAttributeBasedUrl(dataAttribute, cachedUpload, siteSettings);
|
2020-01-23 07:41:39 +08:00
|
|
|
|
|
|
|
if (url) {
|
|
|
|
$upload.removeAttr(`data-${dataAttribute}`);
|
|
|
|
if (url !== MISSING) {
|
|
|
|
callback(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 07:11:08 +08:00
|
|
|
function getAttributeBasedUrl(dataAttribute, cachedUpload, siteSettings) {
|
|
|
|
if (!cachedUpload.url) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// non-attachments always use the full URL
|
|
|
|
if (dataAttribute !== "orig-href") {
|
|
|
|
return cachedUpload.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// attachments should use the full /secure-media-uploads/ URL
|
|
|
|
// in this case for permission checks
|
|
|
|
if (
|
|
|
|
siteSettings.secure_media &&
|
|
|
|
cachedUpload.url.indexOf("secure-media-uploads") > -1
|
|
|
|
) {
|
|
|
|
return cachedUpload.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedUpload.short_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _loadCachedShortUrls($uploads, siteSettings) {
|
2020-01-23 07:41:39 +08:00
|
|
|
$uploads.each((_idx, upload) => {
|
2019-05-29 09:00:25 +08:00
|
|
|
const $upload = $(upload);
|
|
|
|
switch (upload.tagName) {
|
|
|
|
case "A":
|
2020-03-04 07:11:08 +08:00
|
|
|
retrieveCachedUrl($upload, siteSettings, "orig-href", url => {
|
2020-01-23 07:41:39 +08:00
|
|
|
$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-29 09:00:25 +08:00
|
|
|
}
|
2020-01-23 07:41:39 +08:00
|
|
|
});
|
2019-05-29 09:00:25 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
case "IMG":
|
2020-03-04 07:11:08 +08:00
|
|
|
retrieveCachedUrl($upload, siteSettings, "orig-src", url => {
|
2020-01-23 07:41:39 +08:00
|
|
|
$upload.attr("src", url);
|
|
|
|
});
|
2019-05-29 09:00:25 +08:00
|
|
|
|
2020-01-23 07:41:39 +08:00
|
|
|
break;
|
2020-03-03 12:44:01 +08:00
|
|
|
case "SOURCE": // video/audio tag > source tag
|
2020-03-04 07:11:08 +08:00
|
|
|
retrieveCachedUrl($upload, siteSettings, "orig-src", url => {
|
2020-01-23 07:41:39 +08:00
|
|
|
$upload.attr("src", url);
|
2019-05-29 09:00:25 +08:00
|
|
|
|
2020-01-23 07:41:39 +08:00
|
|
|
if (url.startsWith(`//${window.location.host}`)) {
|
|
|
|
let hostRegex = new RegExp("//" + window.location.host, "g");
|
|
|
|
url = url.replace(hostRegex, "");
|
2019-05-29 09:00:25 +08:00
|
|
|
}
|
2020-03-03 12:44:01 +08:00
|
|
|
let fullUrl = window.location.origin + url;
|
|
|
|
$upload.attr("src", fullUrl);
|
2020-01-23 07:41:39 +08:00
|
|
|
|
|
|
|
// this is necessary, otherwise because of the src change the
|
2020-03-03 12:44:01 +08:00
|
|
|
// video/audio just doesn't bother loading!
|
|
|
|
let $parent = $upload.parent();
|
|
|
|
$parent[0].load();
|
|
|
|
|
|
|
|
// set the url and text for the <a> tag within the <video/audio> tag
|
|
|
|
$parent
|
|
|
|
.find("a")
|
|
|
|
.attr("href", fullUrl)
|
|
|
|
.text(fullUrl);
|
2020-01-23 07:41:39 +08:00
|
|
|
});
|
2019-05-29 09:00:25 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-04 07:11:08 +08:00
|
|
|
function _loadShortUrls($uploads, ajax, siteSettings) {
|
2020-03-03 12:44:01 +08:00
|
|
|
let urls = $uploads.toArray().map(upload => {
|
2019-05-29 09:00:25 +08:00
|
|
|
const $upload = $(upload);
|
|
|
|
return $upload.data("orig-src") || $upload.data("orig-href");
|
|
|
|
});
|
|
|
|
|
|
|
|
return lookupUncachedUploadUrls(urls, ajax).then(() =>
|
2020-03-04 07:11:08 +08:00
|
|
|
_loadCachedShortUrls($uploads, siteSettings)
|
2019-05-29 09:00:25 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-04 07:11:08 +08:00
|
|
|
export function resolveAllShortUrls(ajax, siteSettings, scope = null) {
|
2020-01-23 07:41:39 +08:00
|
|
|
const attributes =
|
|
|
|
"img[data-orig-src], a[data-orig-href], source[data-orig-src]";
|
2020-03-03 12:44:01 +08:00
|
|
|
let $shortUploadUrls = $(scope || document).find(attributes);
|
2019-05-29 09:00:25 +08:00
|
|
|
|
|
|
|
if ($shortUploadUrls.length > 0) {
|
2020-03-04 07:11:08 +08:00
|
|
|
_loadCachedShortUrls($shortUploadUrls, siteSettings);
|
2019-05-29 09:00:25 +08:00
|
|
|
|
2020-03-03 12:44:01 +08:00
|
|
|
$shortUploadUrls = $(scope || document).find(attributes);
|
2019-05-29 09:00:25 +08:00
|
|
|
if ($shortUploadUrls.length > 0) {
|
|
|
|
// this is carefully batched so we can do a leading debounce (trigger right away)
|
2019-10-30 21:48:24 +08:00
|
|
|
return debounce(
|
2019-05-29 09:00:25 +08:00
|
|
|
null,
|
2020-03-04 07:11:08 +08:00
|
|
|
() => _loadShortUrls($shortUploadUrls, ajax, siteSettings),
|
2019-05-29 09:00:25 +08:00
|
|
|
450,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|