Skip to content

Commit

Permalink
Make expiry optional, and add years to expiry options
Browse files Browse the repository at this point in the history
  • Loading branch information
rutvora committed Aug 9, 2024
1 parent efc882e commit 940d205
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h2>Settings</h2>
<div id='paste-expiration-panel' class='paste-setting-subitem-panel'>
<input list='expiration-choices' type='text' min='60' step='1' name='paste-expiration'
id='paste-expiration-input' placeholder='Expiration (secs)' value='7d'>
<label class='small-label' for='paste-expiration-input'>Delete your paste after a period of time. <br>Units: s (seconds), m (minutes), h (hours), d (days), M (months)</label>
<label class='small-label' for='paste-expiration-input'>Delete your paste after a period of time (leave blank for no expiry). <br>Units: s (seconds), m (minutes), h (hours), d (days), M (months), Y (years) <br> Max 68 years</label>
</div>
<div id='paste-passwd-panel' class='paste-setting-subitem-panel'>
<input type='text' spellcheck='false' name='paste-expiration' id='paste-passwd-input' placeholder='Password'>
Expand Down
8 changes: 6 additions & 2 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export function parsePath(pathname) {
}

export function parseExpiration(expirationStr) {
const EXPIRE_REGEX = /^[\d\.]+\s*[smhdwM]?$/
if (!expirationStr) {
return NaN
}
const EXPIRE_REGEX = /^[\d\.]+\s*[smhdwMY]?$/
if (!EXPIRE_REGEX.test(expirationStr)) {
throw new WorkerError(400, `‘${expirationStr}’ is not a valid expiration specification`)
}
Expand All @@ -86,7 +89,8 @@ export function parseExpiration(expirationStr) {
else if (lastChar === 'h') expirationSeconds *= 3600
else if (lastChar === 'd') expirationSeconds *= 3600 * 24
else if (lastChar === 'w') expirationSeconds *= 3600 * 24 * 7
else if (lastChar === 'M') expirationSeconds *= 3600 * 24 * 7 * 30
else if (lastChar === 'M') expirationSeconds *= 3600 * 24 * 30
else if (lastChar === 'Y') expirationSeconds *= 3600 * 24 * 365
return expirationSeconds
}

Expand Down
17 changes: 10 additions & 7 deletions src/handlers/handleWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ async function createPaste(env, content, isPrivate, expire, short, createDate, p
}
}

await env.PB.put(short, content, {
expirationTtl: expire,
let options = {
metadata: {
postedAt: createDate,
passwd: passwd,
filename: filename,
lastModified: now,
},
})
}

if (!isNan(expire)) {
options.expirationTtl = expire
}

await env.PB.put(short, content, options)

let accessUrl = env.BASE_URL + "/" + short
const adminUrl = env.BASE_URL + "/" + short + params.SEP + passwd
return {
Expand Down Expand Up @@ -101,10 +107,7 @@ export async function handlePostOrPut(request, env, ctx, isPut) {
let expirationSeconds = undefined
if (expire !== undefined) {
expirationSeconds = parseExpiration(expire)
if (isNaN(expirationSeconds)) {
throw new WorkerError(400, `cannot parse expire ${expirationSeconds} as an number`)
}
if (expirationSeconds < 60) {
if (!isNan(expirationSeconds) && expirationSeconds < 60) {
throw new WorkerError(
400,
`due to limitation of Cloudflare, expire should be a integer greater than 60, '${expirationSeconds}' given`,
Expand Down

0 comments on commit 940d205

Please sign in to comment.