-
Notifications
You must be signed in to change notification settings - Fork 292
Feature/Complete functionality to delete documents #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
psbhatbvbcs
wants to merge
4
commits into
Nutlope:main
Choose a base branch
from
psbhatbvbcs:feature/deleteDocuments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,116 @@ | ||
| import { NextResponse } from 'next/server'; | ||
| import prisma from '@/utils/prisma'; | ||
| import { getAuth } from '@clerk/nextjs/server'; | ||
| import { Pinecone } from '@pinecone-database/pinecone'; | ||
|
|
||
| interface DeleteFileParams { | ||
| accountId: string; | ||
| apiKey: string; | ||
| querystring: { | ||
| filePath: string; | ||
| }; | ||
| } | ||
|
|
||
| const PINECONE_INDEX_NAME = process.env.PINECONE_INDEX_NAME ?? ''; | ||
|
|
||
| const pinecone = new Pinecone({ | ||
| apiKey: process.env.PINECONE_API_KEY ?? '', | ||
| environment: process.env.PINECONE_ENVIRONMENT ?? '', | ||
| }); | ||
|
|
||
| if (!process.env.PINECONE_INDEX_NAME) { | ||
| throw new Error('Missing Pinecone index name in .env file'); | ||
| } | ||
|
|
||
| async function deleteFile(params: DeleteFileParams) { | ||
| const baseUrl = 'https://api.bytescale.com'; | ||
| const path = `/v2/accounts/${params.accountId}/files`; | ||
| const entries = (obj: Record<string, unknown>) => | ||
| Object.entries(obj).filter(([, val]) => (val ?? null) !== null) as [ | ||
| string, | ||
| string, | ||
| ][]; | ||
| const query = entries(params.querystring ?? {}) | ||
| .flatMap(([k, v]) => (Array.isArray(v) ? v.map((v2) => [k, v2]) : [[k, v]])) | ||
| .map((kv) => kv.join('=')) | ||
| .join('&'); | ||
| const response = await fetch( | ||
| `${baseUrl}${path}${query.length > 0 ? '?' : ''}${query}`, | ||
| { | ||
| method: 'DELETE', | ||
| headers: Object.fromEntries( | ||
| entries({ | ||
| Authorization: `Bearer ${params.apiKey}`, | ||
| }) as [string, string][], | ||
| ), | ||
| }, | ||
| ); | ||
| if (Math.floor(response.status / 100) !== 2) { | ||
| const result = await response.json(); | ||
| throw new Error(`Bytescale API Error: ${JSON.stringify(result)}`); | ||
| } | ||
| } | ||
|
|
||
| export async function DELETE(request: Request) { | ||
| const { id, fileUrl } = await request.json(); | ||
|
|
||
| const { userId } = getAuth(request as any); | ||
|
|
||
| if (!userId) { | ||
| return NextResponse.json({ error: 'You must be logged in to delete data' }); | ||
| } | ||
|
|
||
| try { | ||
| const document = await prisma.document.findFirst({ | ||
| where: { | ||
| id, | ||
| userId, | ||
| }, | ||
| }); | ||
|
|
||
| if (!document) { | ||
| return NextResponse.json({ error: 'Document not found' }); | ||
| } | ||
|
|
||
| const pathWithAccId = fileUrl.replace('https://upcdn.io/', ''); | ||
| const accId = pathWithAccId.split('/')[0]; | ||
| const path = pathWithAccId.replace(`${accId}/raw`, ''); | ||
|
|
||
| deleteFile({ | ||
| accountId: accId, | ||
| apiKey: !!process.env.NEXT_SECRET_BYTESCALE_API_KEY | ||
| ? process.env.NEXT_SECRET_BYTESCALE_API_KEY | ||
| : 'no api key found', | ||
| querystring: { | ||
| filePath: path, | ||
| }, | ||
| }).then( | ||
| () => console.log('Success.'), | ||
| (error) => { | ||
| console.error(error); | ||
| return NextResponse.json({ | ||
| error: 'Could not delete document from cloud', | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| const index = pinecone.Index(PINECONE_INDEX_NAME); | ||
|
|
||
| try { | ||
| await index.namespace(id).deleteAll(); | ||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
|
|
||
| await prisma.document.delete({ | ||
| where: { | ||
| id, | ||
| }, | ||
| }); | ||
|
|
||
| return NextResponse.json({ text: 'Document deleted successfully', id }); | ||
| } catch (error) { | ||
| console.log('error', error); | ||
| return NextResponse.json({ error: 'Failed to delete your data' }); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| import React from 'react'; | ||
|
|
||
| interface TrashIconProps { | ||
| onClick: () => void; | ||
| } | ||
|
|
||
| const TrashIcon: React.FC<TrashIconProps> = ({ onClick }) => { | ||
| return ( | ||
| <> | ||
| <svg | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| width="16" | ||
| height="16" | ||
| fill="currentColor" | ||
| className="bi bi-trash hover:cursor-pointer m-2" | ||
| viewBox="0 0 16 16" | ||
| color="red" | ||
| onClick={onClick} | ||
| > | ||
| <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5m2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5m3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0z" /> | ||
| <path d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4zM2.5 3h11V2h-11z" /> | ||
| </svg> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default TrashIcon; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be a parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed the docs of bytescale to implement functionality so I just added this to resolve typescript errors. The Api key is taken from the env variables and then passed to this. Can be removed but i though of keeping the same syntax as docs.