Skip to content

Commit

Permalink
Update index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
daoauth committed Jul 17, 2024
1 parent 516c936 commit 4acad84
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,14 @@ export const upload = onRequest(async (req, res) => {
const busboy = Busboy({ headers: req.headers });
const tmpdir = os.tmpdir();
const uploads: any = {};
let uploadError = false;

busboy.on('file', (fieldname, file, { filename }) => {
const filepath = path.join(tmpdir, filename);
uploads[fieldname] = filepath;
const writeStream = fs.createWriteStream(filepath);
file.pipe(writeStream);

writeStream.on('error', (err) => {
functions.logger.error('Error writing file:', err);
res.status(500).send('Error writing file');
uploadError = true;
});
file.pipe(fs.createWriteStream(filepath));
});

busboy.on('finish', async () => {
if (uploadError) {
return;
}

const bucket = storage.bucket('slsa-on-blockchain.appspot.com');
const uploadPromises = [];

Expand All @@ -71,7 +59,7 @@ export const upload = onRequest(async (req, res) => {
await Promise.all(uploadPromises);
res.status(200).send('File uploaded successfully.');
} catch (error) {
functions.logger.error('Error uploading file:', error);
console.error('Error uploading file:', error);
res.status(500).send('Internal Server Error');
} finally {
for (const file in uploads) {
Expand All @@ -80,12 +68,7 @@ export const upload = onRequest(async (req, res) => {
}
});

busboy.on('error', (err) => {
functions.logger.error('Error parsing form:', err);
res.status(500).send('Error parsing form');
});

req.pipe(busboy);
busboy.end(req.rawBody);
});

/**
Expand All @@ -112,7 +95,7 @@ export const download = onRequest(async (req, res) => {
const [contents] = await file.download();
res.status(200).send(contents.toString('base64'));
} catch (error) {
functions.logger.error('Error downloading file:', error);
console.error('Error downloading file:', error);
res.status(500).send('Internal Server Error');
}
});
Expand Down Expand Up @@ -151,7 +134,7 @@ export const create = onRequest(async (req, res) => {

res.status(200).json({ uid });
} catch (error) {
functions.logger.error('Error saving data:', error);
console.error('Error saving data:', error);
res.status(500).send('Internal Server Error');
}
});
Expand Down Expand Up @@ -196,7 +179,7 @@ export const update = onRequest({ cors: true }, async (req, res) => {

res.status(200).json({ uid });
} catch (error) {
functions.logger.error('Error updating and moving data:', error);
console.error('Error updating and moving data:', error);
res.status(500).send('Internal Server Error');
}
});
Expand Down Expand Up @@ -228,7 +211,7 @@ const _load = async (
res.status(200).json({ name, network, project, provenance });
}
} catch (error) {
functions.logger.error(`Error reading and deleting data from ${collection}:`, error);
console.error(`Error reading and deleting data from ${collection}:`, error);
res.status(500).send('Internal Server Error');
}
};
Expand Down Expand Up @@ -288,8 +271,8 @@ export const deleteOldData = pubsub
try {
await deleteOldDataFromCollection('unsigned', cutoff);
await deleteOldDataFromCollection('signed', cutoff);
functions.logger.log('Old data deleted from both collections');
console.log('Old data deleted from both collections');
} catch (error) {
functions.logger.error('Error deleting old data:', error);
console.error('Error deleting old data:', error);
}
});

0 comments on commit 4acad84

Please sign in to comment.