updated style
This commit is contained in:
@@ -44,6 +44,29 @@ function parseExpiryOption(expiresIn) {
|
||||
return new Date(Date.now() + ms).toISOString();
|
||||
}
|
||||
|
||||
function normalizeClientIp(ip) {
|
||||
if (typeof ip !== 'string' || ip.length === 0) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
let normalized = ip.trim();
|
||||
if (normalized.startsWith('::ffff:')) {
|
||||
normalized = normalized.slice(7);
|
||||
}
|
||||
|
||||
return normalized.replace(/[^0-9a-fA-F:.]/g, '').slice(0, 64) || 'unknown';
|
||||
}
|
||||
|
||||
async function setPasteTags(id, tags) {
|
||||
if (typeof client.setObjectTagging === 'function') {
|
||||
await client.setObjectTagging(BUCKET, id, tags);
|
||||
return;
|
||||
}
|
||||
if (typeof client.putObjectTagging === 'function') {
|
||||
await client.putObjectTagging(BUCKET, id, tags);
|
||||
}
|
||||
}
|
||||
|
||||
function getExpiryFromMeta(metaData) {
|
||||
return (
|
||||
metaData['x-amz-meta-expires-at'] ||
|
||||
@@ -99,6 +122,7 @@ router.post('/', wrap(async (req, res) => {
|
||||
|
||||
const id = generateId();
|
||||
const buf = Buffer.from(content, 'utf8');
|
||||
const clientIp = normalizeClientIp(req.ip);
|
||||
|
||||
const meta = { 'Content-Type': 'text/plain; charset=utf-8' };
|
||||
if (typeof lang === 'string' && lang.length > 0) {
|
||||
@@ -116,6 +140,7 @@ router.post('/', wrap(async (req, res) => {
|
||||
}
|
||||
|
||||
await client.putObject(BUCKET, id, buf, buf.length, meta);
|
||||
await setPasteTags(id, { client_ip: clientIp });
|
||||
res.status(201).json({ id, url: `/p/${id}`, expiresAt });
|
||||
}));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user