nodebb-plugin-pdf-secure2 1.3.7 → 1.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/gemini-chat.js +66 -9
- package/package.json +1 -1
- package/static/lib/image.png +0 -0
- package/static/image.png +0 -0
package/lib/gemini-chat.js
CHANGED
|
@@ -140,25 +140,82 @@ async function getOrUploadPdf(filename) {
|
|
|
140
140
|
throw new Error('PDF too large for AI chat');
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
+
// Upload to Gemini File API — file is stored server-side, only URI is sent per request
|
|
144
|
+
console.log('[PDF-Secure] === FILE API UPLOAD START ===');
|
|
145
|
+
console.log('[PDF-Secure] Filename:', filename);
|
|
146
|
+
console.log('[PDF-Secure] FilePath:', filePath);
|
|
147
|
+
console.log('[PDF-Secure] FileSize:', stats.size, 'bytes');
|
|
148
|
+
console.log('[PDF-Secure] ai.files exists:', !!ai.files);
|
|
149
|
+
console.log('[PDF-Secure] ai.files.upload exists:', typeof (ai.files && ai.files.upload));
|
|
150
|
+
|
|
151
|
+
const fileBuffer = await fs.promises.readFile(filePath);
|
|
152
|
+
console.log('[PDF-Secure] File read OK, buffer size:', fileBuffer.length);
|
|
153
|
+
|
|
154
|
+
// Try upload with multiple approaches
|
|
155
|
+
let uploadResult;
|
|
156
|
+
const uploadErrors = [];
|
|
157
|
+
|
|
158
|
+
// Approach 1: file path string
|
|
159
|
+
console.log('[PDF-Secure] Trying approach 1: file path string...');
|
|
143
160
|
try {
|
|
144
|
-
|
|
145
|
-
const uploadResult = await ai.files.upload({
|
|
161
|
+
uploadResult = await ai.files.upload({
|
|
146
162
|
file: filePath,
|
|
147
|
-
config: { mimeType: 'application/pdf' },
|
|
163
|
+
config: { mimeType: 'application/pdf', displayName: filename },
|
|
148
164
|
});
|
|
165
|
+
console.log('[PDF-Secure] Approach 1 SUCCESS! Result:', JSON.stringify(uploadResult).slice(0, 500));
|
|
166
|
+
} catch (err1) {
|
|
167
|
+
console.error('[PDF-Secure] Approach 1 FAILED:', err1.message);
|
|
168
|
+
console.error('[PDF-Secure] Approach 1 error detail:', err1.status || '', err1.code || '', err1.stack?.split('\n').slice(0, 3).join(' | '));
|
|
169
|
+
uploadErrors.push('path: ' + err1.message);
|
|
170
|
+
}
|
|
149
171
|
|
|
172
|
+
// Approach 2: Blob (Node 18+)
|
|
173
|
+
if (!uploadResult) {
|
|
174
|
+
console.log('[PDF-Secure] Trying approach 2: Blob...');
|
|
175
|
+
console.log('[PDF-Secure] Blob available:', typeof Blob !== 'undefined');
|
|
176
|
+
try {
|
|
177
|
+
const blob = new Blob([fileBuffer], { type: 'application/pdf' });
|
|
178
|
+
uploadResult = await ai.files.upload({
|
|
179
|
+
file: blob,
|
|
180
|
+
config: { mimeType: 'application/pdf', displayName: filename },
|
|
181
|
+
});
|
|
182
|
+
console.log('[PDF-Secure] Approach 2 SUCCESS! Result:', JSON.stringify(uploadResult).slice(0, 500));
|
|
183
|
+
} catch (err2) {
|
|
184
|
+
console.error('[PDF-Secure] Approach 2 FAILED:', err2.message);
|
|
185
|
+
uploadErrors.push('blob: ' + err2.message);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Approach 3: Buffer directly
|
|
190
|
+
if (!uploadResult) {
|
|
191
|
+
console.log('[PDF-Secure] Trying approach 3: Buffer...');
|
|
192
|
+
try {
|
|
193
|
+
uploadResult = await ai.files.upload({
|
|
194
|
+
file: fileBuffer,
|
|
195
|
+
config: { mimeType: 'application/pdf', displayName: filename },
|
|
196
|
+
});
|
|
197
|
+
console.log('[PDF-Secure] Approach 3 SUCCESS! Result:', JSON.stringify(uploadResult).slice(0, 500));
|
|
198
|
+
} catch (err3) {
|
|
199
|
+
console.error('[PDF-Secure] Approach 3 FAILED:', err3.message);
|
|
200
|
+
uploadErrors.push('buffer: ' + err3.message);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (uploadResult && uploadResult.uri) {
|
|
150
205
|
const fileUri = uploadResult.uri;
|
|
151
206
|
const mimeType = uploadResult.mimeType || 'application/pdf';
|
|
152
|
-
console.log('[PDF-Secure]
|
|
207
|
+
console.log('[PDF-Secure] === UPLOAD FINAL: SUCCESS ===', fileUri);
|
|
153
208
|
|
|
154
209
|
fileUploadCache.set(filename, { fileUri, mimeType, cachedAt: Date.now() });
|
|
155
210
|
return { type: 'fileData', fileUri, mimeType };
|
|
156
|
-
} catch (uploadErr) {
|
|
157
|
-
// Fallback: use inline base64 (works but uses many tokens)
|
|
158
|
-
console.warn('[PDF-Secure] File API upload failed, falling back to inline:', uploadErr.message);
|
|
159
|
-
const base64 = await getPdfBase64(filename);
|
|
160
|
-
return { type: 'inlineData', mimeType: 'application/pdf', data: base64 };
|
|
161
211
|
}
|
|
212
|
+
|
|
213
|
+
// All upload methods failed
|
|
214
|
+
console.error('[PDF-Secure] === UPLOAD FINAL: ALL FAILED ===');
|
|
215
|
+
console.error('[PDF-Secure] Errors:', uploadErrors.join(' | '));
|
|
216
|
+
console.error('[PDF-Secure] Falling back to inline base64 (will use many tokens!)');
|
|
217
|
+
const base64 = fileBuffer.toString('base64');
|
|
218
|
+
return { type: 'inlineData', mimeType: 'application/pdf', data: base64 };
|
|
162
219
|
}
|
|
163
220
|
|
|
164
221
|
// Read PDF and cache base64 in memory (fallback for File API failure)
|
package/package.json
CHANGED
|
Binary file
|
package/static/image.png
DELETED
|
Binary file
|