Skip to content

Commit a46a0a9

Browse files
committed
Handle URL rewriting
1 parent afd75b6 commit a46a0a9

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

script/helpers.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,52 @@ function extractNs(url) {
5454
}
5555

5656
/**
57-
* extract image id from fetch media URL
58-
* FIXME handle url rewriting
57+
* extract image id from media URL
5958
*
6059
* @param {string} url
6160
* @returns {string}
6261
*/
6362
function extractIdFromMediaUrl(url) {
64-
if (typeof url === 'undefined') {
65-
return '';
63+
// handle empty base url
64+
if (url.indexOf('http') !== 0 && DOKU_BASE === '/') {
65+
url = window.location.origin + url;
6666
}
67-
return url.split('media=')[1].split('&')[0];
67+
68+
const urlObj = new URL(url);
69+
const path = urlObj.pathname;
70+
const href = urlObj.href;
71+
72+
if (path.indexOf('/lib/exe/detail.php/') === 0 || path.indexOf('/lib/exe/fetch.php/') === 0) {
73+
// media with internal rewriting
74+
const mediaIdMatch = new RegExp(
75+
'(?:\\/lib\\/exe\\/detail.php\\/|\\/lib\\/exe\\/fetch.php\\/)([^&]+)$'
76+
);
77+
const matches = mediaIdMatch.exec(path);
78+
if (matches[1]) {
79+
return normalizeId(matches[1]);
80+
}
81+
} else if (path.indexOf('/lib/exe/detail.php') === 0 || path.indexOf('/lib/exe/fetch.php') === 0) {
82+
// media without rewriting
83+
const mediaIdMatch = new RegExp('(?:media=)([^&]+)');
84+
const matches = mediaIdMatch.exec(href);
85+
if (matches[1]) {
86+
return normalizeId(matches[1]);
87+
}
88+
} else if (path.indexOf('/_media/') === 0) { // media with .htaccess rewriting
89+
const mediaIdMatch = /(?:_media\/)([^&\?]+)/;
90+
const matches = href.match(mediaIdMatch);
91+
if (matches[1]) {
92+
return normalizeId(matches[1]);
93+
}
94+
} else if (path.indexOf('/_detail/') === 0) { // media with .htaccess rewriting
95+
const mediaIdMatch = /(?:_detail\/)([^&\?]+)/;
96+
const matches = href.match(mediaIdMatch);
97+
if (matches[1]) {
98+
return normalizeId(matches[1]);
99+
}
100+
}
101+
}
102+
103+
function normalizeId(id) {
104+
return ':' + id.replace(/\//g, ":");
68105
}

0 commit comments

Comments
 (0)