-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: import pdfs & images, improve clip from URL, direct to discord,…
… new languages enabled (#1515)
- Loading branch information
Showing
39 changed files
with
1,282 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ export const sendWelcome = async (to: string[], ccTo: string[]) => { | |
<br /><br /> | ||
Please feel free to contact me if you have questions, concerns or comments at <a href="mailto:[email protected]?subject=RecipeSage%20Support">[email protected]</a>. | ||
Please feel free to contact me if you have questions, concerns or comments via <a href="https://discord.gg/yCfzBft">Discord</a>. | ||
<br /> | ||
|
@@ -30,7 +30,7 @@ export const sendWelcome = async (to: string[], ccTo: string[]) => { | |
You can access the RecipeSage user guide for more information about using the application: https://docs.recipesage.com | ||
Please feel free to contact me if you have questions, concerns or comments at [email protected]. | ||
Please feel free to contact me if you have questions, concerns or comments via Discord https://discord.gg/yCfzBft. | ||
${signaturePlain} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
import { BadRequestError } from "../../../errors"; | ||
import { | ||
AuthenticationEnforcement, | ||
defineHandler, | ||
} from "../../../defineHandler"; | ||
import * as multer from "multer"; | ||
import * as fs from "fs/promises"; | ||
import * as extract from "extract-zip"; | ||
import * as path from "path"; | ||
import { indexRecipes } from "@recipesage/util/server/search"; | ||
import { JobStatus, JobType } from "@prisma/client"; | ||
import { | ||
importStandardizedRecipes, | ||
StandardizedRecipeImportEntry, | ||
} from "@recipesage/util/server/db"; | ||
import { ocrImagesToRecipe } from "@recipesage/util/server/ml"; | ||
import { JobMeta, prisma } from "@recipesage/prisma"; | ||
import * as Sentry from "@sentry/node"; | ||
import { | ||
deletePathsSilent, | ||
getImportJobResultCode, | ||
} from "@recipesage/util/server/general"; | ||
import { cleanLabelTitle, JOB_RESULT_CODES } from "@recipesage/util/shared"; | ||
import { z } from "zod"; | ||
|
||
const schema = { | ||
query: z.object({ | ||
labels: z.string().optional(), | ||
}), | ||
}; | ||
|
||
export const imagesHandler = defineHandler( | ||
{ | ||
schema, | ||
authentication: AuthenticationEnforcement.Required, | ||
beforeHandlers: [ | ||
multer({ | ||
dest: "/tmp/import/", | ||
}).single("file"), | ||
], | ||
}, | ||
async (req, res) => { | ||
const userLabels = | ||
req.query.labels?.split(",").map((label) => cleanLabelTitle(label)) || []; | ||
|
||
const cleanupPaths: string[] = []; | ||
|
||
const file = req.file; | ||
if (!file) { | ||
throw new BadRequestError( | ||
"Request must include multipart file under the 'file' field", | ||
); | ||
} | ||
|
||
const job = await prisma.job.create({ | ||
data: { | ||
userId: res.locals.session.userId, | ||
type: JobType.IMPORT, | ||
status: JobStatus.RUN, | ||
progress: 1, | ||
meta: { | ||
importType: "images", | ||
importLabels: userLabels, | ||
} satisfies JobMeta, | ||
}, | ||
}); | ||
|
||
// We complete this work outside of the scope of the request | ||
const start = async () => { | ||
const zipPath = file.path; | ||
cleanupPaths.push(zipPath); | ||
const extractPath = zipPath + "-extract"; | ||
cleanupPaths.push(extractPath); | ||
|
||
await extract(zipPath, { dir: extractPath }); | ||
|
||
const fileNames = await fs.readdir(extractPath); | ||
|
||
const standardizedRecipeImportInput: StandardizedRecipeImportEntry[] = []; | ||
for (const fileName of fileNames) { | ||
const filePath = path.join(extractPath, fileName); | ||
|
||
if ( | ||
!filePath.endsWith(".jpg") && | ||
!filePath.endsWith(".jpeg") && | ||
!filePath.endsWith(".png") | ||
) { | ||
continue; | ||
} | ||
|
||
const recipeImageBuffer = await fs.readFile(filePath); | ||
const recipeImageBase64 = await fs.readFile(filePath, "base64"); | ||
const images = []; | ||
images.push(recipeImageBase64); | ||
|
||
const recipe = await ocrImagesToRecipe([recipeImageBuffer]); | ||
if (!recipe) { | ||
continue; | ||
} | ||
|
||
standardizedRecipeImportInput.push({ | ||
...recipe, | ||
images, | ||
labels: userLabels, | ||
}); | ||
} | ||
|
||
if (standardizedRecipeImportInput.length === 0) { | ||
throw new Error("No recipes"); | ||
} | ||
|
||
await prisma.job.update({ | ||
where: { | ||
id: job.id, | ||
}, | ||
data: { | ||
progress: 50, | ||
}, | ||
}); | ||
|
||
const createdRecipeIds = await importStandardizedRecipes( | ||
res.locals.session.userId, | ||
standardizedRecipeImportInput, | ||
); | ||
|
||
const recipesToIndex = await prisma.recipe.findMany({ | ||
where: { | ||
id: { | ||
in: createdRecipeIds, | ||
}, | ||
userId: res.locals.session.userId, | ||
}, | ||
}); | ||
|
||
await prisma.job.update({ | ||
where: { | ||
id: job.id, | ||
}, | ||
data: { | ||
progress: 75, | ||
}, | ||
}); | ||
|
||
await indexRecipes(recipesToIndex); | ||
|
||
await prisma.job.update({ | ||
where: { | ||
id: job.id, | ||
}, | ||
data: { | ||
status: JobStatus.SUCCESS, | ||
resultCode: JOB_RESULT_CODES.success, | ||
progress: 100, | ||
}, | ||
}); | ||
}; | ||
|
||
start() | ||
.catch(async (e) => { | ||
const isBadZipError = | ||
e instanceof Error && | ||
e.message === "end of central directory record signature not found"; | ||
|
||
const isNoRecipesError = | ||
e instanceof Error && e.message === "No recipes"; | ||
|
||
await prisma.job.update({ | ||
where: { | ||
id: job.id, | ||
}, | ||
data: { | ||
status: JobStatus.FAIL, | ||
resultCode: getImportJobResultCode({ | ||
isBadFormat: isBadZipError, | ||
isNoRecipes: isNoRecipesError, | ||
}), | ||
}, | ||
}); | ||
|
||
if (!isBadZipError && !isNoRecipesError) { | ||
Sentry.captureException(e, { | ||
extra: { | ||
jobId: job.id, | ||
}, | ||
}); | ||
console.error(e); | ||
} | ||
}) | ||
.finally(async () => { | ||
await deletePathsSilent(cleanupPaths); | ||
}); | ||
|
||
return { | ||
statusCode: 201, | ||
data: { | ||
jobId: job.id, | ||
}, | ||
}; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.