diff --git a/public/links.html b/public/links.html
new file mode 100644
index 0000000..e528a10
--- /dev/null
+++ b/public/links.html
@@ -0,0 +1,162 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/handlers/link.ts b/src/handlers/link.ts
index 1863ed8..08680e0 100644
--- a/src/handlers/link.ts
+++ b/src/handlers/link.ts
@@ -17,10 +17,10 @@ export async function handleCreateLink(c: HonoContext): Promise {
await c.env.DB.prepare('INSERT INTO links (key, destination, created_at, expire_at) VALUES (?, ?, ?, ?)')
.bind(key, destination, created_at, expire_at)
.run()
- return c.json({
+ return c.json({
key: key,
access_url: accessURL
- }, { status: 201 })
+ }, { status: 201 })
} catch (e) {
return c.json({ error: 'Internal Server Error' }, { status: 500 })
}
@@ -28,9 +28,9 @@ export async function handleCreateLink(c: HonoContext): Promise {
export async function deleteLink(db: D1Database, key: string): Promise {
await db.prepare('DELETE FROM links WHERE key = ?')
- .bind(key)
- .run()
-
+ .bind(key)
+ .run()
+
// await db.prepare('DELETE FROM link_access WHERE key = ?')
// .bind(key)
// .run()
@@ -65,7 +65,7 @@ export async function handleAccessLink(c: HonoContext): Promise {
await c.env.DB.prepare('INSERT INTO link_access (key, ip, country, city, ua, referer, access_at) VALUES (?, ?, ?, ?, ?, ?, ?)')
.bind(key, ip, country, city, ua, referer, access_at)
.run()
-
+
await c.env.DB.prepare('UPDATE links SET access_count = access_count + 1 WHERE key = ?')
.bind(key)
.run()
@@ -169,7 +169,8 @@ export async function handleDeleteLink(c: HonoContext): Promise {
* - `order` (optional): A string to specify the sort order. Can be 'ASC' or 'DESC'. Defaults to 'DESC'.
*/
export async function handleListLinks(c: HonoContext): Promise {
- const dest = c.req.query('dest') || null
+ const before_raw = parseInt(c.req.query('before') || '')
+ const before = (!isNaN(before_raw) && before_raw > 0) ? before_raw : new Date().getTime()
const page_raw = parseInt(c.req.query('page') || '')
const page = (!isNaN(page_raw) && page_raw > 0) ? page_raw : 1
@@ -177,29 +178,54 @@ export async function handleListLinks(c: HonoContext): Promise {
const num_raw = parseInt(c.req.query('num') || '')
const num = (!isNaN(num_raw) && num_raw > 0 && num_raw < 500) ? num_raw : 50
+ const key = c.req.query('key') || null
+
+ const dest = c.req.query('destination') || null
+
const order_by_raw = c.req.query('order_by') || 'created_at'
- const order_by = ['created_at', 'access_count'].includes(order_by_raw) ? order_by_raw : 'created_at'
+ const order_by = ['created_at', 'expire_at', 'access_count'].includes(order_by_raw) ? order_by_raw : 'created_at'
const order_raw = c.req.query('order')?.toUpperCase() || 'DESC'
const order = ['ASC', 'DESC'].includes(order_raw) ? order_raw : 'DESC'
-
- let query = 'SELECT * FROM links'
+ let condition_query = ''
let params = []
+ condition_query += ' WHERE created_at < ?'
+ params.push(before)
+ if (key) {
+ condition_query += ' AND key LIKE ?'
+ params.push(`%${key}%`)
+ }
if (dest) {
- query += ' WHERE destination LIKE ?'
+ condition_query += ' AND destination LIKE ?'
params.push(`%${dest}%`)
}
- query += ` ORDER BY ${order_by} ${order}`
- query += ` LIMIT ? OFFSET ?`
+
+ const query_count = `SELECT COUNT(*) as count FROM links ${condition_query}`
+ const count = await c.env.DB.prepare(query_count)
+ .bind(...params)
+ .first()
+ if (!count) {
+ return c.json({ error: 'Internal Server Error' }, { status: 500 })
+ }
+ const total = count.count as number
+
+ const query = `SELECT * FROM links ${condition_query} ORDER BY ${order_by} ${order} LIMIT ? OFFSET ?`
params.push(num)
params.push((page - 1) * num)
-
const links = await c.env.DB.prepare(query)
.bind(...params)
.all()
+ const total_pages = Math.ceil(total / num)
+ const remaining = total > (page * num) ? total - (page * num) : 0
+ const remaining_pages = Math.ceil(remaining / num)
+
return c.json({
+ total: total,
+ total_pages: total_pages,
+ remaining: remaining,
+ remaining_pages: remaining_pages,
success: links.success,
data: links.results
}, { status: 200 })