Skip to content

Commit

Permalink
Merge branch 'refs/heads/feature/local-img' into feature/v2-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
maliut committed Oct 15, 2024
2 parents 3ae3700 + afe5fa3 commit e9ddebe
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ tailwind.config.js
postcss.config.js
.eslintrc.js
bundle.js
/adapters/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ tsconfig.tsbuildinfo
/adapters/**/lib
/packages/**/lib
/packages/dicecore/src/test/plugins/
/images/
/*_bak/
3 changes: 2 additions & 1 deletion adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
}

async resolveFile(attrs: Dict, download = false) {
if (!download && !await this.bot.ctx.http.isLocal(attrs.src || attrs.url)) {
const isLocal = (url: string) => !url.startsWith('http://') && !url.startsWith('https://')
if (!download && !isLocal(attrs.src || attrs.url)) {
return this.fileUrl = attrs.src || attrs.url
}
const { data, filename, type } = await this.bot.ctx.http.file(this.fileUrl || attrs.src || attrs.url, attrs)
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/adapter/satori.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Context extends SatoriContext {
super(config)
try {
this.provide('http', undefined, true)
this.plugin(HTTP, config.request)
this.plugin(HTTP, { baseURL: 'http://localhost:4174' })
} catch (e) {
console.log(e)
}
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/app/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { createWss } from '@paotuan/syncserver'
import { Wss } from './wss'
import { resolveRootDir } from '../utils'
import { GlobalStore } from '../state'
import { serveStatic } from './static'

export async function setupServer(port: number) {
const httpServer = createServer()
const httpServer = createServer(serveStatic)

const persistenceDir = resolveRootDir('db')
const syncServer = createWss({ persistenceDir })
Expand Down
48 changes: 48 additions & 0 deletions apps/server/src/app/static.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { IncomingMessage, ServerResponse } from 'node:http'
import { extname } from 'node:path'
import fs from 'fs'
import { resolveRootDir } from '../utils'

const IMAGE_SERVE_DIR = resolveRootDir('images')

// https://github.com/adrian-deniz/nodejs-static-file-server/blob/master/index.js
export function serveStatic(request: IncomingMessage, response: ServerResponse) {
console.log('request', request.url) // '/images.png'
const filePath = IMAGE_SERVE_DIR + request.url
const ext = extname(filePath).toLowerCase()
const contentType = mimeTypes[ext] || 'application/octet-stream'

fs.readFile(filePath, function (error, content) {
if (error) {
if (error.code == 'ENOENT') {
response.writeHead(404, { 'Content-Type': 'text/html' })
response.end('404', 'utf-8')
} else {
response.writeHead(500)
response.end(`static server error: ${error.code}`)
}
} else {
response.writeHead(200, { 'Content-Type': contentType })
response.end(content, 'utf-8')
}
})
}

const mimeTypes: Record<string, string> = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm',
'.ico' : 'image/x-icon'
}

0 comments on commit e9ddebe

Please sign in to comment.