npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mg-mongoose-thumbnail

v0.0.2

Published

Mongoose plugin adding a thumbnail field to a schema - useful for expressjs image file uploads

Downloads

6

Readme

About mongoose-thumbnail

mongoose plugin that adds a thumbnail field to a mongoose schema. This is especially suited to handle image file uploads with nodejs/expressjs.

Install

npm install mongoose-thumbnail

Usage

The plugin adds a thumbnail field to the mongoose schema. As it uses mongoose-file internally, the added field has all the mongoose-file field sub-properties. Please refer to mongoose-file documentation to understand basic usage.

In addition to the sub-fields carried by mongoose-file, this plugin creates an additional sub-field, named by default thumb. Depending on the inline plugin option, the thumbnail field may contain additional sub-fields containing the thumbnail file properties, or could be a string containing the Data URI encoded thumbnail.

In addition those pertaining to mongoose-file, the following options are available:

  • thumb - the name of the thumbnail sub-field (defaults to thumb)
  • format - the image format for the thumbnail (defaults to jpg)
  • size - the side size of the thumbnail (by default 96)
  • thumb_prefix - the prefix for the thumbnail files (defaults to t_)
  • inline - if true the thumbnail is not saved to a file but directly in the mongoose document, in the thumbnail sub-field, encoded as a string using the Data URI scheme (defaults to false)
  • save - if true the model instance is saved after every assignment to the image field (change to the file sub-property) (defaults to true)

Please note that this library re-exports also filePlugin and make_upload_to_model.

JavaScript

var mongoose = require('mongoose');
var thumbnailPluginLib = require('mongoose-thumbnail');
var thumbnailPlugin = thumbnailPluginLib.thumbnailPlugin;
var make_upload_to_model = thumbnailPluginLib.make_upload_to_model;

...

var uploads_base = path.join(__dirname, "uploads");
var uploads = path.join(uploads_base, "u");
...

var SampleSchema = new mongoose.Schema({
  ...
});
SampleSchema.plugin(thumbnailPlugin, {
	name: "photo",
	format: "png",
	size: 80,
	inline: false,
	save: true,
	upload_to: make_upload_to_model(uploads, 'photos'),
	relative_to: uploads_base
});
var SampleModel = db.model("SampleModel", SampleSchema);

CoffeeScript

mongoose = require 'mongoose'
filePluginLib = require 'mongoose-thumbnail'
filePlugin = filePluginLib.filePlugin
make_upload_to_model = filePluginLib.make_upload_to_model

...
uploads_base = path.join(__dirname, "uploads")
uploads = path.join(uploads_base, "u")
...

SampleSchema = new mongoose.Schema
  ...
SampleSchema.plugin thumbnailPlugin
	name: "photo"
	format: "png"
	size: 80
	inline: false
	save: true
	upload_to: make_upload_to_model(uploads, 'photos')
	relative_to: uploads_base
SampleModel = db.model("SampleModel", SampleSchema)

Using with express


PictureSchema = new mongoose.Schema
  title: String
PictureSchema.plugin thumbnailPlugin
  name: "photo"
  inline: false
Picture = db.model("Picture", PictureSchema)

...

app.post "/upload", (req, res, next) ->
  picture = new Picture({title: req.body.title})
  picture.set('image.file', req.files.image)
  picture.save (err) ->
    return next(err)  if (err)
  res.redirect '/'

Now in a Jade template, you could have something like:

<img src="/{{ picture.image.thumb.rel }}" />

Otherwise, using thumbnail inlining:


PictureSchema = new mongoose.Schema
  title: String
PictureSchema.plugin thumbnailPlugin,
  name: "photo"
  inline: true
Picture = db.model("Picture", PictureSchema)

the template would use:

<img src="{{ picture.image.thumb }}" />

Bugs and pull requests

Please use the github repository to notify bugs and make pull requests.

License

This software is © 2012 Marco Pantaleoni, released under the MIT licence. Use it, fork it.

See the LICENSE file for details.