feat: add authentication
This commit is contained in:
@@ -13,6 +13,8 @@ export function safeFileName(inputString: string): string {
|
||||
// Remove characters that are not safe for file names
|
||||
fileName = fileName.replace(/[^\w.-]/g, "");
|
||||
|
||||
fileName = fileName.replaceAll(":", "");
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
@@ -34,7 +36,9 @@ export function extractHashTags(inputString: string) {
|
||||
export const isYoutubeLink = (link: string) => {
|
||||
try {
|
||||
const url = new URL(link);
|
||||
return ["youtu.be", "youtube.com","www.youtube.com" ].includes(url.hostname);
|
||||
return ["youtu.be", "youtube.com", "www.youtube.com"].includes(
|
||||
url.hostname,
|
||||
);
|
||||
} catch (_err) {
|
||||
return false;
|
||||
}
|
||||
@@ -52,3 +56,39 @@ export function extractYoutubeId(link: string) {
|
||||
|
||||
return url.pathname.replace(/^\//, "");
|
||||
}
|
||||
|
||||
export async function hash(message: string) {
|
||||
const data = new TextEncoder().encode(message);
|
||||
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
||||
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(
|
||||
"",
|
||||
);
|
||||
return hashHex;
|
||||
}
|
||||
// Helper function to calculate SHA-256 hash
|
||||
export async function sha256(input: string) {
|
||||
const encoder = new TextEncoder();
|
||||
const data = encoder.encode(input);
|
||||
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
||||
return base64urlencode(new Uint8Array(hashBuffer));
|
||||
}
|
||||
|
||||
// Helper function to encode a byte array as a URL-safe base64 string
|
||||
function base64urlencode(data: Uint8Array) {
|
||||
const base64 = btoa(String.fromCharCode(...data));
|
||||
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
||||
}
|
||||
export function getCookie(name: string): string | null {
|
||||
if (typeof document === "undefined") return null;
|
||||
const nameLenPlus = name.length + 1;
|
||||
return document.cookie
|
||||
.split(";")
|
||||
.map((c) => c.trim())
|
||||
.filter((cookie) => {
|
||||
return cookie.substring(0, nameLenPlus) === `${name}=`;
|
||||
})
|
||||
.map((cookie) => {
|
||||
return decodeURIComponent(cookie.substring(nameLenPlus));
|
||||
})[0] || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user