Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw). Local database, uploaded photos, build output and dependencies are gitignored. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
+129
@@ -0,0 +1,129 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import { kv } from './db.mjs';
|
||||
import {
|
||||
SMTP_APP_PASSWORD,
|
||||
SMTP_FROM,
|
||||
SMTP_HOST,
|
||||
SMTP_PORT,
|
||||
SMTP_SECURE,
|
||||
SMTP_USER,
|
||||
} from './config.mjs';
|
||||
|
||||
const SETTINGS_KEY = 'mailSettings';
|
||||
|
||||
function savedSettings() {
|
||||
const value = kv.get(SETTINGS_KEY);
|
||||
return value && typeof value === 'object' ? value : {};
|
||||
}
|
||||
|
||||
/** Never returns the app password; `hasPassword` is enough for the admin UI. */
|
||||
export function mailStatus() {
|
||||
const saved = savedSettings();
|
||||
const user = String(saved.user || SMTP_USER || '').trim();
|
||||
const appPassword = String(saved.appPassword || SMTP_APP_PASSWORD || '');
|
||||
const from = String(saved.from || SMTP_FROM || user).trim();
|
||||
const host = String(saved.host || SMTP_HOST || 'smtp.gmail.com').trim();
|
||||
const port = Number(saved.port || SMTP_PORT || 465);
|
||||
const secure = typeof saved.secure === 'boolean' ? saved.secure : SMTP_SECURE;
|
||||
return {
|
||||
configured: Boolean(user && appPassword),
|
||||
source: saved.user || saved.appPassword ? 'admin' : user || appPassword ? 'environment' : 'none',
|
||||
host,
|
||||
port,
|
||||
secure,
|
||||
user,
|
||||
from,
|
||||
hasPassword: Boolean(appPassword),
|
||||
gmail: host === 'smtp.gmail.com',
|
||||
};
|
||||
}
|
||||
|
||||
function validateSettings(input) {
|
||||
const current = savedSettings();
|
||||
const user = String(input.user ?? current.user ?? SMTP_USER ?? '').trim();
|
||||
const appPassword = String(
|
||||
(input.appPassword && String(input.appPassword).trim()) ||
|
||||
current.appPassword ||
|
||||
SMTP_APP_PASSWORD ||
|
||||
'',
|
||||
).trim();
|
||||
const from = String(input.from ?? current.from ?? SMTP_FROM ?? user).trim();
|
||||
const host = String(input.host ?? current.host ?? SMTP_HOST ?? 'smtp.gmail.com').trim();
|
||||
const port = Number(input.port ?? current.port ?? SMTP_PORT ?? 465);
|
||||
const secure = typeof input.secure === 'boolean' ? input.secure : port === 465;
|
||||
|
||||
if (!/^[^@\s]+@[^@\s]+\.[^@\s]{2,}$/.test(user)) {
|
||||
throw new Error('Enter the Gmail address that will send notifications');
|
||||
}
|
||||
if (!appPassword) throw new Error('Enter the Google app password for that Gmail account');
|
||||
if (appPassword.length < 8) throw new Error('That app password looks too short');
|
||||
if (from && !/^[^@\s]+@[^@\s]+\.[^@\s]{2,}$/.test(from)) {
|
||||
throw new Error('The From address is not a valid email address');
|
||||
}
|
||||
if (!host) throw new Error('Enter an SMTP host');
|
||||
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
||||
throw new Error('Enter a valid SMTP port');
|
||||
}
|
||||
|
||||
return { user, appPassword, from: from || user, host, port, secure };
|
||||
}
|
||||
|
||||
export function saveMailSettings(input) {
|
||||
const next = validateSettings(input ?? {});
|
||||
kv.set(SETTINGS_KEY, next);
|
||||
return mailStatus();
|
||||
}
|
||||
|
||||
export async function sendAppMail({ to, subject, text, html }) {
|
||||
const settings = mailStatus();
|
||||
if (!settings.configured) {
|
||||
throw new Error(
|
||||
'Email is not configured. Add the Gmail address and Google app password in Administration → Quotas.',
|
||||
);
|
||||
}
|
||||
if (!to) throw new Error('A recipient email address is required');
|
||||
|
||||
const saved = savedSettings();
|
||||
const appPassword = saved.appPassword || SMTP_APP_PASSWORD;
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: settings.host,
|
||||
port: settings.port,
|
||||
secure: settings.secure,
|
||||
auth: { user: settings.user, pass: appPassword },
|
||||
connectionTimeout: 12_000,
|
||||
greetingTimeout: 12_000,
|
||||
socketTimeout: 20_000,
|
||||
});
|
||||
|
||||
try {
|
||||
return await transporter.sendMail({
|
||||
from: settings.from || settings.user,
|
||||
replyTo: settings.from || settings.user,
|
||||
to,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
});
|
||||
} finally {
|
||||
transporter.close();
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendTestMail(to) {
|
||||
return sendAppMail({
|
||||
to,
|
||||
subject: 'Luminary email notifications are working',
|
||||
text:
|
||||
'This is a test message from Luminary. Your Gmail app password is configured correctly, ' +
|
||||
'so quota warnings and scheduled storage-prune notices can reach your team.\n\n' +
|
||||
'— Luminary',
|
||||
html: `
|
||||
<div style="font-family:Arial,Helvetica,sans-serif;line-height:1.55;color:#222">
|
||||
<h2 style="margin:0 0 12px">Luminary email is working</h2>
|
||||
<p>Your Gmail app password is configured correctly. Quota warnings and scheduled
|
||||
storage-prune notices can now reach your team.</p>
|
||||
<p style="color:#777;font-size:12px">— Luminary</p>
|
||||
</div>
|
||||
`,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user