This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mySchema = new Schema({ | |
//... | |
} | |
}, {bufferCommands: false}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mongoose.connect("mongo://yourdatabaseURI:4321", {server: {reconnectTries: Number.MAX_SAFE_INTEGER}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const makeResizer = (type:'contain'|'cover')=>(req, res) =>{ | |
let resizer = sharp(); | |
//.... | |
const {quality, blur} = res.props; | |
if (blur) { | |
if (blur === true) { | |
resizer = resizer.blur() | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
export const validateRequest = (req, res, next)=>{ | |
async.applyEachSeries([ | |
validateHeaders, | |
validateUrlParams, | |
validateResizeRequest, | |
validateAlterImageRequest | |
], req, res, next); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import request from "request" | |
import sharp from 'sharp' | |
function requestImage(imageUrl) { | |
const resizer = sharp().resize(500, 500).max(); | |
request(imageUrl).pipe(resizer); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
}); | |
} |
NewerOlder