Skip to content

Commit d963fd9

Browse files
authored
feat: video and files upload to s3 (#160)
1 parent d3e69f5 commit d963fd9

2 files changed

Lines changed: 42 additions & 14 deletions

File tree

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,14 @@ async function handlePhoto(ctx: Context) {
145145
return
146146
}
147147

148-
const extension = downloadUrl.split('.').pop()
149-
const buffer = await fetch(downloadUrl).then((res) => res.arrayBuffer())
150-
151-
const fileUri = `/${S3_TELEGRAM_DIRECTORY}/${fileId}.${extension}`
152-
const fileUrl = `${mediaUrl}${fileUri}`
153-
154-
const storage = useStorage('s3')
155-
await storage.setItemRaw(fileUri, buffer)
148+
const { fileUrl } = await uploadToStorage(downloadUrl, fileId)
156149

157150
await repository.ticket.createMessage({
158151
ticketId: data.ticket.id,
159152
userId: data.user.id,
160153
telegramFileId: fileId,
161154
fileUrl,
155+
fileType: 'image',
162156
text: ctx.message.caption ?? '',
163157
})
164158

@@ -176,21 +170,30 @@ async function handleVideo(ctx: Context) {
176170
return
177171
}
178172

173+
const fileId = ctx.message.video.file_id
174+
179175
const botToken = await getBotToken()
180176
if (!botToken) {
181177
return null
182178
}
183179

184-
const downloadUrl = await getFileDownloadUrl({ ctx, fileId: ctx.message.video.file_id, botToken })
180+
const downloadUrl = await getFileDownloadUrl({ ctx, fileId, botToken })
181+
if (!downloadUrl) {
182+
return
183+
}
184+
185+
const { fileUrl } = await uploadToStorage(downloadUrl, fileId)
185186

186187
await repository.ticket.createMessage({
187188
ticketId: data.ticket.id,
188189
userId: data.user.id,
189-
telegramFileId: ctx.message.video.file_id,
190+
telegramFileId: fileId,
191+
fileUrl,
192+
fileType: 'video',
190193
text: ctx.message.caption ?? '',
191194
})
192195

193-
logger.log('video', data.user.id, ctx.message.from.id, ctx.message.text, ctx.message.caption, ctx.message.video, downloadUrl)
196+
logger.log('video', data.user.id, ctx.message.from.id, ctx.message.caption, ctx.message.video, downloadUrl)
194197
ctx.reply('Видео передано в службу поддержки.')
195198
}
196199

@@ -204,18 +207,27 @@ async function handleFile(ctx: Context) {
204207
return
205208
}
206209

210+
const fileId = ctx.message.document.file_id
211+
207212
const botToken = await getBotToken()
208213
if (!botToken) {
209214
return null
210215
}
211216

212-
const downloadUrl = await getFileDownloadUrl({ ctx, fileId: ctx.message.document.file_id, botToken })
217+
const downloadUrl = await getFileDownloadUrl({ ctx, fileId, botToken })
218+
if (!downloadUrl) {
219+
return
220+
}
221+
222+
const { fileUrl } = await uploadToStorage(downloadUrl, fileId)
213223

214224
await repository.ticket.createMessage({
215225
ticketId: data.ticket.id,
216226
userId: data.user.id,
217-
telegramFileId: ctx.message.document.file_id,
218-
text: `${ctx.message.caption ?? ''} ${ctx.message.document?.file_name ?? ''} ${ctx.message.document?.mime_type ?? ''}`,
227+
telegramFileId: fileId,
228+
fileUrl,
229+
fileType: 'document',
230+
text: `${ctx.message.caption ?? ''} ${ctx.message.document?.file_name ?? ''}`,
219231
})
220232

221233
logger.log('file', data.user.id, ctx.message.from.id, ctx.message.text, ctx.message.caption, ctx.message.document, downloadUrl)
@@ -266,6 +278,19 @@ async function getBotToken(): Promise<string | null> {
266278
return botInDb.token
267279
}
268280

281+
async function uploadToStorage(downloadUrl: string, fileId: string) {
282+
const extension = downloadUrl.split('.').pop()
283+
const buffer = await fetch(downloadUrl).then((res) => res.arrayBuffer())
284+
285+
const fileInnerUri = `/${S3_TELEGRAM_DIRECTORY}/${fileId}.${extension}`
286+
const fileUrl = `${mediaUrl}${fileInnerUri}`
287+
288+
const storage = useStorage('s3')
289+
await storage.setItemRaw(fileInnerUri, buffer)
290+
291+
return { fileUrl }
292+
}
293+
269294
export function useWasabiBot(): Bot {
270295
if (!bot) {
271296
throw new Error('Wasabi bot is not initialized. Call useCreateWasabiBot() first.')

packages/database/src/tables.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type TelegramUserType = 'private' | 'group' | 'supergroup' | 'channel'
4747

4848
type TicketStatus = 'opened' | 'closed'
4949

50+
type TicketFileType = 'image' | 'document' | 'video'
51+
5052
type CommunicationChannel = 'telegram'
5153
| 'vk'
5254
| 'website'
@@ -705,6 +707,7 @@ export const ticketMessages = pgTable('ticket_messages', {
705707
text: varchar('text').notNull(),
706708
telegramFileId: varchar('telegram_file_id'),
707709
fileUrl: varchar('file_url'),
710+
fileType: varchar('file_type').$type<TicketFileType>(),
708711
userId: cuid2('user_id').notNull().references(() => users.id, {
709712
onDelete: 'cascade',
710713
onUpdate: 'cascade',

0 commit comments

Comments
 (0)