with thumbnail!
This commit is contained in:
@@ -60,6 +60,7 @@ interface ExtractedRecipe {
|
||||
unit: string;
|
||||
}> | null;
|
||||
steps: string[] | null;
|
||||
image?: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -279,7 +280,7 @@ function buildTandoorRecipeDTO(recipe: ExtractedRecipe): TandoorRecipeDTO {
|
||||
*/
|
||||
export async function uploadRecipeWithIngredientsDTO(
|
||||
recipe: ExtractedRecipe
|
||||
): Promise<{ success: boolean; recipeId?: number; error?: string }> {
|
||||
): Promise<{ success: boolean; recipeId?: number; imageUrl?: string; error?: string }> {
|
||||
try {
|
||||
// Validate token
|
||||
const token = tandoorConfig.token;
|
||||
@@ -317,7 +318,8 @@ export async function uploadRecipeWithIngredientsDTO(
|
||||
|
||||
return {
|
||||
success: true,
|
||||
recipeId: createdRecipe.id
|
||||
recipeId: createdRecipe.id,
|
||||
imageUrl: recipe.image || undefined
|
||||
};
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
|
||||
@@ -328,3 +330,51 @@ export async function uploadRecipeWithIngredientsDTO(
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads an image to a Tandoor recipe
|
||||
*/
|
||||
export async function uploadRecipeImage(
|
||||
recipeId: number,
|
||||
imageUrl: string
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
try {
|
||||
const token = tandoorConfig.token;
|
||||
if (!token) {
|
||||
return { success: false, error: 'TANDOOR_TOKEN not set' };
|
||||
}
|
||||
|
||||
console.log('Uploading image for recipe ID:', recipeId, 'URL:', imageUrl.substring(0, 50));
|
||||
|
||||
// Convert base64 data URL to Blob for multipart upload
|
||||
const response = await fetch(imageUrl);
|
||||
const imageBlob = await response.blob();
|
||||
|
||||
// Use image field with multipart form data (Tandoor's binary upload support)
|
||||
const formData = new FormData();
|
||||
formData.append('image', imageBlob, 'recipe-image.jpg');
|
||||
|
||||
// Upload to Tandoor
|
||||
const uploadResponse = await fetch(
|
||||
`${tandoorConfig.serverUrl}/api/recipe/${recipeId}/image/`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: { 'Authorization': `Bearer ${token}` },
|
||||
body: formData
|
||||
}
|
||||
);
|
||||
|
||||
if (!uploadResponse.ok) {
|
||||
console.warn(`Image upload returned ${uploadResponse.status}`);
|
||||
return { success: false, error: `Upload failed: ${uploadResponse.statusText}` };
|
||||
}
|
||||
|
||||
console.log('Image uploaded successfully');
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
|
||||
console.warn(`Image upload failed: ${errorMsg}`);
|
||||
// Don't fail recipe creation if image fails
|
||||
return { success: false, error: errorMsg };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user