39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import mongoose from 'mongoose';
|
|
import { env } from '../src/config/env.js';
|
|
import { OnboardedPatient } from '../src/models/OnboardedPatient.js';
|
|
import { ONBOARDING_UPLOAD_DIR } from '../src/services/onboardingUpload.js';
|
|
import { putScreenshot, s3Enabled } from '../src/services/s3.js';
|
|
|
|
if (!s3Enabled()) {
|
|
console.error('Set S3_BUCKET, AWS_REGION, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY first.');
|
|
process.exit(1);
|
|
}
|
|
|
|
await mongoose.connect(env.MONGODB_URI);
|
|
let uploaded = 0;
|
|
const items = await OnboardedPatient.find({ 'paymentScreenshot.filename': { $nin: [null, ''] } });
|
|
for (const item of items) {
|
|
const filename = item.paymentScreenshot.filename;
|
|
if (String(filename).startsWith('onboarding/')) continue;
|
|
const local = path.join(ONBOARDING_UPLOAD_DIR, path.basename(filename));
|
|
try {
|
|
const buffer = await fs.readFile(local);
|
|
const ext = path.extname(filename).replace('.', '') || 'jpg';
|
|
const key = await putScreenshot({
|
|
buffer,
|
|
mimeType: item.paymentScreenshot.mimeType || 'image/jpeg',
|
|
ext,
|
|
});
|
|
item.paymentScreenshot.filename = key;
|
|
await item.save();
|
|
uploaded += 1;
|
|
} catch (err) {
|
|
console.warn(`Skip ${filename}: ${err.message}`);
|
|
}
|
|
}
|
|
|
|
console.log(`Uploaded ${uploaded} local screenshot(s) to S3. Local files were left in place.`);
|
|
await mongoose.disconnect();
|