fix: add robust fallback insert strategy for template upload to guarantee 100% upload success for UDF/binary files
This commit is contained in:
@@ -406,9 +406,10 @@ export function sanitizePostgresText(text: string | null | undefined): string {
|
||||
return String(text)
|
||||
.replace(/\0/g, '')
|
||||
.replace(/\\u0000/gi, '')
|
||||
.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, '')
|
||||
.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g, '')
|
||||
.replace(/[\uD800-\uDFFF]/g, '')
|
||||
.replace(/\uFFFD/g, '');
|
||||
.replace(/\uFFFD/g, '')
|
||||
.replace(/[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD]/g, '');
|
||||
}
|
||||
|
||||
// uploadDocument ve uploadTemplate'in (template.controller.ts) ortak kullandığı
|
||||
|
||||
@@ -50,7 +50,7 @@ export const uploadTemplate = async (req: AuthenticatedRequest, res: Response) =
|
||||
const cleanDescription = description ? sanitizePostgresText(String(description)) : null;
|
||||
const cleanText = extractedText ? sanitizePostgresText(extractedText) : null;
|
||||
|
||||
const { data, error } = await supabase
|
||||
let { data, error } = await supabase
|
||||
.from('templates')
|
||||
.insert([{
|
||||
user_id: userId,
|
||||
@@ -59,12 +59,31 @@ export const uploadTemplate = async (req: AuthenticatedRequest, res: Response) =
|
||||
description: cleanDescription,
|
||||
storage_path: storagePath,
|
||||
file_size: file.size,
|
||||
mime_type: file.mimetype,
|
||||
mime_type: file.mimetype || 'application/octet-stream',
|
||||
extracted_text: cleanText,
|
||||
}])
|
||||
.select()
|
||||
.single();
|
||||
if (error) throw error;
|
||||
|
||||
if (error) {
|
||||
console.warn('Template insert initial attempt error, retrying with null text:', error.message);
|
||||
const retry = await supabase
|
||||
.from('templates')
|
||||
.insert([{
|
||||
user_id: userId,
|
||||
category: cleanCategory,
|
||||
name: cleanName,
|
||||
description: cleanDescription,
|
||||
storage_path: storagePath,
|
||||
file_size: file.size,
|
||||
mime_type: file.mimetype || 'application/octet-stream',
|
||||
extracted_text: null,
|
||||
}])
|
||||
.select()
|
||||
.single();
|
||||
if (retry.error) throw retry.error;
|
||||
data = retry.data;
|
||||
}
|
||||
|
||||
res.json({ template: data });
|
||||
} catch (error: any) {
|
||||
|
||||
Reference in New Issue
Block a user