-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm.hbs
121 lines (92 loc) · 3.6 KB
/
rm.hbs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
[![view on npm](https://img.shields.io/npm/v/@uttori/plugin-upload-multer.svg)](https://www.npmjs.com/package/@uttori/plugin-upload-multer)
[![npm module downloads](https://img.shields.io/npm/dt/@uttori/plugin-upload-multer.svg)](https://www.npmjs.com/package/@uttori/plugin-upload-multer)
[![Build Status](https://travis-ci.com/uttori/uttori-plugin-upload-multer.svg?branch=master)](https://travis-ci.com/uttori/uttori-plugin-upload-multer)
[![Coverage Status](https://coveralls.io/repos/github/uttori/uttori-plugin-upload-multer/badge.svg?branch=master)](https://coveralls.io/github/uttori/uttori-plugin-upload-multer?branch=master)
![dependency status](https://img.shields.io/librariesio/release/npm/@uttori/plugin-upload-multer)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/@uttori/plugin-upload-multer?label=Minified%20%2B%20GZip)
# Uttori Plugin - Multer Upload
A plugin to add file uploading using Multer.
## Install
```bash
npm install --save uttori-plugin-upload-multer
```
## Config
```js
{
// Registration Events
events: {
bindRoutes: ['bind-routes'],
},
// Directory files will be uploaded to.
directory: 'uploads',
// Server route to POST uploads to.
route: '/upload',
// Server route to GET uploads from.
publicRoute: '/uploads',
// Custom Middleware for the Upload route
middleware: [],
}
```
### Nested Uploads
When supplised with a `fullPath` key, nested uploads will retain their paths on upload:
```js
// File Uploads
try {
// https://docs.dropzone.dev/configuration/basics/configuration-options
Dropzone.options.fileupload = {
paramName: 'file',
withCredentials: true,
init: function init() {
// Allow dropping a folder to upload all files.
this.hiddenFileInput.setAttribute('webkitdirectory', true);
},
};
const dropzone = new Dropzone('.dropzone');
// Add more information to the file uploads.
dropzone.on('sending', (file, _xhr, formData) => {
formData.append('fullPath', file.fullPath || '');
formData.append('filesize', file.size || 0);
formData.append('lastModified', file.lastModified || Date.now());
});
// Read the upload path from the elements data-upload attribute and escape any text sent back from the server.
dropzone.on('success', (file, responseText) => {
// Create the correct type of Markdown link.
let linkToUploadedFile = `[${file.name}](${encodeURI(responseText)})`;
if (file.type.startsWith('image/')) {
linkToUploadedFile = `![${file.name}](${encodeURI(responseText)})`;
} else if (file.type.startsWith('video/')) {
linkToUploadedFile = `<video controls playsinline muted src="${responseText}"></video>`;
}
// Copy to clipboard.
navigator.clipboard.writeText(linkToUploadedFile).then(() => {
console.log('Async: Copying to clipboard was successful!');
}, (error) => {
console.error('Async: Could not copy text: ', error);
});
// Append to the content.
/** @type {HTMLTextAreaElement | null} */
const textarea = document.querySelector('textarea#editing');
if (textarea) {
textarea.value = `${textarea.value}\n${linkToUploadedFile}\n`;
textarea.dispatchEvent(new Event('input'));
}
});
} catch (error) {
console.error('Dropzone Error:', error);
}
```
* * *
## API Reference
{{>main}}
* * *
## Tests
To run the test suite, first install the dependencies, then run `npm test`:
```bash
npm install
npm test
DEBUG=Uttori* npm test
```
## Contributors
* [Matthew Callis](https://github.com/MatthewCallis)
## License
* [MIT](LICENSE)