Skip to content

Instantly share code, notes, and snippets.

@kkrishnan90
Last active October 23, 2021 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkrishnan90/aeb0bf123a10ab8c18113928e55a44cd to your computer and use it in GitHub Desktop.
Save kkrishnan90/aeb0bf123a10ab8c18113928e55a44cd to your computer and use it in GitHub Desktop.
'use strict';
const process = require('process'); // Required to mock environment variables
// [START gae_flex_storage_app]
const {format} = require('util');
const express = require('express');
const Multer = require('multer');
const axios = require('axios');
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GOOGLE_CLOUD_PROJECT environment variable. See
// https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/docs/authentication.md
// These environment variables are set automatically on Google App Engine
const {Storage} = require('@google-cloud/storage');
// Instantiate a storage client
const storage = new Storage();
const app = express();
app.set('view engine', 'pug');
// This middleware is available in Express v4.16.0 onwards
app.use(express.json());
// Multer is required to process file uploads and make them available via
// req.files.
const multer = Multer({
storage: Multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
},
});
// A bucket is a container for objects (files).
const bucket = storage.bucket(process.env.GCLOUD_STORAGE_BUCKET);
// Display a form for uploading files.
app.get('/', (req, res) => {
res.render('form.pug');
});
// Process the file upload and upload to Google Cloud Storage.
app.post('/upload', multer.single('file'), (req, res, next) => {
if (!req.file) {
res.status(400).send('No file uploaded.');
return;
}
// Create a new blob in the bucket and upload the file data.
const blob = bucket.file(req.file.originalname);
const blobStream = blob.createWriteStream();
blobStream.on('error', err => {
next(err);
});
blobStream.on('finish', () => {
// The public URL can be used to directly access the file via HTTP.
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${blob.name}`
);
axios.post("https://us-central1-arboreal-vision-329711.cloudfunctions.net/jijo-ocrdemo",{"image":`gs://${bucket.name}/${blob.name}`}).then(result=>{res.render('output.pug',{content:JSON.stringify(result.data.result),imagepath:publicUrl})});
//res.status(200).send(publicUrl);
});
blobStream.end(req.file.buffer);
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
// [END gae_flex_storage_app]
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment