46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { config } from 'dotenv'
|
||
config()
|
||
|
||
import fs from 'fs'
|
||
import path from 'path'
|
||
|
||
async function uploadToOpeninary(filePath: string, folder: string) {
|
||
const fileBuffer = fs.readFileSync(filePath)
|
||
const ext = path.extname(filePath).toLowerCase()
|
||
let type = 'image/jpeg'
|
||
if (ext === '.png') type = 'image/png'
|
||
if (ext === '.webp') type = 'image/webp'
|
||
|
||
const blob = new Blob([fileBuffer], { type })
|
||
const formData = new FormData()
|
||
|
||
formData.append('files', blob, path.basename(filePath))
|
||
formData.append('folder', folder)
|
||
|
||
const res = await fetch(`${process.env.OPENINARY_API_URL}/api/upload`, {
|
||
method: 'POST',
|
||
headers: { Authorization: `Bearer ${process.env.OPENINARY_API_KEY}` },
|
||
body: formData,
|
||
})
|
||
|
||
if (!res.ok) {
|
||
const errorText = await res.text()
|
||
throw new Error('Upload başarısız: ' + errorText)
|
||
}
|
||
|
||
const data = await res.json()
|
||
return data.files[0]
|
||
}
|
||
|
||
async function main() {
|
||
const file = '/Users/ayrisdev/.gemini/antigravity-ide/brain/4d6ff40f-af54-4d09-b92f-d0078b260d91/media__1784311319733.jpg'
|
||
const result = await uploadToOpeninary(file, 'starapart/hero')
|
||
let finalUrl = result.url
|
||
if (finalUrl && finalUrl.startsWith('/')) {
|
||
finalUrl = `${process.env.NEXT_PUBLIC_OPENINARY_URL || 'https://media.ayris.tech'}${finalUrl}`
|
||
}
|
||
console.log('UPLOAD_URL=' + finalUrl)
|
||
}
|
||
|
||
main().catch(console.error)
|