<commit_before>/*
 * Copyright 2016-present, <NAME>
 * All rights reserved.
 *
 * This source code is licensed under the license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

/* jshint node: true, devel: true */
'use strict'

const bodyParser = require('body-parser')
const busboy = require('express-busboy')
const cloudinary = require('cloudinary')
const config = require('config')
const crypto = require('crypto')
const express = require('express')
const redis = require('redis')
const request = require('request')

const memer = require('./memer')

const CLOUDINARY_PUBLIC_ID_KEY = 'cloudinary_public_id'
const POSITION_TO_GRAVITY = {
  top: 'north',
  bottom: 'south'
}
const STRINGS_KEY = 'strings'

// Used to differentiate user uploads from other images
const USER_UPLOAD_TAG = 'user-upload'

var _redisClient
function redisClient () {
  if (_redisClient === null) {
    _redisClient = redis.createClient(process.env.REDISCLOUD_URL, {no_ready_check: true})
  }
  return _redisClient
}

var app = express()

app.set('port', process.env.PORT || 5000)
app.set('view engine', 'pug')
app.use(bodyParser.json({ verify: verifyRequestSignature }))
app.use('/static', express.static('public'))

busboy.extend(app, { upload: true })

/*
 * Be sure to setup your config values before running this code. You can
 * set them using environment variables or modifying the config file in /config.
 *
 */

// App Secret can be retrieved from the App Dashboard
const APP_SECRET = (process.env.MESSENGER_APP_SECRET)
  ? process.env.MESSENGER_APP_SECRET
  : config.get('appSecret')

// Arbitrary value used to validate a webhook
const VALIDATION_TOKEN = (process.env.MESSENGER_VALIDATION_TOKEN)
  ? (process.env.MESSENGER_VALIDATION_TOKEN)
  : config.get('validationToken')

// Generate a page access token for your page from the App Dashboard
const PAGE_ACCESS_TOKEN = (process.env.MESSENGER_PAGE_ACCESS_TOKEN)
  ? (process.env.MESSENGER_PAGE_ACCESS_TOKEN)
  : config.get('pageAccessToken')

if (!(APP_SECRET && VALIDATION_TOKEN && PAGE_ACCESS_TOKEN)) {
  console.error('Missing config values')
  process.exit(1)
}

app.get('/', function (req, res) {
  res.render('index', {})
})

app.get('/meme', function (req, res) {
  var image_id = req.query['image-id']
  var top_text = decodeURIComponent(req.query['top-text'])
  var bottom_text = decodeURIComponent(req.query['bottom-text'])
  if (image_id && image_id.length > 0) {
    var transformed_url = memer.getMemeUrl(
      image_id,
      top_text,
      bottom_text
    )
    res.status(200).send({
      image_url: transformed_url
    })
  } else {
    res.status(400).send({
      error: 'Missing image-id parameter'
    })
  }
})

app.post('/upload-image', function (req, res) {
  var image_id = req.body['image-id']
  console.log('Image ID: %s', image_id)
  cloudinary.v2.uploader.upload(
    req.files.image.file,
    {
      public_id: image_id
    },
    function (error, result) {
      var cloudinary_url = result.secure_url
      if (cloudinary_url !== null) {
        console.log('Uploaded image with public ID ' +
          result.public_id +
          ', URL: ' + result.url)
      } else {
        console.error('[ERROR] On uploading file: %s', error)
      }
      res.redirect('/')
    }
  )
})

/*
 * Use your own validation token. Check that the token used in the Webhook
 * setup is the same token used here.
 *
 */
app.get('/webhook', function (req, res) {
  if (req.query['hub.mode'] === 'subscribe' &&
      req.query['hub.verify_token'] === VALIDATION_TOKEN) {
    console.log('Validating webhook')
    res.status(200).send(req.query['hub.challenge'])
  } else {
    console.error('Failed validation. Make sure the validation tokens match.')
    res.sendStatus(403)
  }
})<commit_msg>Invalidate CDN on image upload<commit_after>/*
 * Copyright 2016-present, <NAME>
 * All rights reserved.
 *
 * This source code is licensed under the license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

/* jshint node: true, devel: true */
'use strict'

