2024-06-04 13:06:25 +02:00
|
|
|
import { contentType } from '@std/media-types'
|
|
|
|
import { extname } from '@std/path'
|
|
|
|
import { getBaseUrl, getSourceUrl } from './src/url_utils.ts'
|
|
|
|
import { toUser, toUserDomain } from './src/user_utils.ts'
|
|
|
|
|
|
|
|
Deno.serve({ port: Number(Deno.env.get('PORT') ?? 8000) }, async (req) => {
|
|
|
|
const reqUrl = new URL(req.url)
|
|
|
|
|
|
|
|
// Get and check user-domain and user-login
|
|
|
|
const rawUserDomain = reqUrl.hostname.split('.')[0]
|
|
|
|
const userDomain = toUserDomain(rawUserDomain)
|
|
|
|
const user = toUser(userDomain)
|
|
|
|
|
|
|
|
console.log(`Getting portfolio of > @${user}`)
|
|
|
|
|
|
|
|
// Get relative source path
|
|
|
|
const filePath = `.${reqUrl.pathname}`
|
|
|
|
|
|
|
|
// Get source base url ("/dist" if available, "/" else)
|
|
|
|
const baseUrl = await getBaseUrl(user)
|
|
|
|
const fileUrl = getSourceUrl(filePath, baseUrl)
|
|
|
|
|
|
|
|
// Get source and mime type
|
|
|
|
const response = await fetch(fileUrl)
|
|
|
|
const ext = extname(fileUrl.pathname)
|
|
|
|
const mimeType = contentType(ext) ?? 'text/plain; charset=utf-8' // Assume plain text as default.
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
// Update content-type if not bin file
|
2024-06-04 17:47:38 +02:00
|
|
|
const headers = new Headers(response.headers)
|
|
|
|
if (headers.get('Content-Type') !== 'application/octet-stream') {
|
|
|
|
headers.set('Content-Type', mimeType)
|
2024-06-04 13:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stream original file to user
|
2024-06-04 17:47:38 +02:00
|
|
|
return new Response(response.body, { headers })
|
2024-06-04 13:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return response
|
|
|
|
})
|