Compare commits
8 Commits
6c1dd703bc
...
8891cd23af
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8891cd23af | ||
|
|
661cebdb24 | ||
|
|
8b3b32422f | ||
|
|
dffa773acc | ||
|
|
51c6d1f0a6 | ||
|
|
bd35f5c3eb | ||
|
|
7409427ce5 | ||
|
|
7bc843d440 |
@@ -116,6 +116,37 @@ class Client {
|
|||||||
}
|
}
|
||||||
return re
|
return re
|
||||||
}
|
}
|
||||||
|
static async uploadFileLikeApi(fileName: string, fileData: ArrayBuffer | Blob | Response, chatId?: string) {
|
||||||
|
const form = new FormData()
|
||||||
|
form.append("file",
|
||||||
|
fileData instanceof ArrayBuffer
|
||||||
|
? new File([fileData], fileName, { type: 'application/octet-stream' })
|
||||||
|
: (
|
||||||
|
fileData instanceof Blob ? fileData :
|
||||||
|
new File([await fileData.arrayBuffer()], fileName, { type: 'application/octet-stream' })
|
||||||
|
)
|
||||||
|
)
|
||||||
|
form.append('file_name', fileName)
|
||||||
|
chatId && form.append('chat_id', chatId)
|
||||||
|
const re = await fetch('./upload_file', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
"Token": data.access_token,
|
||||||
|
"Device-Id": data.device_id,
|
||||||
|
} as HeadersInit,
|
||||||
|
body: form,
|
||||||
|
credentials: 'omit',
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
...await re.json(),
|
||||||
|
code: re.status,
|
||||||
|
} as ApiCallbackMessage
|
||||||
|
}
|
||||||
|
static async uploadFile(fileName: string, fileData: ArrayBuffer | Blob | Response, chatId?: string) {
|
||||||
|
const re = await this.uploadFileLikeApi(fileName, fileData, chatId)
|
||||||
|
if (re.code != 200) throw new Error(re.msg)
|
||||||
|
return re.data!.hash as string
|
||||||
|
}
|
||||||
static async updateCachedProfile() {
|
static async updateCachedProfile() {
|
||||||
this.myUserProfile = (await Client.invoke("User.getMyInfo", {
|
this.myUserProfile = (await Client.invoke("User.getMyInfo", {
|
||||||
token: data.access_token
|
token: data.access_token
|
||||||
|
|||||||
@@ -25,3 +25,14 @@ const onResize = () => {
|
|||||||
// deno-lint-ignore no-window no-window-prefix
|
// deno-lint-ignore no-window no-window-prefix
|
||||||
window.addEventListener('resize', onResize)
|
window.addEventListener('resize', onResize)
|
||||||
onResize()
|
onResize()
|
||||||
|
|
||||||
|
// @ts-ignore 工作正常, 这里是获取为 URL 以便于构建
|
||||||
|
import sw from './sw.ts?worker&url'
|
||||||
|
|
||||||
|
if ("serviceWorker" in navigator)
|
||||||
|
try {
|
||||||
|
navigator.serviceWorker
|
||||||
|
.register(sw as URL)
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
|||||||
30
client/sw.ts
Normal file
30
client/sw.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
interface FetchEvent extends Event {
|
||||||
|
waitUntil: (p: Promise<unknown | void>) => void
|
||||||
|
request: Request
|
||||||
|
respondWith: (r: Response | Promise<Response>) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
// 上传文件的代理与缓存
|
||||||
|
self.addEventListener("fetch", (e) => {
|
||||||
|
const event = e as FetchEvent
|
||||||
|
if (event.request.method != "GET" || event.request.url.indexOf("/uploaded_files/") == -1) return
|
||||||
|
|
||||||
|
event.respondWith(
|
||||||
|
(async () => {
|
||||||
|
const cache = await caches.open("LingChair-UploadedFile-Cache")
|
||||||
|
const cachedResponse = await cache.match(event.request)
|
||||||
|
if (cachedResponse) {
|
||||||
|
event.waitUntil(cache.add(event.request))
|
||||||
|
return cachedResponse
|
||||||
|
}
|
||||||
|
return fetch({
|
||||||
|
...event.request,
|
||||||
|
headers: {
|
||||||
|
...event.request.headers,
|
||||||
|
// 目前还不能获取 token
|
||||||
|
// localsotrage 在这里不可用
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})()
|
||||||
|
)
|
||||||
|
})
|
||||||
@@ -119,6 +119,7 @@ export default function App() {
|
|||||||
userProfileDialogRef.current!.open = true
|
userProfileDialogRef.current!.open = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ('Notification' in window) {
|
||||||
Notification.requestPermission()
|
Notification.requestPermission()
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
interface OnMessageData {
|
interface OnMessageData {
|
||||||
@@ -132,10 +133,15 @@ export default function App() {
|
|||||||
if (currentChatId != event.chat) {
|
if (currentChatId != event.chat) {
|
||||||
const chat = await DataCaches.getChatInfo(event.chat)
|
const chat = await DataCaches.getChatInfo(event.chat)
|
||||||
const user = await DataCaches.getUserProfile(event.msg.user_id)
|
const user = await DataCaches.getUserProfile(event.msg.user_id)
|
||||||
new Notification(`${user.nickname} (对话: ${chat.title})`, {
|
const notification = new Notification(`${user.nickname} (对话: ${chat.title})`, {
|
||||||
icon: getUrlForFileByHash(chat.avatar_file_hash),
|
icon: getUrlForFileByHash(chat.avatar_file_hash),
|
||||||
body: event.msg.text,
|
body: event.msg.text,
|
||||||
})
|
})
|
||||||
|
notification.addEventListener('click', () => {
|
||||||
|
setCurrentChatId(chat.id)
|
||||||
|
setIsShowChatFragment(true)
|
||||||
|
notification.close()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Client.on('Client.onMessage', onMessage)
|
Client.on('Client.onMessage', onMessage)
|
||||||
@@ -143,6 +149,7 @@ export default function App() {
|
|||||||
Client.off('Client.onMessage', onMessage)
|
Client.off('Client.onMessage', onMessage)
|
||||||
}
|
}
|
||||||
}, [currentChatId])
|
}, [currentChatId])
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{
|
<div style={{
|
||||||
|
|||||||
@@ -167,12 +167,16 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
|
|||||||
setIsMessageSending(true)
|
setIsMessageSending(true)
|
||||||
for (const fileName of Object.keys(cachedFiles.current)) {
|
for (const fileName of Object.keys(cachedFiles.current)) {
|
||||||
if (text.indexOf(fileName) != -1) {
|
if (text.indexOf(fileName) != -1) {
|
||||||
const re = await Client.invoke("Chat.uploadFile", {
|
/* const re = await Client.invoke("Chat.uploadFile", {
|
||||||
token: data.access_token,
|
token: data.access_token,
|
||||||
file_name: fileName,
|
file_name: fileName,
|
||||||
target,
|
target,
|
||||||
data: cachedFiles.current[fileName],
|
data: cachedFiles.current[fileName],
|
||||||
}, 5000)
|
}, 5000) */
|
||||||
|
const re = await Client.uploadFileLikeApi(
|
||||||
|
fileName,
|
||||||
|
cachedFiles.current[fileName]
|
||||||
|
)
|
||||||
if (checkApiSuccessOrSncakbar(re, `文件[${fileName}] 上传失败`)) return setIsMessageSending(false)
|
if (checkApiSuccessOrSncakbar(re, `文件[${fileName}] 上传失败`)) return setIsMessageSending(false)
|
||||||
text = text.replaceAll('(' + fileName + ')', '(tws://file?hash=' + re.data!.file_hash as string + ')')
|
text = text.replaceAll('(' + fileName + ')', '(tws://file?hash=' + re.data!.file_hash as string + ')')
|
||||||
}
|
}
|
||||||
@@ -225,16 +229,22 @@ export default function ChatFragment({ target, showReturnButton, onReturnButtonC
|
|||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
addFile(file.type, file.name, file)
|
addFile(file.type, file.name, file)
|
||||||
}
|
}
|
||||||
uploadChatAvatarRef.current!.value = ''
|
attachFileInputRef.current!.value = ''
|
||||||
})
|
})
|
||||||
useEventListener(uploadChatAvatarRef, 'change', async (_e) => {
|
useEventListener(uploadChatAvatarRef, 'change', async (_e) => {
|
||||||
const file = uploadChatAvatarRef.current!.files?.[0] as File
|
const file = uploadChatAvatarRef.current!.files?.[0] as File
|
||||||
if (file == null) return
|
if (file == null) return
|
||||||
|
|
||||||
const re = await Client.invoke("Chat.setAvatar", {
|
let re = await Client.uploadFileLikeApi(
|
||||||
|
'avatar',
|
||||||
|
file
|
||||||
|
)
|
||||||
|
if (checkApiSuccessOrSncakbar(re, "上传失败")) return
|
||||||
|
const hash = re.data!.file_hash
|
||||||
|
re = await Client.invoke("Chat.setAvatar", {
|
||||||
token: data.access_token,
|
token: data.access_token,
|
||||||
target: target,
|
target: target,
|
||||||
avatar: file
|
file_hash: hash,
|
||||||
})
|
})
|
||||||
uploadChatAvatarRef.current!.value = ''
|
uploadChatAvatarRef.current!.value = ''
|
||||||
|
|
||||||
|
|||||||
@@ -21,14 +21,23 @@ export default function MyProfileDialog({
|
|||||||
}: Refs) {
|
}: Refs) {
|
||||||
const editAvatarButtonRef = React.useRef<HTMLElement>(null)
|
const editAvatarButtonRef = React.useRef<HTMLElement>(null)
|
||||||
const chooseAvatarFileRef = React.useRef<HTMLInputElement>(null)
|
const chooseAvatarFileRef = React.useRef<HTMLInputElement>(null)
|
||||||
useEventListener(editAvatarButtonRef, 'click', () => chooseAvatarFileRef.current!.click())
|
useEventListener(editAvatarButtonRef, 'click', () => {
|
||||||
|
chooseAvatarFileRef.current!.value = ''
|
||||||
|
chooseAvatarFileRef.current!.click()
|
||||||
|
})
|
||||||
useEventListener(chooseAvatarFileRef, 'change', async (_e) => {
|
useEventListener(chooseAvatarFileRef, 'change', async (_e) => {
|
||||||
const file = chooseAvatarFileRef.current!.files?.[0] as File
|
const file = chooseAvatarFileRef.current!.files?.[0] as File
|
||||||
if (file == null) return
|
if (file == null) return
|
||||||
|
|
||||||
const re = await Client.invoke("User.setAvatar", {
|
let re = await Client.uploadFileLikeApi(
|
||||||
|
'avatar',
|
||||||
|
file
|
||||||
|
)
|
||||||
|
if (checkApiSuccessOrSncakbar(re, "上传失败")) return
|
||||||
|
const hash = re.data!.file_hash
|
||||||
|
re = await Client.invoke("User.setAvatar", {
|
||||||
token: data.access_token,
|
token: data.access_token,
|
||||||
avatar: file
|
file_hash: hash
|
||||||
})
|
})
|
||||||
|
|
||||||
if (checkApiSuccessOrSncakbar(re, "修改失败")) return
|
if (checkApiSuccessOrSncakbar(re, "修改失败")) return
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
"file-type": "npm:file-type@21.0.0",
|
"file-type": "npm:file-type@21.0.0",
|
||||||
"express": "npm:express@5.1.0",
|
"express": "npm:express@5.1.0",
|
||||||
"socket.io": "npm:socket.io@4.8.1",
|
"socket.io": "npm:socket.io@4.8.1",
|
||||||
"cookie-parser": "npm:cookie-parser@1.4.7"
|
"cookie-parser": "npm:cookie-parser@1.4.7",
|
||||||
|
"express-fileupload": "npm:express-fileupload@1.5.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ export default class ChatApi extends BaseApi {
|
|||||||
* @param file_name 文件名稱
|
* @param file_name 文件名稱
|
||||||
* @param data 文件二進制數據
|
* @param data 文件二進制數據
|
||||||
*/
|
*/
|
||||||
this.registerEvent("Chat.uploadFile", async (args, { deviceId }) => {
|
/* this.registerEvent("Chat.uploadFile", async (args, { deviceId }) => {
|
||||||
if (this.checkArgsMissing(args, ['token', 'target', 'data', 'file_name'])) return {
|
if (this.checkArgsMissing(args, ['token', 'target', 'data', 'file_name'])) return {
|
||||||
msg: "参数缺失",
|
msg: "参数缺失",
|
||||||
code: 400,
|
code: 400,
|
||||||
@@ -149,7 +149,7 @@ export default class ChatApi extends BaseApi {
|
|||||||
file_hash: file.getHash()
|
file_hash: file.getHash()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
}) */
|
||||||
/**
|
/**
|
||||||
* ======================================================
|
* ======================================================
|
||||||
* 加入对话申请
|
* 加入对话申请
|
||||||
@@ -463,14 +463,14 @@ export default class ChatApi extends BaseApi {
|
|||||||
})
|
})
|
||||||
// 更新頭像
|
// 更新頭像
|
||||||
this.registerEvent("Chat.setAvatar", (args, { deviceId }) => {
|
this.registerEvent("Chat.setAvatar", (args, { deviceId }) => {
|
||||||
if (this.checkArgsMissing(args, ['avatar', 'token'])) return {
|
if (this.checkArgsMissing(args, ['file_hash', 'token'])) return {
|
||||||
msg: "参数缺失",
|
msg: "参数缺失",
|
||||||
code: 400,
|
code: 400,
|
||||||
}
|
}
|
||||||
if (!(args.avatar instanceof Buffer)) return {
|
/* if (!(args.avatar instanceof Buffer)) return {
|
||||||
msg: "参数不合法",
|
msg: "参数不合法",
|
||||||
code: 400,
|
code: 400,
|
||||||
}
|
} */
|
||||||
const token = TokenManager.decode(args.token as string)
|
const token = TokenManager.decode(args.token as string)
|
||||||
|
|
||||||
const user = User.findById(token.author) as User
|
const user = User.findById(token.author) as User
|
||||||
@@ -483,9 +483,10 @@ export default class ChatApi extends BaseApi {
|
|||||||
|
|
||||||
if (chat.bean.type == 'group')
|
if (chat.bean.type == 'group')
|
||||||
if (chat.checkUserIsAdmin(user.bean.id)) {
|
if (chat.checkUserIsAdmin(user.bean.id)) {
|
||||||
const avatar: Buffer = args.avatar as Buffer
|
chat.setAvatarFileHash(args.file_hash as string)
|
||||||
|
/* const avatar: Buffer = args.avatar as Buffer
|
||||||
if (avatar)
|
if (avatar)
|
||||||
chat.setAvatar(avatar)
|
chat.setAvatar(avatar) */
|
||||||
} else
|
} else
|
||||||
return {
|
return {
|
||||||
code: 403,
|
code: 403,
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ export default class TokenManager {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 嚴格檢驗令牌: 時間, 用戶, (設備 ID)
|
* 根据时间, 用户, 设备 ID (和类型 (默认访问令牌)) 检验令牌
|
||||||
*/
|
*/
|
||||||
static checkToken(token: Token, deviceId?: string, type: TokenType = 'access_token') {
|
static checkToken(token: Token, deviceId?: string, type: TokenType = 'access_token') {
|
||||||
if (token.expired_time < Date.now()) return false
|
if (token.expired_time < Date.now()) return false
|
||||||
|
|||||||
@@ -198,23 +198,19 @@ export default class UserApi extends BaseApi {
|
|||||||
*/
|
*/
|
||||||
// 更新頭像
|
// 更新頭像
|
||||||
this.registerEvent("User.setAvatar", (args, { deviceId }) => {
|
this.registerEvent("User.setAvatar", (args, { deviceId }) => {
|
||||||
if (this.checkArgsMissing(args, ['avatar', 'token'])) return {
|
if (this.checkArgsMissing(args, ['file_hash', 'token'])) return {
|
||||||
msg: "参数缺失",
|
msg: "参数缺失",
|
||||||
code: 400,
|
code: 400,
|
||||||
}
|
}
|
||||||
if (!(args.avatar instanceof Buffer)) return {
|
|
||||||
msg: "参数不合法",
|
|
||||||
code: 400,
|
|
||||||
}
|
|
||||||
const token = TokenManager.decode(args.token as string)
|
const token = TokenManager.decode(args.token as string)
|
||||||
if (!this.checkToken(token, deviceId)) return {
|
if (!this.checkToken(token, deviceId)) return {
|
||||||
code: 401,
|
code: 401,
|
||||||
msg: "令牌无效",
|
msg: "令牌无效",
|
||||||
}
|
}
|
||||||
|
|
||||||
const avatar: Buffer = args.avatar as Buffer
|
// const avatar: Buffer = args.avatar as Buffer
|
||||||
const user = User.findById(token.author)
|
const user = User.findById(token.author)
|
||||||
user!.setAvatar(avatar)
|
user!.setAvatarFileHash(args.file_hash as string) //.setAvatar(avatar)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
msg: "成功",
|
msg: "成功",
|
||||||
|
|||||||
@@ -231,7 +231,10 @@ export default class Chat {
|
|||||||
if (this.bean.type == 'group') return this.bean.avatar_file_hash
|
if (this.bean.type == 'group') return this.bean.avatar_file_hash
|
||||||
if (this.bean.type == 'private') return this.getAnotherUserForPrivate(userMySelf as User)?.getAvatarFileHash()
|
if (this.bean.type == 'private') return this.getAnotherUserForPrivate(userMySelf as User)?.getAvatarFileHash()
|
||||||
}
|
}
|
||||||
|
setAvatarFileHash(hash: string) {
|
||||||
|
this.setAttr("avatar_file_hash", hash)
|
||||||
|
}
|
||||||
async setAvatar(avatar: Buffer) {
|
async setAvatar(avatar: Buffer) {
|
||||||
this.setAttr("avatar_file_hash", (await FileManager.uploadFile(`avatar_chat_${this.bean.count}`, avatar)).getHash())
|
this.setAvatarFileHash((await FileManager.uploadFile(`avatar_chat_${this.bean.count}`, avatar)).getHash())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,9 @@ class File {
|
|||||||
getUploadTime() {
|
getUploadTime() {
|
||||||
return this.bean.upload_time
|
return this.bean.upload_time
|
||||||
}
|
}
|
||||||
|
updateLastUsedTime() {
|
||||||
|
this.setAttr("last_used_time", Date.now())
|
||||||
|
}
|
||||||
getLastUsedTime() {
|
getLastUsedTime() {
|
||||||
return this.bean.last_used_time
|
return this.bean.last_used_time
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -167,7 +167,10 @@ export default class User {
|
|||||||
getAvatarFileHash() {
|
getAvatarFileHash() {
|
||||||
return this.bean.avatar_file_hash
|
return this.bean.avatar_file_hash
|
||||||
}
|
}
|
||||||
|
setAvatarFileHash(hash: string) {
|
||||||
|
this.setAttr("avatar_file_hash", hash)
|
||||||
|
}
|
||||||
async setAvatar(avatar: Buffer) {
|
async setAvatar(avatar: Buffer) {
|
||||||
this.setAttr("avatar_file_hash", (await FileManager.uploadFile(`avatar_user_${this.bean.count}`, avatar)).getHash())
|
this.setAvatarFileHash((await FileManager.uploadFile(`avatar_user_${this.bean.count}`, avatar)).getHash())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,32 +16,41 @@ import UserChatLinker from "./data/UserChatLinker.ts"
|
|||||||
import path from "node:path"
|
import path from "node:path"
|
||||||
import cookieParser from 'cookie-parser'
|
import cookieParser from 'cookie-parser'
|
||||||
import fs from 'node:fs/promises'
|
import fs from 'node:fs/promises'
|
||||||
|
// @ts-types="npm:@types/express-fileupload"
|
||||||
|
import fileUpload from 'express-fileupload'
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use('/', express.static(config.data_path + '/page_compiled'))
|
app.use('/', express.static(config.data_path + '/page_compiled'))
|
||||||
app.use(cookieParser())
|
app.use(cookieParser())
|
||||||
app.get('/uploaded_files/:hash', (req, res) => {
|
app.get('/uploaded_files/:hash', (req, res) => {
|
||||||
const hash = req.params.hash as string
|
const hash = req.params.hash as string
|
||||||
res.setHeader('Content-Type', 'text/plain')
|
|
||||||
if (hash == null) {
|
if (hash == null) {
|
||||||
res.status(404).send("404 Not Found")
|
res.status(404).send({
|
||||||
|
msg: "404 Not Found",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const file = FileManager.findByHash(hash)
|
const file = FileManager.findByHash(hash)
|
||||||
|
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
res.status(404).send("404 Not Found")
|
res.status(404).send({
|
||||||
|
msg: "404 Not Found",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file.getChatId() != null) {
|
if (file.getChatId() != null) {
|
||||||
const userToken = TokenManager.decode(req.cookies.token)
|
const userToken = TokenManager.decode(req.headers.token || req.cookies.token)
|
||||||
if (!TokenManager.checkToken(userToken, req.cookies.device_id)) {
|
if (!TokenManager.checkToken(userToken, req.headers['device-id'] || req.cookies.device_id)) {
|
||||||
res.status(401).send("401 UnAuthorized")
|
res.status(401).send({
|
||||||
|
msg: "401 UnAuthorized",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!UserChatLinker.checkUserIsLinkedToChat(userToken.author, file.getChatId() as string)) {
|
if (!UserChatLinker.checkUserIsLinkedToChat(userToken.author, file.getChatId() as string)) {
|
||||||
res.status(403).send("403 Forbidden")
|
res.status(403).send({
|
||||||
|
msg: "403 Forbidden",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,6 +59,53 @@ app.get('/uploaded_files/:hash', (req, res) => {
|
|||||||
res.setHeader('Content-Disposition', `inline; filename="${fileName}"`)
|
res.setHeader('Content-Disposition', `inline; filename="${fileName}"`)
|
||||||
res.setHeader('Content-Type', file!.getMime())
|
res.setHeader('Content-Type', file!.getMime())
|
||||||
res.sendFile(path.resolve(file!.getFilePath()))
|
res.sendFile(path.resolve(file!.getFilePath()))
|
||||||
|
file.updateLastUsedTime()
|
||||||
|
})
|
||||||
|
|
||||||
|
await fs.mkdir(config.data_path + '/upload_cache', { recursive: true })
|
||||||
|
app.use(fileUpload({
|
||||||
|
limits: { fileSize: 2 * 1024 * 1024 * 1024 },
|
||||||
|
useTempFiles: true,
|
||||||
|
tempFileDir: config.data_path + '/upload_cache',
|
||||||
|
abortOnLimit: true,
|
||||||
|
}))
|
||||||
|
app.post('/upload_file', async (req, res) => {
|
||||||
|
const userToken = TokenManager.decode(req.headers.token || req.cookies.token)
|
||||||
|
if (!TokenManager.checkToken(userToken, req.headers['device-id'] || req.cookies.device_id)) {
|
||||||
|
res.status(401).send({
|
||||||
|
msg: "401 UnAuthorized",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (req.body.chat_id && !UserChatLinker.checkUserIsLinkedToChat(userToken.author, req.body.chat_id)) {
|
||||||
|
res.status(403).send({
|
||||||
|
msg: "403 Forbidden",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const file = req.files?.file as fileUpload.UploadedFile
|
||||||
|
if (file?.data == null) {
|
||||||
|
res.status(400).send({
|
||||||
|
msg: "No file was found or multiple files were uploaded",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (req.body.file_name == null) {
|
||||||
|
res.status(400).send({
|
||||||
|
msg: "Filename is required",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const hash = (await FileManager.uploadFile(req.body.file_name, await fs.readFile(file.tempFilePath), req.body.chat_id)).getHash()
|
||||||
|
|
||||||
|
res.status(200).send({
|
||||||
|
msg: "success",
|
||||||
|
data: {
|
||||||
|
file_hash: hash,
|
||||||
|
},
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const httpServer: HttpServerLike = (
|
const httpServer: HttpServerLike = (
|
||||||
@@ -62,7 +118,7 @@ const httpServer: HttpServerLike = (
|
|||||||
http.createServer(app)
|
http.createServer(app)
|
||||||
)
|
)
|
||||||
const io = new SocketIo.Server(httpServer, {
|
const io = new SocketIo.Server(httpServer, {
|
||||||
maxHttpBufferSize: 1e114514,
|
transports: ["polling", "websocket", "webtransport"],
|
||||||
})
|
})
|
||||||
|
|
||||||
ApiManager.initServer(httpServer, io)
|
ApiManager.initServer(httpServer, io)
|
||||||
|
|||||||
Reference in New Issue
Block a user