# express-file-wizardry

> Flexible and extensible Express middleware for handling file uploads. Written in Typescript with support for various storage engines.

Latest version **2.1.2** (published 2024-03-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-file-wizardry
pnpm add express-file-wizardry
yarn add express-file-wizardry
bun add express-file-wizardry
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.2 |
| Published | 2024-03-20 |
| First published | 2023-12-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 37.1 KB |
| Known vulnerabilities | 0 (+13 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Usman Soliu |
| Maintainers | devfresher |
| Keywords | amazons3, google-cloud, disk-storage, express, multer, file-upload, middleware |

## Links

- npm: https://www.npmjs.com/package/express-file-wizardry
- Repository: https://github.com/devfresher/express-file-wizardry
- Homepage: https://github.com/devfresher/express-file-wizardry#readme
- Issues: https://github.com/devfresher/express-file-wizardry/issues
- npm.io page: https://npm.io/package/express-file-wizardry

## Dependencies (8)

- [multer](https://npm.io/package/multer.md) ^1.4.5-lts.1
- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.1516.0
- [express](https://npm.io/package/express.md) ^4.18.2
- [multer-s3](https://npm.io/package/multer-s3.md) ^3.0.1
- [cloudinary](https://npm.io/package/cloudinary.md) ^1.41.0
- [@aws-sdk/client-s3](https://npm.io/package/@aws-sdk/client-s3.md) ^3.470.0
- [@google-cloud/storage](https://npm.io/package/@google-cloud/storage.md) ^7.7.0
- [multer-storage-cloudinary](https://npm.io/package/multer-storage-cloudinary.md) ^4.0.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 2.1.2 (latest) — 2024-03-20
- 2.1.1 — 2024-03-20
- 2.1.0 — 2024-02-24
- 2.0.0 — 2024-02-18
- 1.2.1 — 2023-12-18
- 1.2.0 — 2023-12-17
- 1.1.0 — 2023-12-14
- 1.0.2 — 2023-12-13
- 1.0.0 — 2023-12-13

## README

# express-file-wizardry

[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

`express-file-wizardry` is an Express middleware for handling file uploads with support for different storage types including memory, disk, cloudinary and Amazon S3.

## Why express-file-wizardry

- **Simplified Configuration:** `express-file-wizardry` provides a straightforward API for handling file uploads with various storage options. Whether you prefer `in-memory storage`, `local disk storage`, or `cloud storage` with services like `Cloudinary`, `Amazon S3`, `Google cloud (upcoming)`, the configuration is simplified, allowing you to focus on your application logic.

- **Modularity and Separation of Concerns:** The package is designed with modularity in mind, separating concerns such as storage configuration, file filtering, and middleware setup. This makes the codebase clean, maintainable, and easy to extend.

- **Versatile File Type Support:** You can easily define the allowed file formats for uploads, ensuring that your application only accepts the types of files you expect. The package supports a wide range of file types, from images to documents and archives.

- **Flexible Storage Options:** Choose the storage type that best fits your application requirements. Whether you need fast in-memory storage for temporary files, local disk storage for persistent storage, cloudinary or Amazon S3 for scalable cloud storage, `express-file-wizardry` has you covered.

## Table of Contents

- [Installation](#installation)
- [Usage: JavaScript (CommonJS)](#usage-javascript-commonjs)
- [Usage: TypeScript (ESM)](#usage-typescript-esm)
- [API](#api)
- [Examples](#examples)
- [Future Enhancements / To-Do](#future-enhancements--to-do)
- [Contributing](#contributing)
- [License](#license)

## Installation

Install the package using npm:

```bash
npm install express-file-wizardry
```

## Usage: JavaScript (CommonJS)

``` javascript
const express = require('express');
const { FileWizardry } = require('express-file-wizardry');

const app = express();

const fileWizardry = new FileWizardry();

// Use memory storage (default)
fileWizardry.setStorageType('memory');

// Alternatively, use disk storage (default destination: 'uploads')
// fileWizardry.setStorageType('disk', { destination: '/path/to/upload/folder' });


// Or use Amazon S3 storage
// fileWizardry.setStorageType('amazons3', { accessKeyId: 'your-access-key', secretAccessKey:   'your-secret-key', region: 'your-region', bucket: 'your-bucket' });

// Or use Cloudinary storage
// fileWizardry.setStorageType('cloudinary', { cloud_name: 'my-cloud', api_key: 'api-key', api_secret: 'api-secret' });

// Middleware for handling file uploads
app.post('/upload', fileWizardry.uploadFile({ formats: ['image/jpeg', 'image/png'], fieldName: 'image' }), (req, res) => {

    if (req.fileValidationError) {
        return res.status(400).json({ error: req.fileValidationError.message });
    }

    // Handle successful upload
    res.json({ message: 'File uploaded successfully' });
    });

    app.listen(3000, () => {
    console.log('Server running on http://localhost:3000');
});
```

## Usage: TypeScript (ESM)

``` typescript
import express from 'express';
import { FileWizardry } from 'express-file-wizardry';

const app = express();

const fileWizardry = new FileWizardry();

// Use memory storage (default)
fileWizardry.setStorageType('memory');

// Alternatively, use disk storage (default destination: 'uploads')
// fileWizardry.setStorageType('disk', { destination: '/path/to/upload/folder' });

// Or use Amazon S3 storage
// fileWizardry.setStorageType('amazons3', { accessKeyId: 'your-access-key', secretAccessKey: 'your-secret-key', region: 'your-region', bucket: 'your-bucket' });

// Or use Cloudinary storage
// fileWizardry.setStorageType('cloudinary', { cloud_name: 'my-cloud', api_key: 'api-key', api_secret: 'api-secret' });

// Middleware for handling file uploads
app.post('/upload', fileWizardry.uploadFile({ formats: ['image/jpeg', 'image/png'], fieldName: 'image' }), (req, res) => {

    if (req.fileValidationError) {
        return res.status(400).json({ error: req.fileValidationError.message });
    }

    // Handle successful upload
    res.json({ message: 'File uploaded successfully' });
});

app.listen(3000, () => {
    console.log('Server running on http://localhost:3000');
});
```

## API

`FileWizardry`

`constructor(initialStorageType?: StorageType, options?: StorageTypeConfiguration)`

- `initialStorageType` (optional): Initial storage type. Default is 'memory'.
- `options` (optional): Storage options.

`uploadFile(options: UploadOptions): RequestHandler`
Middleware for handling file uploads.

- `options`: Upload options.
  - `storageType` (optional): Storage type.
  - `formats`: Array of allowed file formats.
  - `fieldName` (optional): Name of the field in the request.
  - `maxSize` (optional): Maximum file size in bytes.
  - `multiFile` (optional): Boolean, set to `true` for multiple file uploads.

    When handling multiple file uploads:
    - If `multiFile` is `true`, the uploaded files will be accessible with `req.files`.
    - If `multiFile` is `false` or not specified, the uploaded file will be accessible with `req.file`.

`setStorageType(storageType: StorageType, options?: StorageTypeConfiguration): void`
Set the storage type for file uploads.

- `storageType`: Storage type ('memory', 'disk', or 'amazons3').
- `options`: Storage options.

## Examples

For more examples, check the [examples](/examples/) directory.

## Future Enhancements / To-Do

Feel free to contribute to the improvement of `express-file-wizardry` by working on or suggesting the following enhancements:

- **Advanced File Filtering:** Enhance file filtering capabilities to support more advanced filtering options such as file size ranges, mime type checking, etc.

- **Middleware Options:** Provide additional options for middleware configuration to offer more flexibility to users.

- **More Storage Options:** Explore additional storage options based on user demand or emerging technologies.

If you have any suggestions or would like to contribute, feel free to open an issue or submit a pull request.

## Contributing

Please contact `fresher.dev01@gmail.com` if you're interested.
Contributions are welcome! See [CONTRIBUTING.md](/CONTRIBUTING.md) for more information.

## License

This project is licensed under the MIT License - see the [LICENSE](https://opensource.org/licenses/MIT) file for details.

## Credits

This project relies on the following packages:

- **multer-S3:** A fantastic library for Amazon S3 storage. Special thanks to [Linus Unnebäck](https://github.com/LinusU) for their valuable contribution to the Node.js community.

- **multer-storage-cloudinary:** Excellent multer storage engine for Cloudinary. Kudos to the authors for their contribution.

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