Skip to content

Commit

Permalink
feat: deploy bunny edge script
Browse files Browse the repository at this point in the history
  • Loading branch information
omBratteng committed Nov 22, 2024
1 parent 6bf584b commit 1ba8287
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .github/workflows/deploy-script.yml
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"
1 change: 1 addition & 0 deletions .vercelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edge/*
64 changes: 64 additions & 0 deletions edge/script.ts
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 })
}
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
"node_modules",
"edge"
]
}

0 comments on commit 1ba8287

Please sign in to comment.