diff --git a/frontend/src/pages/addBookmark/index.jsx b/frontend/src/pages/addBookmark/index.jsx index 082c74d..ad24ecc 100644 --- a/frontend/src/pages/addBookmark/index.jsx +++ b/frontend/src/pages/addBookmark/index.jsx @@ -109,7 +109,9 @@ export default function AddPage() { setIsLoading(false); } else { console.error("error", res.statusText, res.status); - notify("Error", res.statusText, "error"); + let body = await res.json(); + let reason = body.message; + notify("Error", reason, "error"); setIsLoading(false); } } @@ -143,7 +145,9 @@ export default function AddPage() { }; const autocompleteUrl = () => { - if (!/^https?:\/\//i.test(url) && url !== "") { + try { + new URL(url); + } catch (error) { setUrl("https://" + url); } }; diff --git a/server/logics/bookmarks.js b/server/logics/bookmarks.js index ed47844..6b8fa79 100644 --- a/server/logics/bookmarks.js +++ b/server/logics/bookmarks.js @@ -10,6 +10,13 @@ export async function requestHeadFromUrl(url) { let res; const controller = new AbortController(); + let endpointUrl = new URL(url); + if (endpointUrl.protocol !== "http:" && endpointUrl.protocol !== "https:") { + return new Promise((resolve, reject) => { + reject(new Error(`Unsupported protocol ${endpointUrl.protocol}`)); + }); + } + try { res = await axios.get(url, { responseType: "stream", @@ -22,12 +29,12 @@ export async function requestHeadFromUrl(url) { }, }); } catch (error) { - console.error(url, error.message); + console.error(`Get Head tag from ${url}`, error.message); res = error.response; } - let contentType = res.headers["content-type"]; - let matches = contentType.match(/charset=\s*"?(.[^\"]+)"?$/i); + let contentType = res?.headers["content-type"]; + let matches = contentType?.match(/charset=\s*"?(.[^\"]+)"?$/i); let encoding = matches?.[1] || "utf-8"; let stream = res.data; @@ -70,21 +77,25 @@ export async function batchUpdateLinks() { console.log("Starting batch update all links"); bookmarks.forEach(async (bookmark) => { - let headHtml = requestHeadFromUrl(bookmark.url); - let urlInfo = await parseHtml(headHtml, bookmark.url); - let icon = await imgUrlToBase64(urlInfo.icon); - let updated = appContext.stores.bookmarks.updateItem( - bookmark.id, - bookmark.url, - urlInfo.title || bookmark.title, - urlInfo.desc || bookmark.desc, - icon, - bookmark.categoryId, - bookmark.tags - ); - updated - ? console.log(`${bookmark.url} updated!`) - : console.error(`${bookmark.url} not updated!`); + try { + let headHtml = await requestHeadFromUrl(bookmark.url); + let urlInfo = parseHtml(headHtml, bookmark.url); + let icon = await imgUrlToBase64(urlInfo.icon); + let updated = appContext.stores.bookmarks.updateItem( + bookmark.id, + bookmark.url, + urlInfo.title || bookmark.title, + urlInfo.desc || bookmark.desc, + icon, + bookmark.categoryId, + bookmark.tags + ); + updated + ? console.log(`${bookmark.url} updated!`) + : console.error(`${bookmark.url} not updated!`); + } catch (err) { + console.error(`${bookmark.url} - ${err.message}`); + } }); return true; }