const bodyParser = require('body-parser')
const busboy = require('express-busboy')
const cloudinary = require('cloudinary')
const config = require('config')
const crypto = require('crypto')
const express = require('express')
const redis = require('redis')
const request = require('request')

const memer = require('./memer')

const CLOUDINARY_PUBLIC_ID_KEY = 'cloudinary_public_id'
const POSITION_TO_GRAVITY = {
  top: 'north',
  bottom: 'south'
}
const STRINGS_KEY = 'strings'

// Used to differentiate user uploads from other images
const USER_UPLOAD_TAG = 'user-upload'

var _redisClient
function redisClient () {
  if (_redisClient === null) {
    _redisClient = redis.createClient(process.env.REDISCLOUD_URL, {no_ready_check: true})
  }
  return _redisClient
}

var app = express()

app.set('port', process.env.PORT || 5000)
app.set('view engine', 'pug')
app.use(bodyParser.json({ verify: verifyRequestSignature }))
app.use('/static', express.static('public'))

busboy.extend(app, { upload: true })

/*
 * Be sure to setup your config values before running this code. You can
 * set them using environment variables or modifying the config file in /config.
 *
 */

// App Secret can be retrieved from the App Dashboard
const APP_SECRET = (process.env.MESSENGER_APP_SECRET)
  ? process.env.MESSENGER_APP_SECRET
  : config.get('appSecret')

// Arbitrary value used to validate a webhook
const VALIDATION_TOKEN = (process.env.MESSENGER_VALIDATION_TOKEN)
  ? (process.env.MESSENGER_VALIDATION_TOKEN)
  : config.get('validationToken')

// Generate a page access token for your page from the App Dashboard
const PAGE_ACCESS_TOKEN = (process.env.MESSENGER_PAGE_ACCESS_TOKEN)
  ? (process.env.MESSENGER_PAGE_ACCESS_TOKEN)
  : config.get('pageAccessToken')

if (!(APP_SECRET && VALIDATION_TOKEN && PAGE_ACCESS_TOKEN)) {
  console.error('Missing config values')
  process.exit(1)
}

app.get('/', function (req, res) {
  res.render('index', {})
})

app.get('/meme', function (req, res) {
  var image_id = req.query['image-id']
  var top_text = decodeURIComponent(req.query['top-text'])
  var bottom_text = decodeURIComponent(req.query['bottom-text'])
  if (image_id && image_id.length > 0) {
    var transformed_url = memer.getMemeUrl(
      image_id,
      top_text,
      bottom_text
    )
    res.status(200).send({
      image_url: transformed_url
    })
  } else {
    res.status(400).send({
      error: 'Missing image-id parameter'
    })
  }
})

app.post('/upload-image', function (req, res) {
  var image_id = req.body['image-id']
  console.log('Image ID: %s', image_id)
  cloudinary.v2.uploader.upload(
    req.files.image.file,
    {
      public_id: image_id,
      invalidate: true
    },
    function (error, result) {
      var cloudinary_url = result.secure_url
      if (cloudinary_url !== null) {
        console.log('Uploaded image with public ID ' +
          result.public_id +
          ', URL: ' + result.url)
      } else {
        console.error('[ERROR] On uploading file: %s', error)
      }
      res.redirect('/')
    }
  )
})

/*
 * Use your own validation token. Check that the token used in the Webhook
 * setup is the same token used here.
 *
 */
app.get('/webhook', function (req, res) {
  if (req.query['hub.mode'] === 'subscribe' &&
      req.query['hub.verify_token'] === VALIDATION_TOKEN) {
    console.log('Validating webhook')
    res.status(200).send(req.query['hub.challenge'])
  } else {
    console.error('Failed validation. Make sure the validation tokens match.')
    res.sendStatus(403)
  }
})
{'fraction_non_alphanumeric': 0.13363146126529002, 'fraction_numerical': 0.0060502433250032885, 'mean_word_length': 3.779384035197989, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '990092', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2493, 'n_words': 626}