Skip to content

Commit 4e79c05

Browse files
authored
Merge pull request #5 from devforth/feature/AdminForth/1247/we-need-api-for-upload-plugin-
feat: add API for to determine our link or from a third-party source
2 parents 89d9cb0 + 82d387f commit 4e79c05

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

index.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@ export default class MarkdownPlugin extends AdminForthPlugin {
1919

2020
// Placeholder for future Upload Plugin API integration.
2121
// For now, treat all extracted URLs as plugin-owned public URLs.
22-
isPluginPublicUrl(_url: string): boolean {
23-
// todo: here we need to check that host name is same as upload plugin, probably create upload plugin endpoint
24-
// should handle cases that user might define custom preview url
25-
// and that local storage has no host name, here, the fact of luck of hostname might be used as
26-
return true;
22+
async isUrlFromPlugin(url: string): Promise<boolean> {
23+
if (!this.uploadPlugin) return false;
24+
try {
25+
const uploadPlugin = this.uploadPlugin as any;
26+
if (typeof uploadPlugin.isInternalUrl === 'function') {
27+
return await uploadPlugin.isInternalUrl(url);
28+
} else {
29+
throw new Error ('Please update upload plugin and storage adapter')
30+
}
31+
} catch (err) {
32+
console.error(`[MarkdownPlugin] Error checking URL ${url}:`, err);
33+
}
34+
return false;
2735
}
2836

2937
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
@@ -168,18 +176,22 @@ export default class MarkdownPlugin extends AdminForthPlugin {
168176

169177
const shouldTrackUrl = (url: string) => {
170178
try {
171-
return this.isPluginPublicUrl(url);
179+
return this.isUrlFromPlugin(url);
172180
} catch (err) {
173181
console.error('Error checking URL ownership', url, err);
174182
return false;
175183
}
176184
};
177185

178-
const getKeyFromTrackedUrl = (rawUrl: string): string | null => {
186+
const getKeyFromTrackedUrl = async (rawUrl: string): Promise<string | null> => {
179187
const srcTrimmed = rawUrl.trim().replace(/^<|>$/g, '');
180188
if (!srcTrimmed || srcTrimmed.startsWith('data:') || srcTrimmed.startsWith('javascript:')) {
181189
return null;
182190
}
191+
const isInternal = await this.isUrlFromPlugin(srcTrimmed);
192+
if (!isInternal) {
193+
return null;
194+
}
183195
if (!shouldTrackUrl(srcTrimmed)) {
184196
return null;
185197
}
@@ -225,7 +237,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
225237
return cleaned || null;
226238
};
227239

228-
function getAttachmentMetas(markdown: string): AttachmentMeta[] {
240+
async function getAttachmentMetas(markdown: string): Promise<AttachmentMeta[]> {
229241
if (!markdown) {
230242
return [];
231243
}
@@ -242,7 +254,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
242254
const srcRaw = match[2];
243255
const titleRaw = normalizeAttachmentTitleForDb((match[3] ?? match[4]) ?? null);
244256

245-
const key = getKeyFromTrackedUrl(srcRaw);
257+
const key = await getKeyFromTrackedUrl(srcRaw);
246258
if (!key) {
247259
continue;
248260
}
@@ -255,7 +267,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
255267
let srcMatch: RegExpExecArray | null;
256268
while ((srcMatch = htmlSrcRegex.exec(markdown)) !== null) {
257269
const srcRaw = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ?? '';
258-
const key = getKeyFromTrackedUrl(srcRaw);
270+
const key = await getKeyFromTrackedUrl(srcRaw);
259271
if (!key) {
260272
continue;
261273
}
@@ -390,7 +402,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
390402

391403
(resourceConfig.hooks.create.afterSave).push(async ({ record, adminUser }: { record: any, adminUser: AdminUser }) => {
392404
// find all s3Paths in the html
393-
const metas = getAttachmentMetas(record[this.options.fieldName]);
405+
const metas = await getAttachmentMetas(record[this.options.fieldName]);
394406
const keys = metas.map(m => m.key);
395407
process.env.HEAVY_DEBUG && console.log('📸 Found attachment keys', keys);
396408
// create attachment records
@@ -416,7 +428,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
416428
]);
417429
const existingKeys = existingAparts.map((a: any) => a[this.options.attachments.attachmentFieldName]);
418430

419-
const metas = getAttachmentMetas(record[this.options.fieldName]);
431+
const metas = await getAttachmentMetas(record[this.options.fieldName]);
420432
const newKeys = metas.map(m => m.key);
421433

422434
process.env.HEAVY_DEBUG && console.log('📸 Existing keys (from db)', existingKeys)

0 commit comments

Comments
 (0)