Something went wrong. Try again.
A blockchain-powered certificate verification platform for @inovuslabs — enabling instant, tamper-proof validation of credentials through s… certificate.inovuslabs.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
import dotenv from "dotenv";dotenv.config();
const fs = require('fs');const path = require('path');const axios = require('axios');const XLSX = require('xlsx');
import { generatePreSignedUrl } from '../../src/controllers/Media';import { SerialDateToISOString } from '../../src/utils';
// Path to your Excel file and log fileconst excelPath = path.join(__dirname, 'certificates.xlsx');const logPath = path.join(__dirname, 'seeder.log');
// Read Excel fileconst workbook = XLSX.readFile(excelPath);const sheetName = workbook.SheetNames[0];const data = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
// Validation functionfunction isValid(row : any): boolean { return ( row.email && row.mobile && row.event_id && row.certificate_id && row.recipient_name && row.issue_date );}
// API endpointconst API_URL = 'http://localhost:8000/certificate/add';
export const uploadFileToPresignedUrl = async ( presignedUrl: string, filePath: string, fileType: string): Promise<boolean> => { try { const fileData = fs.readFileSync(filePath); const response = await axios.put(presignedUrl, fileData, { headers: { 'Content-Type': fileType, } }); return true; } catch (error) { console.log(`Error uploading file to presigned URL: ${error}`); return false; }};
async function seedCertificates() { console.log(`Starting seeding process...\n`);
for (const row of data) { if (!isValid(row)) { const msg = `Invalid row skipped: ${JSON.stringify(row)}`; console.log(msg); continue; }
console.log(`Processing row no. ${data.indexOf(row) + 1} ...`);
try { const imagePath = row.url; const metadata = { email: row.email, mobile: String(row.mobile), event_id: row.event_id, certificate_id: row.certificate_id, recipient_name: row.recipient_name, issue_date: SerialDateToISOString(row.issue_date), url: '' };
let filePath, fileName, fileType, fileUrl;
if (imagePath) { filePath = path.join(__dirname, 'assets', imagePath);
if (fs.existsSync(filePath)) { const ext = path.extname(imagePath).toLowerCase(); fileName = `${row.certificate_id}${ext}`; fileType = ext === '.png' ? 'image/png' : ext === '.jpg' || ext === '.jpeg' ? 'image/jpeg' : ext === '.pdf' ? 'application/pdf' : 'application/octet-stream';
// 1. Get presigned URL for upload const presignedUrl = await generatePreSignedUrl(fileName, fileType); console.log(`Generated presigned URL for file ${fileName} ...`);
// 2. Upload file to presigned URL await uploadFileToPresignedUrl(presignedUrl as string, filePath, fileType); console.log(`File uploaded successfully ...`);
// 3. Set file URL in metadata (assuming public URL follows a pattern) fileUrl = `${process.env.CLOUDFLARE_R2_BUCKET_URL}/${fileName}`; metadata.url = fileUrl; } else { filePath = null; } }
// 4. Send metadata to /add endpoint const res = await axios.post(API_URL, metadata, { headers: { 'Content-Type': 'application/json' }, }); if (res.status === 200) { console.log(`Row processed successfully\n`); console.log(res.data); } else { console.log(`Failed to process row: ${JSON.stringify(row)}`); } } catch (err : any) { console.log(err) process.exit(1); } finally { console.log(`\n------------------------------------------------------------------------\n`); } }
console.log(`Seeding process completed.`);}
seedCertificates();