Skip to content

Commit 5c424c8

Browse files
committed
chore(blob): batch delete
1 parent 4d9c0fb commit 5c424c8

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { eventHandler, readValidatedBody, sendNoContent } from 'h3'
2+
import { z } from 'zod'
3+
import { hubBlob } from '../../../utils/blob'
4+
5+
export default eventHandler(async (event) => {
6+
const { pathnames } = await readValidatedBody(event, z.object({
7+
pathnames: z.array(z.string().min(1)).min(1)
8+
}).parse)
9+
10+
await hubBlob().delete(pathnames)
11+
12+
return sendNoContent(event)
13+
})

src/server/utils/blob.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,12 @@ export function hubBlob() {
126126

127127
return mapR2ObjectToBlob(object)
128128
},
129-
async delete(pathname: string) {
130-
return await bucket.delete(decodeURI(pathname))
129+
async delete(pathnames: string | string[]) {
130+
if (Array.isArray(pathnames)) {
131+
return await bucket.delete(pathnames.map((p) => decodeURI(p)))
132+
} else {
133+
return await bucket.delete(decodeURI(pathnames))
134+
}
131135
}
132136
}
133137
}
@@ -170,10 +174,19 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string) {
170174
})
171175
return JSON.parse(headers.get('x-blob') || '{}') as BlobObject
172176
},
173-
async delete(pathname: string) {
174-
await blobAPI<void>(decodeURI(pathname), {
175-
method: 'DELETE',
176-
})
177+
async delete(pathnames: string | string[]) {
178+
if (Array.isArray(pathnames)) {
179+
await blobAPI<void>('/delete', {
180+
method: 'POST',
181+
body: {
182+
pathnames: pathnames.map((p) => decodeURI(p))
183+
}
184+
})
185+
} else {
186+
await blobAPI<void>(decodeURI(pathnames), {
187+
method: 'DELETE',
188+
})
189+
}
177190
return
178191
}
179192
}

0 commit comments

Comments
 (0)