-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Transform s3 utility to an Unstorage driver
- Loading branch information
1 parent
ed470c1
commit bcb437e
Showing
8 changed files
with
151 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { defineEventHandler } from "#imports"; | ||
import { deleteObject } from "#s3"; | ||
import { s3Storage, getKey, normalizeKey } from "#s3"; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const key = event.path.split("/s3/mutation/")[1]; | ||
const key = getKey(event); | ||
|
||
await deleteObject(key); | ||
const normalizedKey = normalizeKey(key); | ||
|
||
await s3Storage.removeItem(normalizedKey); | ||
|
||
return "ok"; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { defineEventHandler } from "#imports"; | ||
import { getObject } from "#s3"; | ||
import { s3Storage, getKey, normalizeKey } from "#s3"; | ||
import type { H3Event } from "h3"; | ||
|
||
export default defineEventHandler(async (event: H3Event) => { | ||
const key = event.path.split("/s3/query/")[1]; | ||
export default defineEventHandler((event: H3Event) => { | ||
const key = getKey(event); | ||
|
||
return getObject(event, key); | ||
const normalizedKey = normalizeKey(key); | ||
|
||
return s3Storage.getItemRaw(normalizedKey, { event }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./s3"; | ||
export * from "./key"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { parseURL } from "ufo"; | ||
import type { H3Event } from "h3"; | ||
|
||
function normalizeKey(key: string) { | ||
return key.replace(/\//g, ":"); | ||
} | ||
|
||
function denormalizeKey(key: string) { | ||
return key.replace(/:/g, "/"); | ||
} | ||
|
||
function getKey(event: H3Event) { | ||
const regex = new RegExp("^/api/s3/(mutation|query)/"); | ||
|
||
const pathname = parseURL(event.path).pathname; | ||
|
||
return pathname.replace(regex, ""); | ||
} | ||
|
||
export { normalizeKey, denormalizeKey, getKey }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters