// Import required Wix modules
import { mediaManager } from 'wix-media-backend';
import { googleSheets } from 'wix-google-sheets-backend'; // Custom module for Google Sheets API
// Google Sheets configuration
const SHEET_ID = 'your-google-sheet-id';
const SHEET_NAME = 'Sheet1'; // Replace with your sheet name
const GOOGLE_API_KEY = 'your-google-api-key'; // Use a service account key
// Function to handle file upload and update Google Sheet
export async function handleFileUpload(file) {
try {
// Step 1: Upload file to Wix Media Manager
const uploadResult = await mediaManager.upload(file, {
mediaOptions: {
mediaType: 'document',
},
});
// Get the public URL of the uploaded file
const fileUrl = uploadResult.fileUrl;
// Step 2: Append the file URL to Google Sheet
const appendResult = await googleSheets.appendRow(SHEET_ID, SHEET_NAME, [file.name, fileUrl]);
return {
success: true,
message: 'File uploaded and URL added to Google Sheet successfully.',
fileUrl,
};
} catch (error) {
console.error('Error handling file upload:', error);
return {
success: false,
message: 'Failed to upload file or update Google Sheet.',
};
}
}
// Function to handle file upload and update Google Sheet
export async function handleFileUpload(file) {
try {
// Upload file to Wix Media Manager
const uploadResult = await mediaManager.upload(file);
const fileUrl = uploadResult.fileUrl;
// Authenticate with Google Sheets API
const auth = new google.auth.GoogleAuth({
keyFile: 'path/to/your/credentials.json', // Use Secrets Manager to store this securely
scopes: ['https://www.googleapis.com/auth/spreadsheets']
});
const sheets = google.sheets({ version: 'v4', auth });
// Define the spreadsheet ID and range
const spreadsheetId = 'your-spreadsheet-id';
const range = 'Sheet1!A1';
// Update the Google Sheet with the file URL
await sheets.spreadsheets.values.update({
spreadsheetId,
range,
valueInputOption: 'RAW',
requestBody: {
values: [[fileUrl]]
}
});
return { success: true, fileUrl };
} catch (error) {
console.error('Error uploading file or updating Google Sheet:', error);
return { success: false, error: error.message };
}
}