# keystone-storage-namefunctions

> Simple functions for generating filenames

Latest version **1.1.1** (published 2016-09-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install keystone-storage-namefunctions
pnpm add keystone-storage-namefunctions
yarn add keystone-storage-namefunctions
bun add keystone-storage-namefunctions
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2016-09-05 |
| First published | 2016-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Jed Watson |
| Maintainers | jedwatson |
| Keywords | fs, keystonejs, keystone, storage, utils, file, filename |

## Links

- npm: https://www.npmjs.com/package/keystone-storage-namefunctions
- Repository: https://github.com/keystonejs/keystone-storage-namefunctions
- Homepage: https://github.com/keystonejs/keystone-storage-namefunctions#readme
- Issues: https://github.com/keystonejs/keystone-storage-namefunctions/issues
- npm.io page: https://npm.io/package/keystone-storage-namefunctions

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.1.1 (latest) — 2016-09-05
- 1.1.0 — 2016-09-03
- 1.0.0 — 2016-08-09

## README

# keystone-storage-namefunctions

Simple functions for generating filenames.

Bundled with [KeystoneJS](http://keystonejs.com)

## 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:

```js
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:

```js
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

---
_Source: https://npm.io/package/keystone-storage-namefunctions · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
