-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bf584b
commit 1ba8287
Showing
4 changed files
with
94 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Deploy when pushing on main | ||
|
||
on: | ||
push: | ||
branches: | ||
- 'main' | ||
paths: | ||
- 'edge' | ||
- '.github/workflows/deploy-script.yml' | ||
|
||
|
||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
|
||
name: 'Upload script' | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Deploy Script to Bunny Edge Scripting | ||
uses: BunnyWay/actions/deploy-script@main | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
script_id: ${{ secrets.BUNNY_SCRIPT_ID }} | ||
file: "edge/script.ts" |
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 @@ | ||
edge/* |
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,64 @@ | ||
import process from "node:process" | ||
import * as BunnySDK from "@bunny.net/edgescript-sdk"; | ||
|
||
|
||
/** | ||
* Returns an HTTP response. | ||
* @param {Request} request - The Fetch API Request object. | ||
* @return {Response} The HTTP response or string. | ||
*/ | ||
BunnySDK.net.http.serve(async (request: Request): Promise<Response> => { | ||
if (!process.env.BUNNY_API_KEY || !process.env.BUNNY_STORAGE_ZONE_NAME) { | ||
return new Response('ERR: Bunny API key or storage zone name not set.', { status: 500 }) | ||
} | ||
|
||
if (request.method !== 'POST') { | ||
return new Response('ERR: Method not allowed.', {status: 405}) | ||
} | ||
|
||
if (request.method !== 'POST') { | ||
return new Response('ERR: Method not allowed.', { status: 405 }) | ||
} | ||
|
||
try { | ||
const contentType = request.headers.get('content-type') | ||
|
||
if (contentType && contentType.startsWith('multipart/form-data')) { | ||
const image = (await request.formData()).get('file') as Blob | ||
|
||
if (!image) { | ||
return new Response('ERR: No file provided in the FormData.', { status: 400 }) | ||
} | ||
|
||
// Generate a unique key for the Bunny Edge Files object | ||
const key = `${Date.now()}_${Math.random().toString(36).substring(7)}.${image.type.split('/')[1]}` | ||
|
||
// Define Bunny Edge Files upload URL | ||
const uploadUrl = `https://storage.bunnycdn.com/${process.env.BUNNY_STORAGE_ZONE_NAME}/${key}` | ||
const pullUrl = process.env.BUNNY_STORAGE_ZONE_NAME.replaceAll('-', '.') | ||
|
||
// Use fetch to upload the image to Bunny Edge Files | ||
const upload = await fetch(uploadUrl, { | ||
method: 'PUT', | ||
headers: { | ||
AccessKey: process.env.BUNNY_API_KEY, | ||
'Content-Type': 'application/octet-stream', | ||
}, | ||
body: image, | ||
}) | ||
|
||
if (upload.status !== 201) { | ||
return new Response('ERR: Failed to upload to Bunny Edge Files.', { status: 500 }) | ||
} | ||
|
||
const imageUrl = `https://${pullUrl}/${key}` | ||
console.log(`SUCCESS: ${imageUrl}`) | ||
return new Response(`SUCCESS: ${imageUrl}`, { status: 200 }) | ||
} else { | ||
return new Response('ERR: Content-Type must be multipart/form-data.', { status: 400 }) | ||
} | ||
} catch (error) { | ||
console.error('Server error:', error) | ||
return new Response('ERR: Server error.', { status: 500 }) | ||
} | ||
}); |
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 |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
".next/types/**/*.ts" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
"node_modules", | ||
"edge" | ||
] | ||
} |