ismizo commited on
Commit
2b1f3f2
Β·
verified Β·
1 Parent(s): ccc6848

Upload 17 files

Browse files
Files changed (3) hide show
  1. server.js +54 -1
  2. services/pdfService.js +1 -1
  3. services/r2Service.js +17 -0
server.js CHANGED
@@ -13,7 +13,7 @@ const { generateCV, builderChat } = require('./services/aiService');
13
  const { createPdfFromHtml } = require('./services/pdfService');
14
  const { createWordFromHtml } = require('./services/wordService');
15
  const { getClickPesaAuthToken, initiateClickPesaUssdPush } = require('./services/clickpesaService');
16
- const { uploadFile, uploadText, deleteFile, listFiles, getPresignedUrl, userPrefix, USER_QUOTA_BYTES } = require('./services/r2Service');
17
 
18
  const Transaction = require('./models/Transaction');
19
  const User = require('./models/User');
@@ -367,6 +367,59 @@ app.post('/api/download-word', verifyToken, async (req, res) => {
367
  }
368
  });
369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370
  // ─── PAYMENT ──────────────────────────────────────────────────────────────────
371
 
372
  app.post('/api/pay', verifyToken, async (req, res) => {
 
13
  const { createPdfFromHtml } = require('./services/pdfService');
14
  const { createWordFromHtml } = require('./services/wordService');
15
  const { getClickPesaAuthToken, initiateClickPesaUssdPush } = require('./services/clickpesaService');
16
+ const { uploadFile, uploadText, deleteFile, listFiles, getPresignedUrl, getFileContent, userPrefix, USER_QUOTA_BYTES } = require('./services/r2Service');
17
 
18
  const Transaction = require('./models/Transaction');
19
  const User = require('./models/User');
 
367
  }
368
  });
369
 
370
+ // ─── CONVERT SAVED HTML FILE ─────────────────────────────────────────────────
371
+ // Fetches a saved .html file from R2 by key and converts it to PDF or DOCX.
372
+
373
+ app.post('/api/convert-saved', verifyToken, async (req, res) => {
374
+ try {
375
+ const { key, format } = req.body; // format: 'pdf' | 'word'
376
+ if (!key || !format) return res.status(400).json({ error: 'key and format required' });
377
+
378
+ // Security: only allow files owned by this user
379
+ const expectedPrefix = `cvbora/users/${req.user.id}/`;
380
+ if (!key.startsWith(expectedPrefix)) {
381
+ return res.status(403).json({ error: 'Forbidden' });
382
+ }
383
+
384
+ // Only allow HTML files
385
+ if (!key.toLowerCase().endsWith('.html')) {
386
+ return res.status(400).json({ error: 'Only HTML files can be converted' });
387
+ }
388
+
389
+ // Fetch HTML content from R2
390
+ const htmlBuffer = await getFileContent(key);
391
+ const html = htmlBuffer.toString('utf-8');
392
+
393
+ const path = require('path');
394
+ const baseName = path.basename(key, '.html');
395
+
396
+ if (format === 'pdf') {
397
+ const pdfBuffer = await createPdfFromHtml(html);
398
+ res.set({
399
+ 'Content-Type': 'application/pdf',
400
+ 'Content-Length': pdfBuffer.length,
401
+ 'Content-Disposition': `attachment; filename="${baseName}.pdf"`,
402
+ });
403
+ return res.send(pdfBuffer);
404
+ }
405
+
406
+ if (format === 'word') {
407
+ const docxBuffer = await createWordFromHtml(html, baseName);
408
+ res.set({
409
+ 'Content-Type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
410
+ 'Content-Length': docxBuffer.length,
411
+ 'Content-Disposition': `attachment; filename="${baseName}.docx"`,
412
+ });
413
+ return res.send(docxBuffer);
414
+ }
415
+
416
+ res.status(400).json({ error: 'Invalid format. Use pdf or word.' });
417
+ } catch (error) {
418
+ console.error('Convert-saved Error:', error);
419
+ res.status(500).json({ error: 'Conversion failed. Please try again.' });
420
+ }
421
+ });
422
+
423
  // ─── PAYMENT ──────────────────────────────────────────────────────────────────
424
 
425
  app.post('/api/pay', verifyToken, async (req, res) => {
services/pdfService.js CHANGED
@@ -22,7 +22,7 @@ const createPdfFromHtml = async (html) => {
22
  printBackground: true,
23
  // Ensure no fixed height forces truncation
24
  pageRanges: '',
25
- margin: { top: '0px', bottom: '0px', left: '0px', right: '0px' },
26
  });
27
 
28
  await browser.close();
 
22
  printBackground: true,
23
  // Ensure no fixed height forces truncation
24
  pageRanges: '',
25
+ margin: { top: '5px', bottom: '5px', left: '5px', right: '5px' },
26
  });
27
 
28
  await browser.close();
services/r2Service.js CHANGED
@@ -110,12 +110,29 @@ function userPrefix(userId, folder) {
110
  return `cvbora/users/${userId}/${folder}/`;
111
  }
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  module.exports = {
114
  uploadFile,
115
  uploadText,
116
  deleteFile,
117
  listFiles,
118
  getObjectSize,
 
119
  getPresignedUrl,
120
  userPrefix,
121
  USER_QUOTA_BYTES,
 
110
  return `cvbora/users/${userId}/${folder}/`;
111
  }
112
 
113
+ /**
114
+ * Download the full content of an R2 object as a Buffer.
115
+ * @param {string} key
116
+ * @returns {Promise<Buffer>}
117
+ */
118
+ async function getFileContent(key) {
119
+ const command = new GetObjectCommand({ Bucket: BUCKET, Key: key });
120
+ const response = await r2.send(command);
121
+ // response.Body is a ReadableStream (AWS SDK v3)
122
+ const chunks = [];
123
+ for await (const chunk of response.Body) {
124
+ chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);
125
+ }
126
+ return Buffer.concat(chunks);
127
+ }
128
+
129
  module.exports = {
130
  uploadFile,
131
  uploadText,
132
  deleteFile,
133
  listFiles,
134
  getObjectSize,
135
+ getFileContent,
136
  getPresignedUrl,
137
  userPrefix,
138
  USER_QUOTA_BYTES,