-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (56 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require('express');
const upload = require('./lib/upload');
const serve = require('./lib/serve');
const errorHandler = require('./lib/errorHandler');
const PRODUCTION = process.env.NODE_ENV === 'production';
/**
* Create a storage server instance
* @param {Object} [options]
* @param {string} [options.storageDir] Folder to store files (relative to `cwd()`),
* defaults to `./storage`
* @param {string} [options.tmpDir] Temp folder used for uploading files,
* defaults to `require('os').tmpdir` in production and `./temp` relative to `cwd()` in development
* @param {number} [options.maxUploadSize] Maximum upload file size in bytes, defaults to `1024 * 1024 * 20` (20MB)
* @param {number} [options.pictureQuality] Picture quality (in percents) for stored files,
* defaults to `25` (25%)
* @param {number} [options.maxPicturePixels] Maximum stored file size in pixels,
* defaults to `1920 * 1080` (FullHD ~ 2 megapixels)
* @param {number} [options.maxUrlCacheItems] Maximum number of entries to keep in the file info cache,
* defaults to `10000000` (one million items)
* @param {number|string} [options.maxAge] maxAge of served files,
* a number representing milliseconds or a string in [ms format](https://www.npmjs.com/package/ms);
* defaults to `365 days` in production and `0` in development;
* @returns {import('express')} Express instance
*/
module.exports = (
{ storageDir, tmpDir, maxUploadSize, pictureQuality, maxPicturePixels, maxUrlCacheItems, maxAge } = {
storageDir: './storage',
tmpDir: PRODUCTION ? require('os').tmpdir : './temp',
maxUploadSize: 1024 * 1024 * 20, // 20 megabytes
pictureQuality: 85, // 85%
maxPicturePixels: 1920 * 1080, // FullHD ~ 2 megapixels
maxUrlCacheEntries: 10000000, // one million items
maxAge: PRODUCTION ? '365 days' : 0 // one year for production, none for development
}
) => {
const app = express();
app.use(express.json());
app.use(
upload({
storageDir,
tmpDir,
maxUploadSize,
pictureQuality,
maxPicturePixels
})
);
app.use(
serve({
storageDir,
maxUrlCacheItems,
maxAge
})
);
app.use(errorHandler);
return app;
};