Skip to content

Commit

Permalink
Added connection code that works
Browse files Browse the repository at this point in the history
  • Loading branch information
JuCJeff committed Oct 20, 2019
1 parent 8a18bc1 commit eb5bf75
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 71 deletions.
39 changes: 32 additions & 7 deletions components/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class MyCamera extends React.Component {
visible: false,
confirmLoading: false,
type: Camera.Constants.Type.back,
url: "http://localhost:3000/upload"
url: "https://garbage-classi.appspot.com/imageUpload"
};

showModal = () => {
Expand Down Expand Up @@ -58,6 +58,29 @@ export default class MyCamera extends React.Component {
);
}

base64ToBlob(base64, mime)
{
mime = mime || '';
var sliceSize = 1024;
var byteChars = window.atob(base64);
var byteArrays = [];

for (var offset = 0, len = byteChars.length; offset < len; offset += sliceSize) {
var slice = byteChars.slice(offset, offset + sliceSize);

var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}

var byteArray = new Uint8Array(byteNumbers);

byteArrays.push(byteArray);
}

return new Blob(byteArrays, {type: mime});
}

async snapPhoto() {
if (this.camera) {
const options = {
Expand All @@ -66,17 +89,19 @@ export default class MyCamera extends React.Component {
};
await this.camera.takePictureAsync(options).then(photo => {
// photo.exif.Orientation = 1;

console.log(photo);
// this.showPopUpModel();
axios.post('localhost:3000/image_upload', {
photo
axios.post('https://garbage-classi.appspot.com/imageUpload', {
"base64": photo.base64
})
.then((res) => {
console.log(`statusCode: ${res.statusCode}`)
console.log(res)
console.log(`statusCode: ${res.statusCode}`);
console.log(res);
// Do our pop up modal here
})
.catch((error) => {
console.error(error)
console.error(error);
})

return true;
Expand Down
46 changes: 46 additions & 0 deletions components/ImageUpload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const storage = require('@google-cloud/storage');
const fs = require('fs')

const gcs = storage({
projectId: 'your-project-id',
keyFilename: '/path/to/keyfile.json'
});

const bucketName = 'bucket-name-for-upload'
const bucket = gcs.bucket(bucketName);

function getPublicUrl(filename) {
return 'https://storage.googleapis.com/' + bucketName + '/' + filename;
}

let ImgUpload = {};

ImgUpload.uploadToGcs = (req, res, next) => {
if(!req.file) return next();

// Can optionally add a path to the gcsname below by concatenating it before the filename
const gcsname = req.file.originalname;
const file = bucket.file(gcsname);

const stream = file.createWriteStream({
metadata: {
contentType: req.file.mimetype
}
});

stream.on('error', (err) => {
req.file.cloudStorageError = err;
next(err);
});

stream.on('finish', () => {
req.file.cloudStorageObject = gcsname;
req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
next();
});

stream.end(req.file.buffer);
}

module.exports = ImgUpload;
Loading

0 comments on commit eb5bf75

Please sign in to comment.