feat: file by element id

This commit is contained in:
Wesley F. Young 2024-09-06 20:39:27 +08:00
parent b8fa1e163e
commit 5f30e4c252
2 changed files with 47 additions and 12 deletions

View File

@ -1,4 +1,4 @@
import { NapCatCore } from '@/core'; import { ChatType, FileElement, NapCatCore } from '@/core';
import { NapCatLaanaAdapter } from '..'; import { NapCatLaanaAdapter } from '..';
import { File as LaanaFile } from '../types/entity/file'; import { File as LaanaFile } from '../types/entity/file';
import path from 'path'; import path from 'path';
@ -18,10 +18,6 @@ export class LaanaFileUtils {
async resolveCacheIdFromLaanaFile(laanaFile: LaanaFile) { async resolveCacheIdFromLaanaFile(laanaFile: LaanaFile) {
if (laanaFile.uri.oneofKind === 'cacheId') { if (laanaFile.uri.oneofKind === 'cacheId') {
const cacheFilePath = path.join(this.cacheDir, laanaFile.uri.cacheId);
if (!fs.existsSync(cacheFilePath)) {
throw Error(`请求的缓存不存在: ${laanaFile.uri.cacheId}`);
}
return laanaFile.uri.cacheId; return laanaFile.uri.cacheId;
} else if (laanaFile.uri.oneofKind === 'url') { } else if (laanaFile.uri.oneofKind === 'url') {
return this.createCacheFromUrl(laanaFile.uri.url); return this.createCacheFromUrl(laanaFile.uri.url);
@ -32,7 +28,24 @@ export class LaanaFileUtils {
} }
} }
toLocalPath(cacheId: string) { async toLocalPath(cacheId: string, forceRevalidate = false) {
const cacheFilePath = path.join(this.cacheDir, cacheId);
if (!fs.existsSync(cacheFilePath) || forceRevalidate) {
if (cacheId.startsWith('@QQFileElement')) {
const { msgId, chatType, peerUid, fileElementId } = this.decodeFileElementCacheId(cacheId);
const downloadPath = await this.core.apis.FileApi.downloadMedia(
msgId,
chatType,
peerUid,
fileElementId,
'',
'',
);
await fsPromises.symlink(downloadPath, cacheFilePath);
} else {
throw Error(`请求的缓存不存在: ${cacheId}`);
}
}
return path.join(this.cacheDir, cacheId); return path.join(this.cacheDir, cacheId);
} }
@ -48,4 +61,26 @@ export class LaanaFileUtils {
async createCacheFromUrl(url: string) { async createCacheFromUrl(url: string) {
return this.createCacheFromBytes(await httpDownload({ url })); return this.createCacheFromBytes(await httpDownload({ url }));
} }
encodeFileElementToCacheId(
msgId: string,
chatType: ChatType,
peerUid: string,
fileElementId: string
) {
return `QQFileElement@${msgId}@${chatType}@${peerUid}@${fileElementId}`;
}
decodeFileElementCacheId(cacheId: string) {
if (!cacheId.startsWith('QQFileElement')) {
throw Error('不支持的缓存 ID');
}
const [, msgId, chatType, peerUid, fileElementId] = cacheId.split('@');
return {
msgId,
chatType: parseInt(chatType),
peerUid,
fileElementId
};
}
} }

View File

@ -1,7 +1,7 @@
import { AtType, ChatType, ElementType, NapCatCore, Peer, SendMessageElement, SendTextElement } from '@/core'; import { AtType, ChatType, ElementType, NapCatCore, Peer, SendMessageElement, SendTextElement } from '@/core';
import { NapCatLaanaAdapter } from '..'; import { NapCatLaanaAdapter } from '..';
import { OutgoingMessage, SendMessagePing } from '../types/action/message'; import { OutgoingMessage, SendMessagePing } from '../types/action/message';
import { Peer as LaanaPeer, Peer_Type } from '../types/entity/message'; import { Bubble, Message as LaanaMessage, Peer as LaanaPeer, Peer_Type } from '../types/entity/message';
import faceConfig from '@/core/external/face_config.json'; import faceConfig from '@/core/external/face_config.json';
type Laana2RawConverters = { type Laana2RawConverters = {
@ -143,7 +143,7 @@ export class LaanaMessageUtils {
} else if (content.oneofKind === 'image') { } else if (content.oneofKind === 'image') {
const cacheId = await this.laana.utils.file.resolveCacheIdFromLaanaFile(content.image); const cacheId = await this.laana.utils.file.resolveCacheIdFromLaanaFile(content.image);
elements.push(await this.core.apis.FileApi.createValidSendPicElement( elements.push(await this.core.apis.FileApi.createValidSendPicElement(
this.laana.utils.file.toLocalPath(cacheId) await this.laana.utils.file.toLocalPath(cacheId)
)); ));
fileCacheIds.push(cacheId); fileCacheIds.push(cacheId);
} else { } else {
@ -159,7 +159,7 @@ export class LaanaMessageUtils {
return { return {
elements: [ elements: [
await this.core.apis.FileApi.createValidSendFileElement( await this.core.apis.FileApi.createValidSendFileElement(
this.laana.utils.file.toLocalPath(cacheId), await this.laana.utils.file.toLocalPath(cacheId),
msgContent.name, msgContent.name,
), ),
], ],
@ -172,7 +172,7 @@ export class LaanaMessageUtils {
return { return {
elements: [ elements: [
await this.core.apis.FileApi.createValidSendPicElement( await this.core.apis.FileApi.createValidSendPicElement(
this.laana.utils.file.toLocalPath(cacheId), await this.laana.utils.file.toLocalPath(cacheId),
msgContent.displayText, // TODO: make display text optional msgContent.displayText, // TODO: make display text optional
// TODO: add 'sub type' field // TODO: add 'sub type' field
) )
@ -199,7 +199,7 @@ export class LaanaMessageUtils {
return { return {
elements: [ elements: [
await this.core.apis.FileApi.createValidSendVideoElement( await this.core.apis.FileApi.createValidSendVideoElement(
this.laana.utils.file.toLocalPath(cacheId), await this.laana.utils.file.toLocalPath(cacheId),
// TODO: add file name and thumb path // TODO: add file name and thumb path
), ),
], ],
@ -212,7 +212,7 @@ export class LaanaMessageUtils {
return { return {
elements: [ elements: [
await this.core.apis.FileApi.createValidSendPttElement( await this.core.apis.FileApi.createValidSendPttElement(
this.laana.utils.file.toLocalPath(cacheId), await this.laana.utils.file.toLocalPath(cacheId),
) )
], ],
fileCacheIds: [cacheId], fileCacheIds: [cacheId],