Tweet-311-Denver / tweet_311_denver_service

Backend for Tweet311Denver Turing Project
MIT License
1 stars 0 forks source link

Image upload from the app stores an image on the backend #9

Open ap2322 opened 4 years ago

ap2322 commented 4 years ago

Image upload from the frontend should store the image on the backend servers for future upload to twitter & 311.

https://hapi.dev/tutorials/servingfiles/?lang=en_US

https://medium.com/today-i-learned-chai/how-to-upload-files-with-node-js-hapi-js-c788b5c8586a

ap2322 commented 4 years ago

Background, none very helpful:

https://scotch.io/bar-talk/handling-file-uploads-with-hapi-js

https://medium.com/today-i-learned-chai/how-to-upload-files-with-node-js-hapi-js-c788b5c8586a

https://www.thepolyglotdeveloper.com/2017/11/process-file-uploads-nodejs-hapi-framework/

https://futurestud.io/tutorials/hapi-how-to-upload-files

ap2322 commented 4 years ago

https://gist.github.com/joyrexus/0c6bd5135d7edeba7b87 -> more helpful, still getting error:

{
    "statusCode": 415,
    "error": "Unsupported Media Type",
    "message": "Unsupported Media Type"
}
ap2322 commented 4 years ago

CONFIG PARSE FALSE: https://github.com/outmoded/discuss/issues/543

ap2322 commented 4 years ago

Maybe use multer instead??? https://medium.com/@nitinpatel_20236/image-upload-via-nodejs-server-3fe7d3faa642

There are a LOT more resources on using multer.

ap2322 commented 4 years ago

Concepts to explore: read/write stream (am able to get unparsed stream) https://stackoverflow.com/questions/2496710/writing-files-in-node-js

First console.log gives me a stream (how to use?). The second console.log is undefined.

server.route({
  path: '/upload',
  method: 'POST',
  options: {
    payload: {
      output: 'stream',
      allow: "multipart/form-data",
      parse: false,
      maxBytes: 200 * 1000 * 1000
    }
  },
  handler: async (req, h) => {
    const { payload } = req
    console.log('payload', payload)
    console.log('payload.file', payload.file)
    const response = handleFileUpload(payload.file)
    return response
  }
})

Also, with the buffer method:

// kind of works!!! Need to find out how to read buffers
server.route({
  method: 'POST',
  path: '/upload-data',
  config: {
    handler: async (request, reply) => {
      const { payload } = request
      console.log(payload)
      const response = handleFileUpload(payload)
      return response
    },
    payload: {
      output: 'data',
      allow: "multipart/form-data",
      parse: false
    }
  }
})
ap2322 commented 4 years ago

Streams: https://nodesource.com/blog/understanding-streams-in-nodejs/

https://devhints.io/nodejs-stream