Skip to content

Commit

Permalink
Support for additional S3 providers (#71)
Browse files Browse the repository at this point in the history
* support for other s3 providers

* remove redundant route options

* use type safe env

* prettier
  • Loading branch information
mertd authored Jan 31, 2024
1 parent e6467b4 commit 08d75fd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ S3_UPLOAD_BUCKET=name-of-s3-bucket
S3_UPLOAD_REGION=us-east-1
```

You can also use other S3 providers by providing a custom endpoint:

```.env
S3_UPLOAD_ENDPOINT=http://localhost:9000
```

### Create expense from receipt

You can offer users to create expense by uploading a receipt. This feature relies on [OpenAI GPT-4 with Vision](https://platform.openai.com/docs/guides/vision).
Expand Down
29 changes: 21 additions & 8 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
/**
* Undefined entries are not supported. Push optional patterns to this array only if defined.
* @type {import('next/dist/shared/lib/image-config').RemotePattern}
*/
const remotePatterns = []

// S3 Storage
if (process.env.S3_UPLOAD_ENDPOINT) {
// custom endpoint for providers other than AWS
const url = new URL(process.env.S3_UPLOAD_ENDPOINT);
remotePatterns.push({
hostname: url.hostname,
})
} else if (process.env.S3_UPLOAD_BUCKET && process.env.S3_UPLOAD_REGION) {
// default provider
remotePatterns.push({
hostname: `${process.env.S3_UPLOAD_BUCKET}.s3.${process.env.S3_UPLOAD_REGION}.amazonaws.com`,
})
}

/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
remotePatterns:
process.env.S3_UPLOAD_BUCKET && process.env.S3_UPLOAD_REGION
? [
{
hostname: `${process.env.S3_UPLOAD_BUCKET}.s3.${process.env.S3_UPLOAD_REGION}.amazonaws.com`,
},
]
: [],
remotePatterns
},
}

Expand Down
4 changes: 4 additions & 0 deletions src/app/api/s3-upload/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { randomId } from '@/lib/api'
import { env } from '@/lib/env'
import { POST as route } from 'next-s3-upload/route'

export const POST = route.configure({
Expand All @@ -8,4 +9,7 @@ export const POST = route.configure({
const random = randomId()
return `document-${timestamp}-${random}${extension.toLowerCase()}`
},
endpoint: env.S3_UPLOAD_ENDPOINT,
// forcing path style is only necessary for providers other than AWS
forcePathStyle: !!env.S3_UPLOAD_ENDPOINT,
})
4 changes: 2 additions & 2 deletions src/components/expense-documents-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useToast } from '@/components/ui/use-toast'
import { randomId } from '@/lib/api'
import { ExpenseFormValues } from '@/lib/schemas'
import { Loader2, Plus, Trash, X } from 'lucide-react'
import { getImageData, useS3Upload } from 'next-s3-upload'
import { getImageData, usePresignedUpload } from 'next-s3-upload'
import Image from 'next/image'
import { useEffect, useState } from 'react'

Expand All @@ -29,7 +29,7 @@ type Props = {

export function ExpenseDocumentsInput({ documents, updateDocuments }: Props) {
const [pending, setPending] = useState(false)
const { FileInput, openFileDialog, uploadToS3 } = useS3Upload()
const { FileInput, openFileDialog, uploadToS3 } = usePresignedUpload() // use presigned uploads to addtionally support providers other than AWS
const { toast } = useToast()

const handleFileChange = async (file: File) => {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ const envSchema = z
S3_UPLOAD_SECRET: z.string().optional(),
S3_UPLOAD_BUCKET: z.string().optional(),
S3_UPLOAD_REGION: z.string().optional(),
S3_UPLOAD_ENDPOINT: z.string().optional(),
NEXT_PUBLIC_ENABLE_RECEIPT_EXTRACT: z.coerce.boolean().default(false),
OPENAI_API_KEY: z.string().optional(),
})
.superRefine((env, ctx) => {
if (
env.NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS &&
// S3_UPLOAD_ENDPOINT is fully optional as it will only be used for providers other than AWS
(!env.S3_UPLOAD_BUCKET ||
!env.S3_UPLOAD_KEY ||
!env.S3_UPLOAD_REGION ||
Expand Down

0 comments on commit 08d75fd

Please sign in to comment.