Skip to content

Commit

Permalink
refactor: Reconstruct file upload logic to be compatible with docker …
Browse files Browse the repository at this point in the history
…deployment solution

refactor: Use manifest.json instead of dynamically generating manifest solutions
  • Loading branch information
Amery2010 committed May 26, 2024
1 parent a315322 commit 37424ae
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 122 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ENV NODE_ENV production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,14 @@ pnpm dev
```shell
docker pull xiangfa/talk-with-gemini:latest

docker run -d --name talk-with-gemini -p 3000:3000 xiangfa/talk-with-gemini
docker run -d --name talk-with-gemini -p 5481:3000 xiangfa/talk-with-gemini
```

You can also specify additional environment variables:

```shell
docker run -d --name talk-with-gemini \
-p 3000:3000 \
-p 5481:3000 \
-e GEMINI_API_KEY=AIza... \
-e ACCESS_PASSWORD=your-password \
xiangfa/talk-with-gemini
Expand All @@ -313,14 +313,14 @@ If you need to specify other environment variables, please add `-e key=value` to
```shell
docker pull xiangfa/talk-with-gemini:latest

docker run -d --name talk-with-gemini -p 3000:3000 xiangfa/talk-with-gemini
docker run -d --name talk-with-gemini -p 5481:3000 xiangfa/talk-with-gemini
```

您也可以指定额外的环境变量:

```shell
docker run -d --name talk-with-gemini \
-p 3000:3000 \
-p 5481:3000 \
-e GEMINI_API_KEY=AIza... \
-e ACCESS_PASSWORD=页面访问密码 \
xiangfa/talk-with-gemini
Expand Down
39 changes: 39 additions & 0 deletions app/api/files/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NextResponse, type NextRequest } from 'next/server'
import { checkToken, handleError } from '../utils'
import FileManager from '@/utils/FileManager'
import { ErrorType } from '@/constant/errors'
import { isNull } from 'lodash-es'

const geminiApiKey = process.env.GEMINI_API_KEY as string
const geminiApiBaseUrl = process.env.GEMINI_API_BASE_URL as string
const mode = process.env.NEXT_PUBLIC_BUILD_MODE

export const preferredRegion = ['sfo1']

export async function GET(req: NextRequest) {
if (mode === 'export') return new NextResponse('Not available under static deployment')

const searchParams = req.nextUrl.searchParams
const token = searchParams.get('token')
const id = searchParams.get('id')

if (isNull(token) || !checkToken(token)) {
return NextResponse.json({ code: 40301, message: ErrorType.InValidToken }, { status: 403 })
}
if (!geminiApiKey) {
return NextResponse.json({ code: 50001, message: ErrorType.NoGeminiKey }, { status: 500 })
}
if (isNull(id)) {
return NextResponse.json({ code: 40001, message: ErrorType.MissingParam }, { status: 400 })
}

try {
const fileManager = new FileManager({ apiKey: geminiApiKey, baseUrl: geminiApiBaseUrl })
const result = await fileManager.getFileMetadata(id)
return NextResponse.json(result)
} catch (error) {
if (error instanceof Error) {
return handleError(error.message)
}
}
}
6 changes: 4 additions & 2 deletions app/api/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ export async function POST(req: NextRequest) {
const url = uploadUrl.toString()
return NextResponse.json({ url }, { headers: { Location: url } })
} else {
req.nextUrl.pathname = req.nextUrl.pathname.replace('api/', 'api/google')
return NextResponse.redirect(req.nextUrl, 308)
const formData = await req.formData()
const fileManager = new FileManager({ apiKey: geminiApiKey, baseUrl: geminiApiBaseUrl })
const result = await fileManager.uploadFile(formData.get('file') as File)
return NextResponse.json(result)
}
} catch (error) {
if (error instanceof Error) {
Expand Down
54 changes: 0 additions & 54 deletions app/manifest.ts

This file was deleted.

6 changes: 4 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ export default function Home() {
if (messagesRef.current[messageIndex].role === 'model') {
rovokeMessage(id)
} else {
rovokeMessage(messagesRef.current[messageIndex + 1].id)
const nextMessage = messagesRef.current[messageIndex + 1]
if (nextMessage) rovokeMessage(messagesRef.current[messageIndex + 1].id)
}
}
}
Expand Down Expand Up @@ -470,6 +471,7 @@ export default function Home() {
const handleFileUpload = useCallback(
async (files: FileList | null) => {
if (!supportAttachment) return false
if (!checkAccessStatus()) return false

const fileList: File[] = []

Expand All @@ -495,7 +497,7 @@ export default function Home() {
}
}
},
[supportAttachment, isOldVisionModel],
[supportAttachment, isOldVisionModel, checkAccessStatus],
)

const handlePaste = useCallback(
Expand Down
4 changes: 2 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const { PHASE_PRODUCTION_BUILD, PHASE_EXPORT } = require('next/constants')

const mode = process.env.NEXT_PUBLIC_BUILD_MODE
const basePath = process.env.EXPORT_BASE_PATH || ''
const apiKey = process.env.GEMINI_API_KEY ?? ''
const uploadProxyUrl = process.env.GEMINI_UPLOAD_BASE_URL ?? 'https://generativelanguage.googleapis.com'
const apiKey = process.env.GEMINI_API_KEY || ''
const uploadProxyUrl = process.env.GEMINI_UPLOAD_BASE_URL || 'https://generativelanguage.googleapis.com'

/** @type {(phase: string, defaultConfig: import("next").NextConfig) => Promise<import("next").NextConfig>} */
module.exports = async (phase) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "talk-with-gemini",
"version": "0.10.4",
"version": "0.10.5",
"private": true,
"author": "Amery2010 <[email protected]>",
"license": "GPL-3.0-only",
Expand Down
46 changes: 46 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "Talk with Gemini",
"short_name": "Talk with Gemini",
"description": "Deploy your private Gemini application for free with one click, supporting Gemini 1.5 Pro, Gemini 1.5 Flash, Gemini Pro and Gemini Pro Vision models.",
"id": "talk-with-gemini",
"icons": [
{
"src": "icons/logo-192x192.png",
"sizes": "72x72 96x96 128x128 152x152 167x167 180x180 192x192",
"type": "image/png",
"purpose": "maskable"
},
{
"src": "icons/logo-512x512.png",
"sizes": "256x256 512x512",
"type": "image/png"
},
{
"src": "logo.svg",
"sizes": "any",
"type": "svg+xml"
}
],
"screenshots": [
{
"src": "screenshots/app.jpg",
"sizes": "2048x1192",
"type": "image/jpeg"
},
{
"src": "screenshots/gemini-1.5.jpg",
"sizes": "1920x863",
"type": "image/jpeg"
},
{
"src": "screenshots/assistant-market.jpg",
"sizes": "1920x872",
"type": "image/jpeg"
}
],
"theme_color": "#FFFFFF",
"background_color": "#FFFFFF",
"start_url": ".",
"display": "standalone",
"orientation": "portrait"
}
2 changes: 1 addition & 1 deletion public/sw.js

Large diffs are not rendered by default.

Loading

0 comments on commit 37424ae

Please sign in to comment.