Skip to content

Commit 3d6f21b

Browse files
authored
fix: upload to storage (#161)
1 parent d963fd9 commit 3d6f21b

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

apps/web-app/server/services/telegram/wasabi-bot.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,14 @@ async function handlePhoto(ctx: Context) {
140140
return null
141141
}
142142

143+
let fileUrl
144+
143145
const downloadUrl = await getFileDownloadUrl({ ctx, fileId, botToken })
144-
if (!downloadUrl) {
145-
return
146+
if (downloadUrl) {
147+
const uploaded = await uploadToStorage(downloadUrl, fileId)
148+
fileUrl = uploaded.fileUrl
146149
}
147150

148-
const { fileUrl } = await uploadToStorage(downloadUrl, fileId)
149-
150151
await repository.ticket.createMessage({
151152
ticketId: data.ticket.id,
152153
userId: data.user.id,
@@ -177,13 +178,14 @@ async function handleVideo(ctx: Context) {
177178
return null
178179
}
179180

181+
let fileUrl
182+
180183
const downloadUrl = await getFileDownloadUrl({ ctx, fileId, botToken })
181-
if (!downloadUrl) {
182-
return
184+
if (downloadUrl) {
185+
const uploaded = await uploadToStorage(downloadUrl, fileId)
186+
fileUrl = uploaded.fileUrl
183187
}
184188

185-
const { fileUrl } = await uploadToStorage(downloadUrl, fileId)
186-
187189
await repository.ticket.createMessage({
188190
ticketId: data.ticket.id,
189191
userId: data.user.id,
@@ -214,13 +216,14 @@ async function handleFile(ctx: Context) {
214216
return null
215217
}
216218

219+
let fileUrl
220+
217221
const downloadUrl = await getFileDownloadUrl({ ctx, fileId, botToken })
218-
if (!downloadUrl) {
219-
return
222+
if (downloadUrl) {
223+
const uploaded = await uploadToStorage(downloadUrl, fileId)
224+
fileUrl = uploaded.fileUrl
220225
}
221226

222-
const { fileUrl } = await uploadToStorage(downloadUrl, fileId)
223-
224227
await repository.ticket.createMessage({
225228
ticketId: data.ticket.id,
226229
userId: data.user.id,
@@ -230,7 +233,7 @@ async function handleFile(ctx: Context) {
230233
text: `${ctx.message.caption ?? ''} ${ctx.message.document?.file_name ?? ''}`,
231234
})
232235

233-
logger.log('file', data.user.id, ctx.message.from.id, ctx.message.text, ctx.message.caption, ctx.message.document, downloadUrl)
236+
logger.log('file', data.user.id, ctx.message.from.id, ctx.message.caption, ctx.message.document, downloadUrl)
234237
ctx.reply('Файл передан в службу поддержки.')
235238
}
236239

@@ -259,14 +262,18 @@ async function getUserAndTicket(telegramId: string): Promise<{ user: User, ticke
259262
return { user: telegramUser.user, ticket }
260263
}
261264

262-
async function getFileDownloadUrl(data: { ctx: Context, fileId: string, botToken: string }) {
263-
// https://api.telegram.org/file/bot<token>/<file_path>
264-
const file = await data.ctx.api.getFile(data.fileId)
265-
if (!file) {
265+
async function getFileDownloadUrl(data: { ctx: Context, fileId: string, botToken: string }): Promise<string | null> {
266+
try {
267+
const file = await data.ctx.api.getFile(data.fileId)
268+
if (!file) {
269+
return null
270+
}
271+
272+
return `https://api.telegram.org/file/bot${data.botToken}/${file.file_path}`
273+
} catch (e) {
274+
logger.error('getFileDownloadUrl', e)
266275
return null
267276
}
268-
269-
return `https://api.telegram.org/file/bot${data.botToken}/${file.file_path}`
270277
}
271278

272279
async function getBotToken(): Promise<string | null> {

0 commit comments

Comments
 (0)