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

pony-uploader

v0.0.5

Published

Direct to S3 file uploads from the client.

Downloads

1

Readme

pony-uploader

Pony uploader is a simple drag & drop image uploader which will send image directly to Amazon S3. This project contains a server component which will work well with express, a client side component, and a full example project. Pony-uploader uses several HTML5 features and will probably not work on older browsers.

Getting Started

Clone the repo https://github.com/ponycode/s3-file-upload and run npm install. Next, go into the example folder and run npm install. To start the server run node app/apps.js and go to http://localhost:3000 in a browser.

$ git clone https://github.com/ponycode/s3-file-upload
$ cd s3-file-upload
$ npm install
$ cd example
$ npm install
$ node app/app.js

Picking the client apart

The client is made of 3 components: an image resizer, an uploader, and a jQuery plugin. If you don't wish to use the HTML & CSS provided by pony-debugger you can use the image resizer and uploader independently.

The uploader

  • getSignedUpload( signatureUrl, fileData, callback ) - This method will call your server to get a signed S3 upload url. The response will contain the upload url, the complete url, and what will be the images public S3 url.
  • uploadToS3( uploadUrl, uploadData, progressCallback, callback ) - This method sends the image's raw data to S3 using the signed S3 upload url.
  • completeUpload( completeUrl, callback ) - This method will call your server to let it know that the upload is complete. This is so that you can keep image references in the database and know which images were uploaded successfully.

The image resizer

  • resizeFile( file, maxWidth, maxHeight, callback ) - This method takes an HTML file object

The jQuery plugin

  • ponyUpload() - This method can be called on your .dropZone element. It will wire the drag & drop events, and expect to handle the state changes as files are uploaded. Your .dropZone element must have a data-signUrl which tells the plugin what URL to call to get a signed upload url. This route will be mostly handled by pony-uploader.
<div class="dropZone dropZoneDefault" data-signUrl="{signatureUrl}">
    <div class="defaultMessage text-center">
        <h3>Drag files here</h3>
    </div>
    <div class="dropMessage text-center">
        <h3>Now drop it</h3>
    </div>
    <div class="uploading text-center">
        <h3>Uploading</h3>
        <div class="progress">
            <div class="progress-bar progress-bar-warning progress-bar-striped" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0;"></div>
        </div>
    </div>
    <div class="complete text-center">
        <h3>All Done!</h3>
    </div>
    <div class="error text-center">
        <h3>Oops!</h3>
        <p class="errorMessage">Error uploading file</p>
    </div>
</div>

Integrating the server component

You can integrate the server component by requiring pony-uploader:


var ponyUploaderOptions = {
    bucket: config.get("s3.bucket"),
    s3AccessKeyId: config.get("s3.accessKey"),
    s3AccessKeySecret: config.get("s3.secretKey")
};

var ponyUploader = require('pony-uploader')( ponyUploaderOptions );

app.get( "/uploads/sign", function( req, res ){

    var filename = req.query['filename'];
    var filetype = req.query['filetype'];
    var filesize = req.query['filesize'];
    var width = req.query['width'] || 0;
    var height = req.query['height'] || 0;

    filename = _sanitizeFilename( filename );

    // This is your chance to load the image data into your database.
    // You'll also need to create an S3 key for your image.
    var image = new Image();
    image.uploadedBy = req.user;
    image.s3Key = (new Date().getTime()/1000) + "-" + filename;
    image.width = width;
    image.height = height;

    var upload = {
        key: image.s3Key,
        filename: filename,
        filetype: filetype,
        filesize: filesize,
        completeUrl: req.protocol + '://' + req.get('host') + "/uploads/" + image.id + "/complete"
    };

    ponyUploader.getS3UploadSignature( upload, function( error, result ){
        if( error ){
            res.status( 500 ).send( "Error signing file for upload" );
            return;
        }

        // This is a good place to save the image because the signature may be rejected it is the wrong filetype or too big
        image.url = result.publicUrl;
        image.save( function( error, image ) {
            if (error) {
                console.error("Error creating image: ", error);
                res.status(500).send("Error creating image");
                return;
            }
            res.send( result );
        });
    });

});

License

Copyright (c) 2015 Joshua Kennedy Licensed under the MIT license.