Skip to content

Commit

Permalink
feat: napcat 群文件获取下载地址
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Dec 11, 2024
1 parent 035ee56 commit 93eb3c4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
5 changes: 3 additions & 2 deletions main/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ model ForwardPair {
}

model File {
id Int @id @default(autoincrement())
roomId BigInt @db.BigInt
id Int @id @default(autoincrement())
roomId BigInt @db.BigInt
fileId String
info String
name String?
}

model FlashPhoto {
Expand Down
5 changes: 5 additions & 0 deletions main/src/client/NapCatClient/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ export class NapCatGroup extends NapCatEntity implements Group {
return false;
}
}

override async getFileUrl(fid: string): Promise<string> {
const data = await this.client.callApi('get_group_file_url', { group_id: this.gid, file_id: fid });
return data.url;
}
}

export class NapCatGroupMember extends NapCatUser implements GroupMember {
Expand Down
5 changes: 4 additions & 1 deletion main/src/controllers/FileAndFlashPhotoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export default class FileAndFlashPhotoController {
const fileInfo = await db.file.findFirst({
where: { id },
});
const downloadUrl = await (await this.oicq.getChat(Number(fileInfo.roomId))).getFileUrl(fileInfo.fileId);
const downloadUrl = new URL(await (await this.oicq.getChat(Number(fileInfo.roomId))).getFileUrl(fileInfo.fileId));
if (!downloadUrl.searchParams.get('fname')) {
downloadUrl.searchParams.set('fname', fileInfo.name);
}
await message.reply({
message: fileInfo.info + `\n<a href="${downloadUrl}">下载</a>`,
});
Expand Down
8 changes: 3 additions & 5 deletions main/src/services/ForwardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,10 @@ export default class ForwardService {
}
}
const dbEntry = await db.file.create({
data: { fileId: elem.fid, roomId: pair.qqRoomId, info: message },
data: { fileId: elem.fid, roomId: pair.qqRoomId, info: message, name: elem.name },
});
if (this.oicq instanceof OicqClient) {
buttons.push(Button.url('📎获取下载地址',
`https://t.me/${this.tgBot.me.username}?start=file-${dbEntry.id}`));
}
buttons.push(Button.url('📎获取下载地址',
`https://t.me/${this.tgBot.me.username}?start=file-${dbEntry.id}`));
break;
}
case 'record': {
Expand Down

0 comments on commit 93eb3c4

Please sign in to comment.