generated from taylorbryant/gatsby-starter-tailwind
-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix broken photo upload/delete (#1226)
- Loading branch information
Showing
10 changed files
with
241 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
import { customAlphabet } from 'nanoid' | ||
import { nolookalikesSafe } from 'nanoid-dictionary' | ||
import { extname } from 'path' | ||
|
||
import { withUserAuth, PREDEFINED_HEADERS } from '@/js/auth/withUserAuth' | ||
import { getSignedUrlForUpload } from '@/js/media/storageClient' | ||
|
||
export interface MediaPreSignedProps { | ||
url: string | ||
fullFilename: string | ||
} | ||
|
||
/** | ||
* Endpoint for getting a signed url to upload a media file to remote cloud storage. | ||
* Usage: `/api/user/get-signed-url?filename=image001.jpg` | ||
* See https://cloud.google.com/storage/docs/access-control/signed-urls | ||
*/ | ||
const getHanlder = async (req: NextRequest): Promise<any> => { | ||
try { | ||
const fullFilename = prepareFilenameFromRequest(req) | ||
if (fullFilename == null) { | ||
return NextResponse.json({ status: 400 }) | ||
} | ||
const url = await getSignedUrlForUpload(fullFilename) | ||
|
||
return NextResponse.json({ url, fullFilename: '/' + fullFilename }) | ||
} catch (e) { | ||
console.error('Uploading to media server failed', e) | ||
return NextResponse.json({ status: 500 }) | ||
} | ||
} | ||
|
||
export const GET = withUserAuth(getHanlder) | ||
|
||
/** | ||
* Random filename generator | ||
*/ | ||
const safeFilename = (original: string): string => { | ||
return safeRandomString() + extname(original) | ||
} | ||
|
||
const safeRandomString = customAlphabet(nolookalikesSafe, 10) | ||
|
||
/** | ||
* Construct the S3 path for a given media file and an authenticated user. Format: `u/{user_uuid}/{filename}`. | ||
* It's super important **not** to have the leading slash '/'. | ||
*/ | ||
export const prepareFilenameFromRequest = (req: NextRequest): string | null => { | ||
const searchParams = req.nextUrl.searchParams | ||
const filename = searchParams.get('filename') | ||
if (filename == null) { | ||
return null | ||
} | ||
|
||
const uuid = req.headers.get(PREDEFINED_HEADERS.user_uuid) | ||
if (uuid == null) { | ||
return null | ||
} | ||
|
||
/** | ||
* Important: no starting slash / when working with buckets | ||
*/ | ||
return `u/${uuid}/${safeFilename(filename)}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
import { withUserAuth } from '@/js/auth/withUserAuth' | ||
import { deleteMediaFromBucket } from '@/js/media/storageClient' | ||
import { prepareFilenameFromRequest } from '../get-signed-url/route' | ||
|
||
/** | ||
* Endpoint for removing a media object from remote cloud storage | ||
*/ | ||
const postHandler = async (req: NextRequest): Promise<any> => { | ||
try { | ||
const filename = prepareFilenameFromRequest(req) | ||
if (filename == null) { | ||
return NextResponse.json({ status: 400 }) | ||
} | ||
await deleteMediaFromBucket(filename) | ||
return NextResponse.json({ status: 200 }) | ||
} catch (e) { | ||
console.log('Removing file from media server failed', e) | ||
return NextResponse.json({ status: 500 }) | ||
} | ||
} | ||
|
||
export const POST = withUserAuth(postHandler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.