Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/import-duplicates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('server: every duplicate answer names the recipe it matched', () => {

test('server: a Paprika archive never counts a skipped duplicate as imported', () => {
assert.doesNotMatch(routes, /const row = _saveImportedRecipe\(u, r,[^\n]*\n\s*created\.push\(row\);/);
assert.match(routes, /if \(row\) created\.push\(row\);\s*else skipped\.push/);
assert.match(routes, /if \(row\) \{ created\.push\(row\); createdIds\.add\(row\.id\); \}\s*else skipped\.push/);
});

test('local mode answers duplicates the same way as the server', () => {
Expand Down
131 changes: 131 additions & 0 deletions scripts/paprika-import.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Paprika exports embed each photo inside the .paprikarecipe JSON:
* `photo_data` (or `photos[0].data` on some versions) holds the base64
* bytes, `photo` is only the embedded filename, and `photo_url` /
* `image_url` point at the source site. The importer built its data URL
* out of the *filename*, so the magic-byte check downstream rejected
* every picture and Paprika imports arrived imageless.
*
* The same archive can also hold several user-made copies of a recipe
* (same name or same source URL, distinct entries in the app). Dedup
* must compare against the library the user had *before* the import,
* not against rows written earlier in the same batch — otherwise three
* copies import as one and the rest are reported as its "duplicates".
*
* Fixtures are trimmed from a real Paprika 3 export (2026-10).
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { gzipSync } from 'node:zlib';
import JSZip from 'jszip';
import { loadRecipeZip, scanLoadedZip, importPaprikaArchive } from '../server/lib/recipe-importers.js';

const read = (p) => readFileSync(new URL(`../${p}`, import.meta.url), 'utf8');

// JPEG header bytes only — enough to be a plausible photo. The importer
// treats photo_data as opaque base64; the magic-byte validation happens
// later, when the data URL is localized at save time.
const PHOTO_B64 = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00]).toString('base64');

const paprikaRecipe = (over = {}) => ({
name: 'Lentil Soup',
ingredients: '1 cup lentils\n2 cups water',
directions: 'Boil.\n\nSimmer.',
photo: 'lentil-soup.jpg', // a filename, not bytes
photo_data: PHOTO_B64,
...over,
});

async function paprikaArchive(entries) {
const z = new JSZip();
for (const [name, recipe] of entries) {
z.file(name, gzipSync(Buffer.from(JSON.stringify(recipe))));
}
return z.generateAsync({ type: 'nodebuffer' });
}

test('photo_data becomes the image, not the photo filename', async () => {
const out = await scanLoadedZip(await loadRecipeZip(await paprikaArchive([['a.paprikarecipe', paprikaRecipe()]])));
assert.equal(out.length, 1);
assert.equal(out[0].source, 'paprika');
assert.equal(out[0].recipe.imgUrl, `data:image/jpeg;base64,${PHOTO_B64}`);
});

test('the photos[] array is used when photo_data is absent', async () => {
const withPhotos = paprikaRecipe({ photo_data: null, photos: [{ filename: 'x.jpg', hash: '', data: PHOTO_B64 }] });
const out = await scanLoadedZip(await loadRecipeZip(await paprikaArchive([['a.paprikarecipe', withPhotos]])));
assert.equal(out[0].recipe.imgUrl, `data:image/jpeg;base64,${PHOTO_B64}`);
});

test('a recipe without embedded bytes falls back to the source-site URL', async () => {
const remote = paprikaRecipe({ photo_data: null, photos: [], image_url: 'https://example.com/soup.jpg' });
const out = await scanLoadedZip(await loadRecipeZip(await paprikaArchive([['a.paprikarecipe', remote]])));
assert.equal(out[0].recipe.imgUrl, 'https://example.com/soup.jpg');
});

test('a recipe with no picture imports with a null image', async () => {
const bare = paprikaRecipe({ photo_data: null, photos: [], image_url: null });
const out = await scanLoadedZip(await loadRecipeZip(await paprikaArchive([['a.paprikarecipe', bare]])));
assert.equal(out[0].recipe.imgUrl, null);
});

test('importPaprikaArchive (the /import path) reads the same fields', async () => {
const recipes = await importPaprikaArchive(await paprikaArchive([
['a.paprikarecipe', paprikaRecipe()],
['b.paprikarecipe', paprikaRecipe({ name: 'Other Soup' })],
]));
assert.equal(recipes.length, 2);
assert.ok(recipes.every(r => r.imgUrl === `data:image/jpeg;base64,${PHOTO_B64}`));
});

test('a Paprika rating survives import (0 means unrated)', async () => {
const out = await scanLoadedZip(await loadRecipeZip(await paprikaArchive([
['a.paprikarecipe', paprikaRecipe({ rating: 4 })],
['b.paprikarecipe', paprikaRecipe({ name: 'Unrated', rating: 0 })],
])));
const byName = Object.fromEntries(out.map(o => [o.recipe.name, o.recipe]));
assert.equal(byName['Lentil Soup'].rating, 4);
assert.equal(byName['Unrated'].rating, null);
});

test("Paprika's nutritional_info rides along in notes instead of being dropped", async () => {
const out = await scanLoadedZip(await loadRecipeZip(await paprikaArchive([
['a.paprikarecipe', paprikaRecipe({ notes: 'Family favourite.', nutritional_info: '340 kcal per serving' })],
['b.paprikarecipe', paprikaRecipe({ name: 'Bare', notes: null, nutritional_info: null })],
])));
const byName = Object.fromEntries(out.map(o => [o.recipe.name, o.recipe]));
assert.equal(byName['Lentil Soup'].notes, 'Family favourite.\n\nNutrition (from Paprika):\n340 kcal per serving');
assert.equal(byName['Bare'].notes, null);
});

test('the client-side importer copy builds the same data URL', () => {
const client = read('src/lib/recipe-importers-client.js');
assert.match(client, /function _paprikaImgUrl\(r\)/);
assert.doesNotMatch(client, /data:image\/jpeg;base64,\$\{r\.photo\}/, 'the filename is never used as image bytes');
});

test('server: batch imports dedup against the library, not against the batch', () => {
const routes = read('server/routes/recipes.js');
assert.match(routes, /function _findImportDuplicate\(u, parsed, excludeIds\)/);
assert.match(routes, /id NOT IN \(\$\{excl\.map/);
assert.match(routes, /_saveImportedRecipe\(u, recipe, \{ addToPantry, applyTags, importCategories, dedup, creatorUsername, excludeIds: createdIds \}\)/);
assert.match(routes, /_saveImportedRecipe\(u, r, \{ addToPantry, applyTags, importCategories, creatorUsername, excludeIds: createdIds \}\)/);
assert.equal((routes.match(/createdIds\.add\(row\.id\)/g) || []).length, 2, 'both server batch paths record what they wrote');
});

test('local mode: batch imports dedup against the library, not against the batch', () => {
const native = read('src/lib/api-native.js');
assert.match(native, /function _findImportDuplicateLocal\(r, excludeIds\)/);
assert.match(native, /id NOT IN \(\$\{excl\.map/);
assert.match(native, /_saveImportedRecipeLocal\(this, r, \{ addToPantry, applyTags, excludeIds: createdIds \}\)/);
assert.match(native, /_saveImportedRecipeLocal\(this, entry\.recipe, \{ addToPantry, applyTags, dedup, excludeIds: createdIds \}\)/);
assert.equal((native.match(/createdIds\.add\(saved\.id\)/g) || []).length, 2, 'both local batch paths record what they wrote');
});

test('local mode: an embedded data: photo becomes a file before it reaches the table', () => {
const native = read('src/lib/api-native.js');
assert.match(native, /async function _localizeImportedImage\(api, url\)/);
assert.match(native, /api\.uploadImage\(new File\(\[bytes\]/);
assert.match(native, /await _importedPayloadWithImage\(api, r\)/);
});
26 changes: 24 additions & 2 deletions server/lib/recipe-importers.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,18 +816,20 @@ function _importPaprika(r) {
return {
name: r.name || 'Imported recipe',
description: r.description || null,
imgUrl: r.photo_url || (r.photo ? `data:image/jpeg;base64,${r.photo}` : null),
imgUrl: _paprikaImgUrl(r),
servings: _parseServings(r.servings),
yield_text: r.servings || null,
prep_minutes: _parseMinutes(r.prep_time),
cook_minutes: _parseMinutes(r.cook_time),
// Paprika's 0-5 stars; 0 means unrated, which is null here.
rating: r.rating > 0 ? r.rating : null,
ingredients: items.length ? [{ name: '', items }] : [],
steps,
tags: paprikaCats.slice(1, 13),
tools: [],
nutrition: {},
source_url: r.source_url || r.source || null,
notes: r.notes || null,
notes: _paprikaNotes(r),
category_name: categoryName,
// Paprika stores the recipe creation timestamp in `created` (or
// older `dateAdded` for legacy exports). Pull this so the imported
Expand Down Expand Up @@ -858,6 +860,26 @@ export function parseImportedCreatedAt(raw) {
}

// ── Helpers ─────────────────────────────────────────────────────────────────
// Paprika's photo fields: `photo` is only the embedded filename, the
// bytes live in `photo_data` (base64 JPEG) or `photos[0].data`;
// `photo_url` / `image_url` are remote URLs on the source site. Prefer
// the embedded bytes — _saveImportedRecipe localizes data URLs into
// real files under /uploads/ — and fall back to a remote URL only when
// nothing was embedded.
function _paprikaImgUrl(r) {
const b64 = r.photo_data || r.photos?.[0]?.data;
if (b64) return `data:image/jpeg;base64,${b64}`;
return r.photo_url || r.image_url || null;
}
// Paprika's `nutritional_info` is a free-text blob with no structured
// equivalent here; append it to notes so it survives the import
// instead of being dropped.
function _paprikaNotes(r) {
const nutrition = String(r.nutritional_info || '').trim();
if (!nutrition) return r.notes || null;
const label = `Nutrition (from Paprika):\n${nutrition}`;
return r.notes ? `${r.notes}\n\n${label}` : label;
}
function _parseServings(s) {
if (s == null) return null;
const m = String(s).match(/(\d+)/);
Expand Down
40 changes: 29 additions & 11 deletions server/routes/recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1678,17 +1678,23 @@ function _aiConfigForUser(u, req) {
// then source_url. Returns { id } or null. _saveImportedRecipe skips on
// a match (dedup 'skip'), and the routes report the match's id so the
// client can open it instead of a recipe that was never written.
function _findImportDuplicate(u, parsed) {
// `excludeIds` (a Set of ids written earlier in the same batch) are
// ignored, so an archive that itself contains several near-copies —
// e.g. Paprika user-made copies sharing a name or source URL — doesn't
// swallow its own entries.
function _findImportDuplicate(u, parsed, excludeIds) {
const excl = excludeIds ? [...excludeIds] : [];
const notIn = excl.length ? ` AND id NOT IN (${excl.map(() => '?').join(',')})` : '';
const lower = String(parsed?.name || '').toLowerCase().trim();
let existing = lower ? db.prepare(
`SELECT id FROM recipes
WHERE ${_whereUser(u)} AND deleted_at IS NULL AND lower(name) = ? LIMIT 1`
).get(...[..._userArgs(u), lower]) : null;
WHERE ${_whereUser(u)} AND deleted_at IS NULL AND lower(name) = ?${notIn} LIMIT 1`
).get(...[..._userArgs(u), lower, ...excl]) : null;
if (!existing && parsed?.source_url) {
existing = db.prepare(
`SELECT id FROM recipes
WHERE ${_whereUser(u)} AND deleted_at IS NULL AND source_url = ? LIMIT 1`
).get(...[..._userArgs(u), parsed.source_url]);
WHERE ${_whereUser(u)} AND deleted_at IS NULL AND source_url = ?${notIn} LIMIT 1`
).get(...[..._userArgs(u), parsed.source_url, ...excl]);
}
return existing || null;
}
Expand All @@ -1710,9 +1716,12 @@ function _saveImportedRecipe(u, parsed, opts = {}) {
// shape; 'force' creates a duplicate (pre-dedup behaviour). Match
// is by case-insensitive name first, then by source_url.
dedup = 'skip',
// Recipe ids written earlier in the same import batch — near-copies
// inside one archive are not duplicates of each other.
excludeIds = null,
} = opts;
if (dedup !== 'force' && parsed?.name) {
const existing = _findImportDuplicate(u, parsed);
const existing = _findImportDuplicate(u, parsed, excludeIds);
if (existing) {
if (dedup === 'skip') return null;
// dedup === 'replace' falls through and gets re-inserted; the
Expand Down Expand Up @@ -2072,6 +2081,12 @@ router.post('/import-zip/commit', _zipImportUpload.single('file'), wrap(async (r
const failed = [];
const skippedDup = [];
let timelineImported = 0;
// Rows written earlier in this same batch. Archives legitimately hold
// several near-copies (e.g. Paprika keeps user-made copies with the
// same name or source URL as distinct recipes), so dedup must compare
// against what the user had *before* the import, not against entries
// from this batch.
const createdIds = new Set();

for (let i = 0; i < scanned.length; i++) {
if (wantedIdx && !wantedIdx.has(i)) continue;
Expand All @@ -2090,16 +2105,17 @@ router.post('/import-zip/commit', _zipImportUpload.single('file'), wrap(async (r
} else if (!importImages) {
recipe.imgUrl = null;
}
const row = _saveImportedRecipe(u, recipe, { addToPantry, applyTags, importCategories, dedup, creatorUsername });
const row = _saveImportedRecipe(u, recipe, { addToPantry, applyTags, importCategories, dedup, creatorUsername, excludeIds: createdIds });
// Resolve a target recipe_id even when the row was dedup-skipped
// so timeline events still land on the existing recipe.
let targetRecipeId = row?.id || null;
if (!row) {
const existing = _findImportDuplicate(u, recipe);
const existing = _findImportDuplicate(u, recipe, createdIds);
targetRecipeId = existing?.id || null;
skippedDup.push({ idx: i, name: recipe?.name || `(item ${i})`, existing_id: targetRecipeId });
} else {
created.push(row);
createdIds.add(row.id);
}

// Mealie timeline events ride along with each recipe entry from
Expand Down Expand Up @@ -2221,12 +2237,14 @@ router.post('/import', _importUpload.single('file'), wrap(async (req, res) => {
catch (e) { return res.status(400).json({ error: e.message }); }
const created = [];
const skipped = [];
// Same-batch exemption — see the createdIds comment in /import-zip/commit.
const createdIds = new Set();
for (const r of recipes) {
try {
const row = _saveImportedRecipe(u, r, { addToPantry, applyTags, importCategories, creatorUsername });
const row = _saveImportedRecipe(u, r, { addToPantry, applyTags, importCategories, creatorUsername, excludeIds: createdIds });
// null = a duplicate that was skipped, not a recipe written.
if (row) created.push(row);
else skipped.push({ name: r?.name || '', existing_id: _findImportDuplicate(u, r)?.id || null });
if (row) { created.push(row); createdIds.add(row.id); }
else skipped.push({ name: r?.name || '', existing_id: _findImportDuplicate(u, r, createdIds)?.id || null });
} catch (e) {
// Continue on per-recipe failure — most archives have many.
}
Expand Down
Loading