What I am trying to do is upload a file from the front end to my GCP workflow, and it properly does this, however I keep encountering the API resolved without sending a response
issue. I tried placing the form.parse()
inside a Promise
, however that also does not effectively return a response for me. The part that’s even more confusing for me is I console log the response on the front end, and it properly shows the response if it’s succesful. Below is my current code:
export const config = {
api: {
bodyParser: false
}
}
export default async function handler(req, res) {
const { query: { uuid } } = req
const token = await getToken({ req })
const key = JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS.toString())
if (!token) {
return res.status(401).json({ error: 'User must be logged in to perform this action' })
}
const storage = new Storage({
projectId: process.env.PROJECT_ID,
credentials: {
client_email: key.client_email,
private_key: key.private_key.replace(/\n/g, 'n')
}
})
const form = formidable({
keepExtensions: true,
})
form.parse(req, async (error, fields, files) => {
if (error) {
console.error(`Error parsing form: ${error}`);
return res.status(500).json({ error: 'Error parsing form' });
}
const selectedFile = files.file[0];
if (!selectedFile) {
console.error('No file uploaded')
return res.status(400).json({ error: 'No file uploaded' })
}
const salt = crypto.randomBytes(16).toString('hex')
const hashedFilename = crypto.createHash('sha256').update(selectedFile.originalFilename + salt).digest('hex')
const bucket = storage.bucket(process.env.CLOUD_STORAGE_BUCKET_NAME)
const blob = bucket.file(hashedFilename)
const blobStream = createReadStream(selectedFile.filepath)
.pipe(blob.createWriteStream({
resumable: false,
contentType: selectedFile.mimetype
}))
blobStream.on('error', (error) => {
console.error(`Error uploading files to cloud storage: ${error}`)
cleanupStreams(blobStream, selectedFile.filepath)
return res.status(500).json({ error: 'Error uploading files to cloud storage' })
})
blobStream.on('finish', async () => {
try {
await blob.setMetadata({
metadata: {
UUID: uuid,
FILE_NAME: selectedFile.originalFilename,
CREATION_DATE: getDate(),
}
})
cleanupStreams(blobStream, selectedFile.filepath)
return res.status(201).json({ message: `Uploaded the file successfully: ${selectedFile.newFilename}` })
} catch (error) {
console.error(`Error setting metadata: ${error}`)
cleanupStreams(blobStream, selectedFile.filepath)
return res.status(500).json({ error: 'Error setting metadata' })
}
})
})
form.on('error', (error) => {
console.error(`Form parsing error: ${error}`)
return res.status(500).json({ error: 'Error processing form data' })
})
}