Sat Oct 27 2018
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
const express = require('express')
const app = express()
// --------------------------------------------


const shortid = require('shortid')
let Datastore = require('@google-cloud/datastore')
var bodyParser = require('body-parser')
const cors = require('cors')
const ds = Datastore()

//app.use(bodyParser.json({ type: 'application/*+json' }))
app.use(bodyParser.urlencoded())
app.use(cors())



/**
 * Saves a paste into datastore. A paste is an array of objects like
 * {title : 'paste title', content: 'paste content/code' }
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */


exports.savePaste = async(req, res) => {
  res.set('Access-Control-Allow-Origin', '*');
  if (req.method === 'OPTIONS') {
    // Send response to OPTIONS requests
    res.set('Access-Control-Allow-Methods', '*');
    res.set('Access-Control-Allow-Headers', 'Content-Type');
    res.set('Access-Control-Max-Age', '3600');
    res.status(204).send('');
  } else {
    
  

    let id = shortid.generate()    
    let email = null
    let ip = null
    let privacy = req.body.privacy
    let paste = req.body.paste || []
    let userId = null
    let username = null

  
    res.set('Access-Control-Allow-Origin', "*")
    
    // Validate privacy
    if (!privacy || (privacy && !/^public$|^me$|^link$/.test(privacy)))
    {
      privacy = 'public'
    }    

    console.log(req.body)

    // Validate paste. Paste must:
    // 1. Be an array that is not empty
    // 2. Each itewm in array must be obj
    // 3. Each obj must have a title and content key
    // 4. If title is empty, default to 'untitled'
    // 5. If content is empty, delete entry from paste
    if (Array.isArray(paste))
    {
      if (!paste.length)
      {
        res.send({ result: 0, msg: `Failed. ${req.get('content-type')}`})
        return
      }
      
      paste.map(entry => {
        entry.title = entry.title.trim()
        
        if (!entry.content)
        {        
          return null
        }
        if (!entry.title)
        {
          entry.title = 'untitled'
        }
        return entry
      })      
    }
    
    // Note that index exclusion for arrays (ie, paste) is cloudy.
    // https://github.com/googleapis/nodejs-datastore/issues/14

    let pasteEntity  = {
      key: ds.key(['Paste', id]),
      data: {
        date: Date.now(),
        email: email || null,
        username: username || null,
        ip: null,
        privacy: privacy || 'public',
        paste: paste,
        userId: userId || null
      },
      excludeFromIndexes: [
        'ip',
        'privacy',
        'paste',
        'paste[]',
        'paste[].content',
        'paste[].title',
        'userId'
      ]
    }
    
      
    try
    {
      await ds.insert(pasteEntity)
      res.send({ result: 1, id: id})
    }
    catch (e)
    {
      res.send({ result: 0, msg: 'Failed.'})
    }
  }
}