feat(import): enhance image URL handling and error reporting during recipe import
This commit is contained in:
@@ -20,6 +20,7 @@ export interface QuickImportResult {
|
||||
markdown: string;
|
||||
source: 'ica' | 'pdf' | 'image' | 'other';
|
||||
imageUrl?: string;
|
||||
imageWarning?: string;
|
||||
}
|
||||
|
||||
type UploadKind = 'pdf' | 'image';
|
||||
@@ -317,12 +318,24 @@ export class QuickImportService {
|
||||
|
||||
// Ladda ner och optimera bild om parser hittade en
|
||||
let imageUrl: string | undefined;
|
||||
let imageWarning: string | undefined;
|
||||
if (recipe.imageUrl) {
|
||||
try {
|
||||
imageUrl = await downloadAndOptimizeImage(recipe.imageUrl, IMAGE_DEST_DIR);
|
||||
this.logger.log(`Bild optimerad och sparad: ${imageUrl}`);
|
||||
} catch (imgErr) {
|
||||
this.logger.warn(`Kunde inte ladda ner bild: ${imgErr}`);
|
||||
const normalizedImageUrl = this.normalizeImageUrl(recipe.imageUrl, url);
|
||||
if (!normalizedImageUrl) {
|
||||
imageWarning = 'Receptbild kunde inte tolkas till en giltig URL.';
|
||||
this.logger.warn(
|
||||
`Kunde inte normalisera bild-URL: "${recipe.imageUrl}" (källsida: ${url})`,
|
||||
);
|
||||
} else {
|
||||
try {
|
||||
imageUrl = await downloadAndOptimizeImage(normalizedImageUrl, IMAGE_DEST_DIR);
|
||||
this.logger.log(`Bild optimerad och sparad: ${imageUrl}`);
|
||||
} catch (imgErr) {
|
||||
imageWarning = 'Receptbild kunde inte laddas ner.';
|
||||
this.logger.warn(
|
||||
`Kunde inte ladda ner bild: ${imgErr} (källa: ${normalizedImageUrl})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,6 +343,7 @@ export class QuickImportService {
|
||||
markdown,
|
||||
source,
|
||||
imageUrl,
|
||||
imageWarning,
|
||||
};
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Okänt fel vid scraping';
|
||||
@@ -340,6 +354,20 @@ export class QuickImportService {
|
||||
}
|
||||
}
|
||||
|
||||
private normalizeImageUrl(rawImageUrl: string, pageUrl: string): string | null {
|
||||
const trimmed = rawImageUrl.trim();
|
||||
if (!trimmed) return null;
|
||||
|
||||
const protocolNormalized =
|
||||
trimmed.startsWith('//') ? `https:${trimmed}` : trimmed;
|
||||
|
||||
try {
|
||||
return new URL(protocolNormalized, pageUrl).toString();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Konvertera receptobjekt till Markdown-format
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user