Skip to content

Commit

Permalink
feat dynamic max file size
Browse files Browse the repository at this point in the history
  • Loading branch information
oddyamill committed Oct 6, 2024
1 parent dc8fa7e commit 2075f50
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 22 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
},
"dependencies": {
"@oddyamill/discord-workers": "^1.1.5",
"discord-api-types": "^0.37.98"
"discord-api-types": "^0.37.101"
}
}
2 changes: 1 addition & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const command = async (interaction: Interaction, t: Translator, env: Env,
return editResponse(interaction, { content: t(error) })
}

const media = await resolveMedia(data, response, env)
const media = await resolveMedia(data, response, interaction, env)

if (media === null) {
return editResponse(interaction, { content: t('video_not_supported') })
Expand Down
2 changes: 0 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ export const TIKTOK_ENDPOINT = 'https://tiktok.com/@/video/'

export const TIKTOK_USER_ENDPOINT = 'https://tiktok.com/@'

export const MAX_FILE_LENGTH = 25 * 1024 * 1024

// https://stackoverflow.com/questions/74077377/regular-expression-to-match-any-tiktok-video-id-and-url#comment130792938_74077377
export const TIKTOK_URL_REGEXP =
/https:\/\/(?:m|www|vm|vt)?\.?tiktok\.com\/((?:.*\b(?:(?:usr|v|embed|user|video|photo)\/|\?shareId=|&item_id=)(\d+))|\w+)/
Expand Down
4 changes: 3 additions & 1 deletion src/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
APIChatInputApplicationCommandInteraction,
APIMessageApplicationCommandInteraction,
APIPartialGuild,
} from 'discord-api-types/v10'

export type Interaction =
export type Interaction = { guild: APIPartialGuild | null } & (
| APIChatInputApplicationCommandInteraction
| APIMessageApplicationCommandInteraction
)
18 changes: 18 additions & 0 deletions src/utils/resolveMaxFileSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { GuildFeature } from 'discord-api-types/v10'
import { Interaction } from '../interaction'

export const resolveMaxFileSize = (interaction: Interaction) => {
const features = interaction.guild?.features ?? []

switch (true) {
case features?.includes(GuildFeature.AnimatedBanner):
return 100 * 1024 * 1024

// wtf??
case features?.includes('SEVEN_DAY_THREAD_ARCHIVE' as GuildFeature):
return 50 * 1024 * 1024

default:
return 25 * 1024 * 1024
}
}
26 changes: 13 additions & 13 deletions src/utils/resolveMedia.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { ItemStruct } from '../tiktok'
import {
TIKTOK_HEADERS,
MAX_FILE_LENGTH,
IMAGE_WORKER_CACHE_TTL,
} from '../constants'
import { TIKTOK_HEADERS, IMAGE_WORKER_CACHE_TTL } from '../constants'
import { resolveCookie } from './resolveCookie'
import { Env } from '../env'
import { Interaction } from '../interaction'
import { resolveMaxFileSize } from './resolveMaxFileSize'

export interface Media {
stream: Response
format: string
}

export const resolveMedia = async (tiktok: ItemStruct, response: Response, env: Env): Promise<Media | null> => {
export const resolveMedia = async (tiktok: ItemStruct, response: Response, interaction: Interaction, env: Env): Promise<Media | null> => {
const init: RequestInit = {
headers: {
Cookie: resolveCookie(response),
...TIKTOK_HEADERS,
},
}

const maxFileSize = resolveMaxFileSize(interaction)

if (tiktok.imagePost !== undefined) {
return resolveImage(tiktok, init, env)
return resolveImage(tiktok, init, maxFileSize, env)
}

if (tiktok.video.bitrateInfo !== undefined) {
return resolveVideo(tiktok, init)
return resolveVideo(tiktok, init, maxFileSize)
}

return null
}

const resolveImage = async (tiktok: ItemStruct, init: RequestInit, env: Env): Promise<Media> => {
const resolveImage = async (tiktok: ItemStruct, init: RequestInit, maxFileSize: number, env: Env): Promise<Media> => {
const { imagePost } = tiktok

if (imagePost!.images.length > 1 && env.IMAGE_WORKER_ENDPOINT !== undefined) {
Expand All @@ -48,7 +48,7 @@ const resolveImage = async (tiktok: ItemStruct, init: RequestInit, env: Env): Pr

if (
stream.ok &&
+stream.headers.get('content-length')! <= MAX_FILE_LENGTH
+stream.headers.get('content-length')! <= maxFileSize
) {
return {
stream,
Expand All @@ -65,9 +65,9 @@ const resolveImage = async (tiktok: ItemStruct, init: RequestInit, env: Env): Pr
}
}

const resolveVideo = async (tiktok: ItemStruct, init: RequestInit): Promise<Media | null> => {
const resolveVideo = async (tiktok: ItemStruct, init: RequestInit, maxFileSize: number): Promise<Media | null> => {
for (const bitrateInfo of tiktok.video.bitrateInfo!) {
if (bitrateInfo.DataSize > MAX_FILE_LENGTH) {
if (bitrateInfo.DataSize > maxFileSize) {
continue
}

Expand All @@ -78,7 +78,7 @@ const resolveVideo = async (tiktok: ItemStruct, init: RequestInit): Promise<Medi
continue
}

if (+stream.headers.get('content-length')! > MAX_FILE_LENGTH) {
if (+stream.headers.get('content-length')! > maxFileSize) {
await stream.body?.cancel()
continue
}
Expand Down

0 comments on commit 2075f50

Please sign in to comment.