Skip to content

Commit

Permalink
feat(qq): support send private url assets
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 14, 2023
1 parent ddda5f1 commit aac353d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
16 changes: 12 additions & 4 deletions adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { QQBot } from './bot'
import FormData from 'form-data'
import { escape } from '@satorijs/element'
import { QQGuildBot } from './bot/guild'
import { Entry } from '@satorijs/server-temp'

export const escapeMarkdown = (val: string) =>
val
Expand Down Expand Up @@ -98,7 +99,7 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE

async resolveFile(attrs: Dict, download = false) {
if (attrs) this.resource = attrs
if (this.resource.url.startsWith('http') && !download) {
if (!download && !await this.bot.ctx.http.isPrivate(this.resource.url)) {
return this.fileUrl = this.resource.url
}
const { data, filename } = await this.bot.ctx.http.file(this.resource.url, this.resource)
Expand Down Expand Up @@ -200,8 +201,14 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
}

async sendFile(type: string, attrs: Dict) {
if (!attrs.url.startsWith('http')) {
return this.bot.logger.warn('unsupported file url')
let url = attrs.url, entry: Entry | undefined
if (await this.bot.ctx.http.isPrivate(url)) {
const temp = this.bot.ctx.get('server.temp')
if (!temp) {
return this.bot.logger.warn('missing temporary file service, cannot send assets with private url')
}
entry = await temp.create(url)
url = entry.url
}
await this.flush()
let file_type = 0
Expand All @@ -210,14 +217,15 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
else return
const data: QQ.SendFileParams = {
file_type,
url: attrs.url,
url,
srv_send_msg: true,
}
if (this.session.isDirect) {
await this.bot.internal.sendFilePrivate(this.options.session.event.message.user.id, data)
} else {
await this.bot.internal.sendFileGuild(this.session.guildId, data)
}
entry?.dispose?.()
}

decodeButton(attrs: Dict, label: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/server-temp/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/server-temp",
"description": "Temp server plugin for cordis",
"version": "1.0.0",
"version": "1.0.1",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
Expand Down
19 changes: 15 additions & 4 deletions packages/server-temp/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context, Dict, Schema, Time } from '@satorijs/satori'
import {} from '@satorijs/router'
import { createReadStream } from 'fs'
import { fileURLToPath } from 'url'
import { mkdir, rm, writeFile } from 'fs/promises'
import internal from 'stream'

Expand All @@ -10,7 +11,7 @@ declare module '@satorijs/core' {
}
}

interface Entry {
export interface Entry {
path: string
url: string
dispose?: () => void
Expand All @@ -25,6 +26,10 @@ class TempServer {
constructor(protected ctx: Context, public config: TempServer.Config) {
const logger = ctx.logger('temp')

if (!ctx.router.config.selfUrl) {
logger.warn('missing configuration router.config.selfUrl')
}

ctx.router.get(config.path + '/:name', async (koa) => {
logger.debug(koa.params.name)
const entry = this.entries[koa.params.name]
Expand All @@ -46,12 +51,18 @@ class TempServer {
await rm(this.baseDir, { recursive: true })
}

async create(data: string | Buffer | internal.Readable) {
async create(data: string | Buffer | internal.Readable): Promise<Entry> {
const name = Math.random().toString(36).slice(2)
const url = this.ctx.router.selfUrl + this.config.path + '/' + name
const url = this.ctx.router.config.selfUrl! + this.config.path + '/' + name
let path: string
if (typeof data === 'string') {
path = data
if (new URL(data).protocol === 'file:') {
path = fileURLToPath(data)
} else {
data = await this.ctx.http.get(data, { responseType: 'stream' })
path = this.baseDir + name
await writeFile(path, data)
}
} else {
path = this.baseDir + name
await writeFile(path, data)
Expand Down

0 comments on commit aac353d

Please sign in to comment.