Created
October 3, 2024 10:52
-
-
Save goranefbl/dcfb417813b3195090f13be652d449c8 to your computer and use it in GitHub Desktop.
Accept form file from webflow, upload to google drive and save to notion. Put file under app/api/upload/route.js
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
import { google } from "googleapis"; | |
import { Readable } from "stream"; | |
import { NextResponse } from "next/server"; | |
import { Client } from "@notionhq/client"; | |
let auth; | |
let drive; | |
const notion = new Client({ auth: process.env.NOTION_API_KEY }); | |
const NOTION_DATABASE_ID = process.env.NOTION_DATABASE_ID; | |
const PARENT_FOLDER_ID = process.env.GOOGLE_DRIVE_FOLDER_ID; | |
try { | |
auth = new google.auth.GoogleAuth({ | |
credentials: JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS), | |
scopes: ["https://www.googleapis.com/auth/drive.file"], | |
}); | |
drive = google.drive({ version: "v3", auth }); | |
} catch (error) { | |
console.error("Error initializing Google Drive API:", error); | |
} | |
async function findOrCreateFolder(folderName) { | |
try { | |
// First, try to find an existing folder | |
const response = await drive.files.list({ | |
q: `mimeType='application/vnd.google-apps.folder' and name='${folderName}' and '${PARENT_FOLDER_ID}' in parents and trashed=false`, | |
fields: "files(id, name)", | |
spaces: "drive", | |
}); | |
if (response.data.files.length > 0) { | |
// Folder already exists, return its ID | |
console.log("Existing folder found:", folderName); | |
return response.data.files[0].id; | |
} else { | |
// Folder doesn't exist, create a new one | |
console.log("Creating new folder:", folderName); | |
const folderResponse = await drive.files.create({ | |
requestBody: { | |
name: folderName, | |
mimeType: "application/vnd.google-apps.folder", | |
parents: [PARENT_FOLDER_ID], | |
}, | |
fields: "id", | |
}); | |
return folderResponse.data.id; | |
} | |
} catch (error) { | |
console.error("Error finding or creating folder:", error); | |
throw error; | |
} | |
} | |
async function findNotionPageByCompanyName(companyName) { | |
try { | |
const response = await notion.databases.query({ | |
database_id: NOTION_DATABASE_ID, | |
filter: { | |
property: "Company Name", | |
title: { | |
equals: companyName, | |
}, | |
}, | |
}); | |
if (response.results.length > 0) { | |
return response.results[0].id; | |
} | |
return null; | |
} catch (error) { | |
console.error("Error finding Notion page:", error); | |
throw error; | |
} | |
} | |
async function updateNotionPage(pageId, fileUrl) { | |
try { | |
await notion.pages.update({ | |
page_id: pageId, | |
properties: { | |
"Written Submission": { | |
url: fileUrl, | |
}, | |
}, | |
}); | |
console.log("Updated Notion page:", pageId); | |
} catch (error) { | |
console.error("Error updating Notion page:", error); | |
throw error; | |
} | |
} | |
export async function POST(request) { | |
if (!auth || !drive) { | |
return NextResponse.json( | |
{ error: "Google Drive API not initialized properly" }, | |
{ status: 500 } | |
); | |
} | |
try { | |
const formData = await request.formData(); | |
const file = formData.get("file"); | |
const companyName = formData.get("companyName"); | |
if (!file || !companyName) { | |
return NextResponse.json( | |
{ error: "File or company name missing" }, | |
{ status: 400 } | |
); | |
} | |
// Find the Notion page for this company | |
const notionPageId = await findNotionPageByCompanyName(companyName); | |
if (!notionPageId) { | |
return NextResponse.json( | |
{ error: "Company not found in Notion database" }, | |
{ status: 404 } | |
); | |
} | |
console.log("Finding or creating folder for company:", companyName); | |
const folderId = await findOrCreateFolder(companyName); | |
console.log("Uploading file:", file.name); | |
const fileBuffer = await file.arrayBuffer(); | |
const fileStream = Readable.from(Buffer.from(fileBuffer)); | |
const response = await drive.files.create({ | |
requestBody: { | |
name: file.name, | |
mimeType: file.type, | |
parents: [folderId], | |
}, | |
media: { | |
mimeType: file.type, | |
body: fileStream, | |
}, | |
fields: "id, webViewLink", | |
}); | |
console.log("File uploaded, ID:", response.data.id); | |
await drive.permissions.create({ | |
fileId: response.data.id, | |
requestBody: { | |
role: "reader", | |
type: "anyone", | |
}, | |
}); | |
console.log("File permissions set to public"); | |
// Update Notion page with the Google Drive link | |
await updateNotionPage(notionPageId, response.data.webViewLink); | |
return NextResponse.json({ | |
message: "File uploaded successfully and Notion updated", | |
url: response.data.webViewLink, | |
companyName: companyName, | |
folderId: folderId, | |
notionPageId: notionPageId, | |
}); | |
} catch (error) { | |
console.error("Error in file upload or Notion update:", error); | |
return NextResponse.json( | |
{ error: "Error processing request: " + error.message }, | |
{ status: 500 } | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment