'use strict'; function isHttpUrl(raw) { return raw.protocol === 'http:' || raw.protocol === 'https:'; } function resolveImgbbEndpoint(rawUrl) { let u; try { u = new URL(String(rawUrl || '').trim()); } catch { throw new Error('URL API загрузки картинок не является корректным URL'); } if (!isHttpUrl(u)) { throw new Error('URL API загрузки картинок должен быть http(s) URL'); } u.search = ''; u.hash = ''; const path = u.pathname.replace(/\/+$/, ''); const isImgbb = /(^|\.)imgbb\.com$/.test(u.hostname); if (isImgbb) { u.pathname = path ? path : '/1/upload'; return { url: u.toString(), field: 'image' }; } if (path === '') { u.pathname = '/api/1/upload'; } else if (/\/api$/.test(path) && !/\/1\/upload$/.test(path)) { u.pathname = path + '/1/upload'; } return { url: u.toString(), field: 'source' }; } function friendlyApiError(status, body) { let text = String(body || '').slice(0, 300); try { const j = JSON.parse(String(body || '').slice(0, 5000)); if (j && j.error && j.error.message) text = 'сервис запретил загрузку: ' + j.error.message; } catch {} if (status === 403 || /forbidden/i.test(text)) { return (text ? text + '. ' : '') + 'Гостевой API-ключ публичный и ограничен сервисом: создайте аккаунт на сайте сервиса и укажите личный API-ключ в настройках'; } return text; } module.exports = { resolveImgbbEndpoint, friendlyApiError };