Skip to content

Instantly share code, notes, and snippets.

View taylorstine's full-sized avatar

Taylor Stine taylorstine

  • Carnegie Mellon
  • Pittsburgh
View GitHub Profile
@taylorstine
taylorstine / schema-right.js
Created March 15, 2017 06:59
Don't buffer those schemas...
const mySchema = new Schema({
//...
}
}, {bufferCommands: false});
@taylorstine
taylorstine / mongo-connect.js
Last active March 15, 2017 06:53
The right way to connect to mongo
mongoose.connect("mongo://yourdatabaseURI:4321", {server: {reconnectTries: Number.MAX_SAFE_INTEGER}})
@taylorstine
taylorstine / operator-middleware-extra.js
Created March 11, 2017 15:25
Resize with blur and quality
const makeResizer = (type:'contain'|'cover')=>(req, res) =>{
let resizer = sharp();
//....
const {quality, blur} = res.props;
if (blur) {
if (blur === true) {
resizer = resizer.blur()
} else {
@taylorstine
taylorstine / validator-middleware-extra.js
Created March 11, 2017 15:22
Validate alter image.
//...
export const validateRequest = (req, res, next)=>{
async.applyEachSeries([
validateHeaders,
validateUrlParams,
validateResizeRequest,
validateAlterImageRequest
], req, res, next);
};
@taylorstine
taylorstine / operator-middleware.js
Last active March 11, 2017 15:24
Middleware to resize the image
import urlJoin from "url-join";
import R from "ramda";
import request from "request";
import sharp from "sharp";
export const containMiddleware = (req, res, next) => {
//Use pipe to pass in the results from one function to the next
R.pipe(makeResizer('contain'), requestImage(req, res, next))(req, res, next);
};
export const coverMiddleware = (req, res, next) => {
@taylorstine
taylorstine / validator-middleware.js
Created March 11, 2017 15:04
Validates an image request
import urlJoin from 'url-join'
import async from 'async'
import _ from 'lodash'
//We don't want an image bigger than this
const MAX_IMAGE_SIZE = 36000;
/**
* Validate a request to resize the image. Call multiple validators.
@taylorstine
taylorstine / router.js
Last active March 11, 2017 14:48
The router for the image resizer.
import express from 'express'
export const router = express.Router();
router.get('/contain/:size/:revision/:image*', validateRequest, containMiddleware);
router.get('/cover/:size/:revision/:image*', validateRequest, coverMiddleware);
@taylorstine
taylorstine / request-resize-image-stream.js
Created March 11, 2017 13:56
Resize an image using streams.
import request from "request"
import sharp from 'sharp'
function requestImage(imageUrl) {
const resizer = sharp().resize(500, 500).max();
request(imageUrl).pipe(resizer);
}
@taylorstine
taylorstine / request-resize-image.js
Created March 11, 2017 12:57
Requests an image as a buffer and resizes it.
//request is the module we'll be using to make network calls.
//https://github.com/request/request
import request from "request"
import sharp from 'sharp'
function requestImage(imageUrl) {
//add encoding:null to make sure we get a buffer instead of a string in body
request({url: imageUrl, encoding:null}, (err, response, body)=>{
const theImage = body;
const resizedImage = sharp(theImage)
@taylorstine
taylorstine / request-image.js
Created March 11, 2017 12:38
A basic image requester
//request is the module we'll be using to make network calls.
//https://github.com/request/request
import request from "request"
function requestImage(imageUrl) {
request(imageUrl, (err, response, body)=>{
const theImage = body;
///do something with the image
});
}