Skip to content

Commit

Permalink
chore: remove returnDirectResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
okdargy committed Mar 16, 2024
1 parent 5727462 commit 351f4f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
28 changes: 21 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { cache } from 'hono/cache'
import { grabAwemeId, getVideoInfo } from './services/tiktok'
import { VideoResponse, ErrorResponse } from './templates'
import generateAlternate from './util/generateAlternate'
import { returnHTMLResponse, returnDirectResponse } from './util/ResponseHelper'
import { returnHTMLResponse } from './util/ResponseHelper'

const app = new Hono()

Expand Down Expand Up @@ -57,7 +57,7 @@ async function handleVideo(c: any): Promise<Response> {
id = awemeId
} catch(e) {
const responseContent = await ErrorResponse((e as Error).message);
return returnHTMLResponse(responseContent, 201) as Response;
return returnHTMLResponse(responseContent, 201);
}
}

Expand All @@ -66,20 +66,34 @@ async function handleVideo(c: any): Promise<Response> {

if (videoInfo instanceof Error) {
const responseContent = await ErrorResponse((videoInfo as Error).message);
return returnHTMLResponse(responseContent, 201) as Response;
return returnHTMLResponse(responseContent, 201);
}

const url = new URL(c.req.url);

if(url.hostname.includes('d.tnktok.com')) {
return returnDirectResponse(videoInfo) as Response;
if(url.hostname.includes('d.tnktok.com') || c.req.query('isDirect') === 'true') {
if(videoInfo.video.duration > 0) {
return new Response('', {
status: 302,
headers: {
'Location': 'https://fxtiktok-rewrite.dargy.workers.dev/generate/video/' + videoInfo.aweme_id
}
})
} else {
return new Response('', {
status: 302,
headers: {
'Location': 'https://fxtiktok-rewrite.dargy.workers.dev/generate/image/' + videoInfo.aweme_id
}
})
}
} else {
const responseContent = await VideoResponse(videoInfo);
return returnHTMLResponse(responseContent) as Response;
return returnHTMLResponse(responseContent);
}
} catch(e) {
const responseContent = await ErrorResponse((e as Error).message);
return returnHTMLResponse(responseContent, 201) as Response;
return returnHTMLResponse(responseContent, 201);
}
}

Expand Down
20 changes: 1 addition & 19 deletions src/util/ResponseHelper.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import { HandlerResponse } from "hono/types"
import { AwemeList } from "../types/Services"
export const returnHTMLResponse = (content: string, status?: number): HandlerResponse<Response> => {
export const returnHTMLResponse = (content: string, status?: number): Response => {
return new Response(content, {
status: status || 200,
headers: {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'public, max-age=3600'
}
})
}

export const returnDirectResponse = (videoInfo: AwemeList): HandlerResponse<Response> => {
if(videoInfo.video.duration !== 0) {
return new Response('', {
status: 302,
headers: {
'Location': `https://fxtiktok-rewrite.dargy.workers.dev/generate/video/` + videoInfo.aweme_id
}
})
} else {
return new Response('', {
status: 302,
headers: {
'Location': videoInfo.video.cover.url_list[0]
}
})
}
}

0 comments on commit 351f4f3

Please sign in to comment.