Skip to content

Commit a45e972

Browse files
committed
feat: cleanup toolset resource corn job
1 parent 01b4dee commit a45e972

File tree

10 files changed

+78
-23
lines changed

10 files changed

+78
-23
lines changed

components/toolset/resource/Item.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const handleSave = async () => {
140140
note: formData.note,
141141
content: base.value.type === 'user' ? formData.content : ''
142142
}
143-
const valid = useSchemaValidator(updateToolsetResourceSchema, body)
143+
const valid = useKunSchemaValidator(updateToolsetResourceSchema, body)
144144
if (!valid) {
145145
return
146146
}

components/toolset/resource/LinkForm.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ watch(
3838
)
3939
4040
const submitLink = async () => {
41-
const result = useSchemaValidator(createToolsetResourceSchema, formData)
41+
const result = useKunSchemaValidator(createToolsetResourceSchema, formData)
4242
if (!result) {
4343
return
4444
}

components/toolset/resource/Upload.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const uploadSmall = async (f: File) => {
9191
filename: f.name,
9292
filesize: f.size
9393
}
94-
const result = useSchemaValidator(initToolsetUploadSchema, initUploadData)
94+
const result = useKunSchemaValidator(initToolsetUploadSchema, initUploadData)
9595
if (!result) {
9696
return
9797
}
@@ -119,7 +119,7 @@ const uploadSmall = async (f: File) => {
119119
const completeUploadData = {
120120
salt: initRes.salt
121121
}
122-
const result = useSchemaValidator(
122+
const result = useKunSchemaValidator(
123123
completeToolsetUploadSchema,
124124
completeUploadData
125125
)
@@ -151,7 +151,7 @@ const uploadLarge = async (f: File) => {
151151
filename: f.name,
152152
filesize: f.size
153153
}
154-
const result = useSchemaValidator(initToolsetUploadSchema, initUploadData)
154+
const result = useKunSchemaValidator(initToolsetUploadSchema, initUploadData)
155155
if (!result) {
156156
return
157157
}
@@ -204,7 +204,7 @@ const uploadLarge = async (f: File) => {
204204
uploadId: initRes.uploadId,
205205
parts
206206
}
207-
const result = useSchemaValidator(
207+
const result = useKunSchemaValidator(
208208
completeToolsetUploadSchema,
209209
completeUploadData
210210
)
@@ -231,7 +231,7 @@ const uploadLarge = async (f: File) => {
231231
salt: initRes.salt,
232232
uploadId: initRes.uploadId
233233
}
234-
const result = useSchemaValidator(
234+
const result = useKunSchemaValidator(
235235
abortToolsetUploadSchema,
236236
abortUploadData
237237
)

composables/useKunSchemaValidator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { ZodSchema } from 'zod'
22

3-
export const useSchemaValidator = <T>(schema: ZodSchema<T>, data: unknown) => {
3+
export const useKunSchemaValidator = <T>(
4+
schema: ZodSchema<T>,
5+
data: unknown
6+
) => {
47
const result = schema.safeParse(data)
58
if (!result.success) {
69
const message = JSON.parse(result.error.message)[0]

lib/s3/client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ export const s3 = new S3Client({
99
},
1010
requestChecksumCalculation: 'WHEN_REQUIRED'
1111
})
12+
13+
export const s3ClientR2 = new S3Client({
14+
endpoint: process.env.KUN_VISUAL_NOVEL_IMAGE_BED_ENDPOINT!,
15+
region: 'auto',
16+
credentials: {
17+
accessKeyId: process.env.KUN_VISUAL_NOVEL_IMAGE_BED_ACCESS_KEY!,
18+
secretAccessKey: process.env.KUN_VISUAL_NOVEL_IMAGE_BED_SECRET_KEY!
19+
}
20+
})

lib/s3/uploadImageToS3.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'
2-
3-
export const s3 = new S3Client({
4-
endpoint: process.env.KUN_VISUAL_NOVEL_IMAGE_BED_ENDPOINT!,
5-
region: 'auto',
6-
credentials: {
7-
accessKeyId: process.env.KUN_VISUAL_NOVEL_IMAGE_BED_ACCESS_KEY!,
8-
secretAccessKey: process.env.KUN_VISUAL_NOVEL_IMAGE_BED_SECRET_KEY!
9-
}
10-
})
1+
import { PutObjectCommand } from '@aws-sdk/client-s3'
2+
import { s3ClientR2 } from './client'
113

124
export const uploadImageToS3 = async (key: string, fileBuffer: Buffer) => {
135
const uploadCommand = new PutObjectCommand({
@@ -16,5 +8,5 @@ export const uploadImageToS3 = async (key: string, fileBuffer: Buffer) => {
168
Body: fileBuffer,
179
ContentType: 'application/octet-stream'
1810
})
19-
await s3.send(uploadCommand)
11+
await s3ClientR2.send(uploadCommand)
2012
}

nuxt.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ export default defineNuxtConfig({
173173
tasks: true
174174
},
175175
scheduledTasks: {
176-
'0 0 * * *': ['reset-daily']
176+
'0 0 * * *': ['reset-daily'],
177+
'0 * * * *': ['cleanup-toolset-resource']
178+
// '* * * * *': ['cleanup-toolset-resource']
177179
}
178180
}
179181
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kun-galgame-nuxt3",
3-
"version": "4.4.3",
3+
"version": "4.4.4",
44
"packageManager": "[email protected]",
55
"private": true,
66
"scripts": {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { DeleteObjectCommand } from '@aws-sdk/client-s3'
2+
import { s3 } from '~/lib/s3/client'
3+
import {
4+
removeUploadCache,
5+
type UploadSaltCache
6+
} from '~/server/utils/upload/saveUploadSalt'
7+
8+
export default defineTask({
9+
meta: {
10+
name: 'cleanup-toolset-resource',
11+
description:
12+
'Every hour, delete S3 objects referenced by toolset:resource keys in Redis.'
13+
},
14+
15+
async run() {
16+
const storage = useStorage('redis')
17+
const keys = await storage.getKeys('toolset:resource')
18+
19+
let deletedCount = 0
20+
21+
for (const key of keys) {
22+
const store = await storage.getItem(key)
23+
const parsedResult = JSON.parse(JSON.stringify(store)) as UploadSaltCache
24+
25+
try {
26+
if (parsedResult && parsedResult.key) {
27+
await s3.send(
28+
new DeleteObjectCommand({
29+
Bucket: process.env.KUN_VISUAL_NOVEL_S3_STORAGE_BUCKET_NAME!,
30+
Key: parsedResult.key
31+
})
32+
)
33+
await removeUploadCache(parsedResult.salt)
34+
35+
deletedCount++
36+
}
37+
} catch (err) {
38+
console.error(`Failed to delete object for key ${key}:`)
39+
}
40+
}
41+
42+
return { result: `Deleted ${deletedCount} toolset resources from S3.` }
43+
}
44+
})

server/tasks/reset-daily.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ export default defineTask({
1111
const { count } = await prisma.user.updateMany({
1212
data: {
1313
daily_check_in: 0,
14-
daily_image_count: 0
14+
daily_image_count: 0,
15+
daily_toolset_upload_count: 0
1516
},
1617
where: {
17-
OR: [{ daily_check_in: { not: 0 } }, { daily_image_count: { not: 0 } }]
18+
OR: [
19+
{ daily_check_in: { not: 0 } },
20+
{ daily_image_count: { not: 0 } },
21+
{ daily_toolset_upload_count: { not: 0 } }
22+
]
1823
}
1924
})
2025

0 commit comments

Comments
 (0)