Skip to content
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

David/file upload #13

Merged
merged 45 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
48a0f35
first commit
ludavidca Jun 13, 2024
57fef1a
Created google drive upload feature
ludavidca Jun 22, 2024
125ae88
Update route.ts
ludavidca Jun 27, 2024
164abac
Completed CRUD functionality for Google Drive api
ludavidca Jun 28, 2024
ad24a10
Merged with changes in main and updated folder location to sistema email
ludavidca Jun 28, 2024
50c7831
Updated package-lock
ludavidca Jun 29, 2024
86d3e4a
Updated package-lock
ludavidca Jun 29, 2024
0aa25b1
Merge branch 'main' into david/file-upload
ludavidca Jun 29, 2024
62de758
Fixed ESLint issues
ludavidca Jun 29, 2024
a7bb015
Fixed ESLint issues
ludavidca Jun 29, 2024
0a82e88
Fixed ESLint issues
ludavidca Jun 29, 2024
15098b6
fixed type errors
ludavidca Jun 29, 2024
5210612
Fixed type errors
ludavidca Jun 29, 2024
c4199e6
Updated .env.sample
ludavidca Jun 29, 2024
5705a7d
Testing API Failure
ludavidca Jun 29, 2024
1f119db
implemented type safety with env variables
ludavidca Jul 4, 2024
e9e1d8f
Updated secret names to preserve functionality
ludavidca Jul 11, 2024
83019c7
Added Firebase Files
ludavidca Jul 11, 2024
4545be3
Added Github Actions
ludavidca Jul 11, 2024
3b23926
Testing preview
ludavidca Jul 22, 2024
d1e7b98
Testing cache control
ludavidca Jul 23, 2024
b3c77ca
Testing cache control after lint :(
ludavidca Jul 23, 2024
429e361
We love Cache Control
ludavidca Jul 23, 2024
be1c426
update package lock
ludavidca Jul 27, 2024
4b702f8
Merge branch 'main' into david/file-upload
ludavidca Jul 27, 2024
cbafa8f
Resolved all Issues in Code Review, awaiting approval
ludavidca Sep 20, 2024
8a68e8a
Resolved merge issues
ludavidca Sep 20, 2024
ddf299b
deleted env d ts
ludavidca Sep 20, 2024
fc6eb83
Testing with env d ts
ludavidca Sep 20, 2024
060ff4e
Merge branch 'main' into david/file-upload
ludavidca Sep 20, 2024
57806de
fixed lint issues
ludavidca Sep 20, 2024
1132d1b
Merge branch 'david/file-upload' of https://github.com/uwblueprint/si…
ludavidca Sep 20, 2024
92f8649
Merge branch 'main' into david/file-upload
ludavidca Sep 22, 2024
fc36f45
Merge branch 'main' into david/file-upload
ludavidca Sep 24, 2024
7cd2f1a
Merge branch 'main' of https://github.com/uwblueprint/sistema into da…
ludavidca Oct 2, 2024
e54b4ee
Update .env.sample
ludavidca Oct 2, 2024
f6f96c4
Resolved issues
ludavidca Oct 2, 2024
93c8f79
Merge branch 'david/file-upload' of https://github.com/uwblueprint/si…
ludavidca Oct 2, 2024
c82511a
Deleted lines
ludavidca Oct 2, 2024
20f94c7
reverted files to main
ludavidca Oct 2, 2024
cdf34c3
reverted files to main
ludavidca Oct 2, 2024
25bb8e5
Merge branch 'main' of https://github.com/uwblueprint/sistema into da…
ludavidca Oct 6, 2024
e8f3d2d
Removed client id because apparently it wasn't necessary, might bite …
ludavidca Oct 6, 2024
bb2ba03
Merge branch 'david/file-upload' of https://github.com/uwblueprint/si…
ludavidca Oct 6, 2024
11197b0
Getting rid of unnecessary changes to main
ludavidca Oct 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .env.sample
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ EMAIL_PASS=
EMAIL_SERVICE=
EMAIL_USER=
GDRIVE_CLIENT_EMAIL=
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
GDRIVE_CLIENT_ID=
GDRIVE_PROJECT_ID=
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
GDRIVE_PRIVATE_KEY=
GDRIVE_PROJECT_ID=
POSTGRES_DATABASE=
POSTGRES_PASSWORD=
POSTGRES_PORT=
POSTGRES_URL_NON_POOLING=
POSTGRES_USER=
POSTGRES_USER=
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
VERCEL=
12 changes: 3 additions & 9 deletions .eslintrc.json
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
"extends": [
"next",
"next/core-web-vitals",
"plugin:prettier/recommended"
],
"plugins": [
"prettier"
],
"extends": ["next", "next/core-web-vitals", "plugin:prettier/recommended"],
"plugins": ["prettier"],
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
"rules": {
"prettier/prettier": [
"error",
Expand All @@ -16,4 +10,4 @@
],
"no-unused-vars": "error"
}
}
}
53 changes: 53 additions & 0 deletions app/api/deleteFile/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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_id: process.env.GDRIVE_PROJECT_ID,
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
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 }
);
}
}
54 changes: 54 additions & 0 deletions app/api/searchDrive/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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) {
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
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_id: process.env.GDRIVE_PROJECT_ID,
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
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 }
);
}
}
80 changes: 80 additions & 0 deletions app/api/uploadFile/route.ts
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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,
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
scopes: 'https://www.googleapis.com/auth/drive',
credentials: {
type: 'service_account',
client_id: process.env.GDRIVE_PROJECT_ID,
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
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_'],
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
driveId: '1schkzvm_b46UGovHpQ2uH-X-nJtlm32_',
},
media: {
mimeType: 'application/pdf',
body: Readable.from(fileBuffer),
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
},
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 }
);
}
}
2 changes: 1 addition & 1 deletion docker-compose.yml
ludavidca marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ services:
- ./db-init:/docker-entrypoint-initdb.d

volumes:
postgres_data:
postgres_data:
Loading
Loading