unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import { Media } from "../models/index.js";
2import { completeEnvironment } from "./backendOptions.js";
3import { logger } from "./logger.js";
4
5const regex = /url\((.*?)\)/gm
6
7export default async function processExternalCustomCss(userId: string, unprocessed: string): Promise<string> {
8 let m;
9 let processedCSS = unprocessed.replaceAll('[external-css-override]', '')
10 while ((m = regex.exec(unprocessed)) !== null) {
11 if (m.index === regex.lastIndex) {
12 regex.lastIndex++;
13 }
14
15 if (m[1]) {
16 const linkMatch = m[1].replace(/['"]/gm, '')
17 try {
18 const extMedia = await Media.findOrCreate({
19 where: {
20 url: linkMatch,
21 },
22 defaults: {
23 NSFW: false,
24 userId: userId,
25 description: "",
26 ipUpload: "MEDIA_FROM_CUSTOM_CSS_FROM_ANOTHER_INSTANCE",
27 external: true
28 }
29 })
30 processedCSS = processedCSS.replaceAll(linkMatch,
31 new URL('/api/v2/cache/media/' + extMedia[0].id, completeEnvironment.externalCacheurl).href
32 )
33 } catch {
34 logger.error({ link: linkMatch }, "cannot media this")
35 }
36 }
37 }
38 return processedCSS
39}