Skip to content

Commit

Permalink
Merge pull request #99 from AlexSciFier/1-4-14
Browse files Browse the repository at this point in the history
fix: Unsupported protocol #97
  • Loading branch information
AlexSciFier authored Jan 31, 2024
2 parents 6df279d + 6a1833f commit 4ad148b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
8 changes: 6 additions & 2 deletions frontend/src/pages/addBookmark/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
};
Expand Down
47 changes: 29 additions & 18 deletions server/logics/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

0 comments on commit 4ad148b

Please sign in to comment.