-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add working basic avatar upload, add tabs
- Loading branch information
Showing
8 changed files
with
1,778 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { S3Client } from '@aws-sdk/client-s3' | ||
import { Upload } from '@aws-sdk/lib-storage' | ||
|
||
const endpoint = 'https://aa20b480cc813f2131bc005e2b7fd140.r2.cloudflarestorage.com/onechatimages' | ||
const bucket = 'onechatimages' | ||
const folder = 'uploads' | ||
|
||
const s3Client = new S3Client({ | ||
credentials: { | ||
accessKeyId: process.env.CLOUDFLARE_R2_ACCESS_KEY!, | ||
secretAccessKey: process.env.CLOUDFLARE_R2_SECRET_KEY!, | ||
}, | ||
endpoint, | ||
region: 'auto', | ||
forcePathStyle: true, // Important for bucket compatibility with R2 | ||
}) | ||
|
||
export default async function handler(req: Request) { | ||
if (req.method !== 'POST') { | ||
return Response.json({ error: 'Method not allowed' }, { status: 405 }) | ||
} | ||
|
||
const formData = await req.formData() | ||
const file = formData.get('file') | ||
if (!file || !(file instanceof File)) { | ||
return Response.json({ error: 'Failed to parse the form' }, { status: 500 }) | ||
} | ||
|
||
const fileStream = file.stream() | ||
|
||
const uploadParams = { | ||
Bucket: bucket, | ||
Key: `${folder}/${file.name}`, | ||
Body: fileStream, | ||
ContentType: file.type, | ||
} | ||
|
||
try { | ||
const uploader = new Upload({ | ||
client: s3Client, | ||
params: uploadParams, | ||
}) | ||
|
||
await uploader.done() | ||
|
||
const key = uploadParams.Key | ||
const url = `https://one1.dev/${bucket}/${key}` | ||
|
||
return new Response(JSON.stringify({ message: 'File uploaded successfully', key, url }), { | ||
status: 200, | ||
}) | ||
} catch (error) { | ||
return new Response(JSON.stringify({ error: 'Failed to upload file', details: `${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
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
Oops, something went wrong.