JSPM

keystone-storage-namefunctions

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 951
  • Score
    100M100P100Q119898F
  • License MIT

Simple functions for generating filenames

Package Exports

  • keystone-storage-namefunctions
  • keystone-storage-namefunctions/ensureCallback
  • keystone-storage-namefunctions/prototypeMethods

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (keystone-storage-namefunctions) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

keystone-storage-namefunctions

Simple functions for generating filenames.

Bundled with KeystoneJS

Included Methods

  • contentHashFilename Calculate the filename from the sha1 hash of the file contents
  • originalFilename Return the original filename provided in the file object
  • randomFilename Generate a random filename

Other modules in this package

prototypeMethods

Two methods are included to help reduce code repetition in adapters that can be added to the adapter prototype when you want to handle filename generation:

  • getFilename - used to generate a filename for a given file object
  • retryFilename - used to retry a number of times when the whenExists option value is "retry" (see below)

These methods both depend on a fileExists(filename, callback) method being implemented on the adapter, and an options object on the adapter instance with the following keys:

  • options.generateFilename - a method to generate the filename, e.g. from this package
  • options.whenExists - one of "overwrite", "error" or "retry"
  • options.retryAttempts - the maximum number of retry attempts when the whenExists option value is "retry"

Use them like this:

var prototypeMethods = require('keystone-storage-namefunctions/prototypeMethods');
Adapter.prototype.getFilename = prototypeMethods.getFilename;
Adapter.prototype.retryFilename = prototypeMethods.retryFilename;
Adapter.prototype.fileExists = function (filename, callback) {
    var result = false; // replace with actual logic
    callback(null, result);
}
Adapter.prototype.uploadFile = function (file, callback) {
    this.getFilename(file, function (err, filename) {
        // move the file into place
    });
};

ensureCallback utility

The ensureCallback method is also included, to standardise sync filename method call signatures. Use it like this:

var ensureCallback = require('keystone-storage-namefunctions/ensureCallback');
function originalFilename (file, index) {
    return file.originalname;
}
var asyncOriginalFilename = ensureCallback(originalFilename);
asyncOriginalFilename(file, index, function (err, result) {
    // result === file.originalname
});

License

Licensed under The MIT License. Copyright (c) 2016 Jed Watson