| @ -0,0 +1,68 @@ | |||||
| const imagePathPrefix = process.env.VUE_APP_IMAGE_PATH_PREFIX || ""; | |||||
| function joinPath(left, right) { | |||||
| return `${left.replace(/\/+$/, "")}/${right.replace(/^\/+/, "")}`; | |||||
| } | |||||
| function pathContainsPrefix(value, prefix) { | |||||
| if (!value || !prefix) { | |||||
| return false; | |||||
| } | |||||
| try { | |||||
| const pathname = /^https?:\/\//i.test(value) | |||||
| ? new URL(value).pathname | |||||
| : value; | |||||
| const normalizedPathname = pathname.replace(/\/+$/, ""); | |||||
| return ( | |||||
| normalizedPathname === prefix || | |||||
| normalizedPathname.endsWith(prefix) | |||||
| ); | |||||
| } catch (error) { | |||||
| return false; | |||||
| } | |||||
| } | |||||
| export function buildImageUrl(ftpViewAddress, imagePath) { | |||||
| if (!imagePath) { | |||||
| return ""; | |||||
| } | |||||
| const path = String(imagePath).trim(); | |||||
| if (/^(data:|blob:)/i.test(path)) { | |||||
| return path; | |||||
| } | |||||
| if (/^https?:\/\//i.test(path)) { | |||||
| try { | |||||
| const absoluteUrl = new URL(path); | |||||
| const normalizedPrefix = imagePathPrefix.replace(/\/+$/, ""); | |||||
| if ( | |||||
| !imagePathPrefix || | |||||
| absoluteUrl.pathname === normalizedPrefix || | |||||
| absoluteUrl.pathname.startsWith(`${normalizedPrefix}/`) | |||||
| ) { | |||||
| return path; | |||||
| } | |||||
| absoluteUrl.pathname = joinPath(imagePathPrefix, absoluteUrl.pathname); | |||||
| return absoluteUrl.href; | |||||
| } catch (error) { | |||||
| return path; | |||||
| } | |||||
| } | |||||
| const normalizedPrefix = imagePathPrefix.replace(/\/+$/, ""); | |||||
| if (ftpViewAddress && pathContainsPrefix(ftpViewAddress, normalizedPrefix)) { | |||||
| const relativePath = path.startsWith(normalizedPrefix) | |||||
| ? path.slice(normalizedPrefix.length) | |||||
| : path; | |||||
| return joinPath(ftpViewAddress, relativePath); | |||||
| } | |||||
| const prefixedPath = | |||||
| normalizedPrefix && !path.startsWith(normalizedPrefix) | |||||
| ? joinPath(normalizedPrefix, path) | |||||
| : path; | |||||
| return ftpViewAddress ? joinPath(ftpViewAddress, prefixedPath) : prefixedPath; | |||||
| } | |||||