-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first commit * Created google drive upload feature * Update route.ts * Merged with changes in main and updated folder location to sistema email * Updated package-lock * Fixed ESLint issues * Fixed ESLint issues * fixed type errors * Fixed type errors * Updated .env.sample * Testing API Failure * implemented type safety with env variables * Updated secret names to preserve functionality * Added Firebase Files * Added Github Actions * Testing preview * Testing cache control * Testing cache control after lint :( * We love Cache Control * update package lock * Resolved all Issues in Code Review, awaiting approval * deleted env d ts * Testing with env d ts * fixed lint issues * Update .env.sample Co-authored-by: Chinemerem <[email protected]> * Resolved issues * Deleted lines * reverted files to main * reverted files to main * Removed client id because apparently it wasn't necessary, might bite us in the rear end later * Getting rid of unnecessary changes to main --------- Co-authored-by: Chinemerem <[email protected]>
- Loading branch information
1 parent
c676837
commit b88f409
Showing
9 changed files
with
659 additions
and
27 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,52 @@ | ||
import { google } from 'googleapis'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
export async function DELETE(req: NextRequest) { | ||
const { searchParams } = new URL(req.url); | ||
const fileId = searchParams.get('fileId'); | ||
|
||
if (!fileId) { | ||
return NextResponse.json( | ||
{ message: 'File ID is required' }, | ||
{ status: 400 } | ||
); | ||
} | ||
try { | ||
const private_key = process.env.GDRIVE_PRIVATE_KEY?.replace(/\\n/g, '\n'); | ||
|
||
if (!private_key) { | ||
return NextResponse.json( | ||
{ error: 'Missing Google Drive private key' }, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
const auth = new google.auth.GoogleAuth({ | ||
projectId: process.env.GDRIVE_PROJECT_ID, | ||
scopes: 'https://www.googleapis.com/auth/drive', | ||
credentials: { | ||
type: 'service_account', | ||
client_email: process.env.GDRIVE_CLIENT_EMAIL, | ||
private_key: private_key, | ||
}, | ||
}); | ||
|
||
const drive = google.drive({ version: 'v3', auth }); | ||
|
||
// Delete the file | ||
await drive.files.delete({ | ||
fileId: fileId, | ||
}); | ||
|
||
return NextResponse.json( | ||
{ message: 'File deleted successfully' }, | ||
{ status: 200 } | ||
); | ||
} catch (error) { | ||
console.error('Error deleting file:', error); | ||
return NextResponse.json( | ||
{ message: 'Error deleting file', error: (error as Error).message }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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,53 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { google } from 'googleapis'; | ||
|
||
export async function GET(req: NextRequest) { | ||
const newHeaders = new Headers(req.headers); | ||
newHeaders.set( | ||
'Cache-Control', | ||
'no-cache, no-store, max-age=0, must-revalidate' | ||
); | ||
|
||
const private_key = process.env.GDRIVE_PRIVATE_KEY?.replace(/\\n/g, '\n'); | ||
|
||
if (!private_key) { | ||
return NextResponse.json( | ||
{ error: 'Missing Google Drive private key' }, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
const auth = new google.auth.GoogleAuth({ | ||
projectId: process.env.GDRIVE_PROJECTID, | ||
scopes: 'https://www.googleapis.com/auth/drive', | ||
credentials: { | ||
type: 'service_account', | ||
client_email: process.env.GDRIVE_CLIENT_EMAIL, | ||
private_key: private_key, | ||
}, | ||
}); | ||
const drive = google.drive({ version: 'v3', auth }); | ||
|
||
try { | ||
const res = await drive.files.list({ | ||
q: "'1schkzvm_b46UGovHpQ2uH-X-nJtlm32_' in parents", | ||
fields: 'nextPageToken, files(id, name)', | ||
spaces: 'drive', | ||
}); | ||
|
||
const files = res.data.files; | ||
return NextResponse.json( | ||
{ results: { files } }, | ||
{ | ||
status: 200, | ||
headers: newHeaders, | ||
} | ||
); | ||
} catch (err) { | ||
console.error('Error querying Google Drive:', err); | ||
return NextResponse.json( | ||
{ error: 'Internal Server Error' }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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,79 @@ | ||
import { google } from 'googleapis'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { Readable } from 'node:stream'; | ||
|
||
export async function POST(req: NextRequest) { | ||
const formData = await req.formData(); | ||
|
||
const file: File | null = formData.get('file') as File | null; | ||
let fileBuffer: Buffer; | ||
|
||
if (file instanceof File) { | ||
const arrayBuffer = await file.arrayBuffer(); | ||
fileBuffer = Buffer.from(arrayBuffer); | ||
} else { | ||
console.error('No file was provided in the form data'); | ||
return NextResponse.json( | ||
{ error: 'No file was provided' }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const filename: string | null = formData.get('fileName') as string | null; | ||
|
||
if (!filename) { | ||
console.error('No filename was provided in the form data'); | ||
return NextResponse.json( | ||
{ error: 'No filename was provided' }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const private_key = process.env.GDRIVE_PRIVATE_KEY?.replace(/\\n/g, '\n'); | ||
|
||
if (!private_key) { | ||
return NextResponse.json( | ||
{ error: 'Missing Google Drive private key' }, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
const auth = new google.auth.GoogleAuth({ | ||
projectId: process.env.GDRIVE_PROJECTID, | ||
scopes: 'https://www.googleapis.com/auth/drive', | ||
credentials: { | ||
type: 'service_account', | ||
client_email: process.env.GDRIVE_CLIENT_EMAIL, | ||
private_key: private_key, | ||
}, | ||
}); | ||
|
||
const drive = google.drive({ version: 'v3', auth }); | ||
|
||
try { | ||
await drive.files.create({ | ||
requestBody: { | ||
name: filename, | ||
mimeType: 'application/pdf', | ||
parents: ['1schkzvm_b46UGovHpQ2uH-X-nJtlm32_'], | ||
driveId: '1schkzvm_b46UGovHpQ2uH-X-nJtlm32_', | ||
}, | ||
media: { | ||
mimeType: 'application/pdf', | ||
body: Readable.from(fileBuffer), | ||
}, | ||
supportsAllDrives: true, | ||
}); | ||
|
||
return NextResponse.json( | ||
{ message: 'File uploaded successfully' }, | ||
{ status: 200 } | ||
); | ||
} catch (error) { | ||
console.error('Error uploading file:', error); | ||
return NextResponse.json( | ||
{ message: 'Error uploading file', error: (error as Error).message }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
Oops, something went wrong.