Commit fbefac43 authored by Peter Müller's avatar Peter Müller

Initial commit

parents
Pipeline #236 passed with stage
in 20 seconds
node_modules
uploads
*.tgz
image: node:8
stages:
- deploy
publish-to-npm:
stage: deploy
script:
- npm config set unsafe-perm true
- npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN
- npm publish
environment:
name: production
url: https://www.npmjs.com/package/http-server-upload
when: manual
only:
- tags
# http-server-upload
This is a simple zero-configuration command-line http server which provides a lightweight interface to upload files.
By default files are uploaded to the current working directory.
Optionally a token may be used to protect against unauthorized uploads.
## Installation
```
npm install --global http-server-upload
```
This will install `http-server-upload` globally so that it may be tun from the command line.
## Usage
```
http-server-upload [uploadRootPath]
```
`[uploadRootPath]` defaults to the current working directory (`./`).
Other options can be set using environment variables.
When the server is running you can visit http://localhost:8080/ to get the upload form.
*Attention:* Already existing files will be overwritten on upload.
### Environment variables
| Variable | Description | Default |
|---|---|---|
| `PORT` | The port to use. | `8080` |
| `UPLOAD_DIR` | The directory where the files should be uploaded to. This overrides the `uploadRootPath` argument. | `uploadRootPath` argument or the current working directory |
| `UPLOAD_TMP_DIR` | Temp directory for the file upload. | The upload directory. |
| `TOKEN` | An optional token which must be provided on upload. | Nothing |
| `PATH_REGEXP` | A regular expression to verify a given upload path. This should be set with care, because it may allow write access to outside the upload directory. | `/^[a-zA-Z0-9-_/]*$/` |
Example:
```
PORT=9000 UPLOAD_DIR=~/uploads/ UPLOAD_TMP_DIR=/tmp/ TOKEN=my-super-secret-token http-server-upload
```
### Uploads from the command line
If the `http-server-upload` is running, you may also upload files from the command line using `curl`:
```
curl -F "uploads=@my-file.txt" http://localhost:8080/upload
```
Advanced example with multiple files, an upload path and a required token:
```
curl \
-F "uploads=@my-file.txt" \
-F "uploads=@my-other-file.txt" \
-F "path=my/dir" \
-F "token=my-super-secret-token" \
http://localhost:8080/upload
```
## License
MIT license
Copyright (c) 2019 Peter Müller <peter@crycode.de> https://crycode.de
#!/usr/bin/env node
require('../http-server-upload.js');
/* jshint node:true, esversion:6 */
'use strict';
const http = require('http');
const formidable = require('formidable');
const fs = require('fs');
const path = require('path');
const port = process.env.PORT || 8080;
const uploadDir = process.env.UPLOAD_DIR || process.argv[2] || process.cwd();
const uploadTmpDir = process.env.UPLOAD_TMP_DIR || uploadDir;
const token = process.env.TOKEN || false;
const pathMatchRegExp = (process.env.PATH_REGEXP) ? new RegExp(process.env.PATH_REGEXP) : /^[a-zA-Z0-9-_/]*$/;
http.createServer(function (req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
const form = new formidable.IncomingForm({
uploadDir: uploadTmpDir,
multiples: true
});
form.parse(req, function (err, fields, files) {
if (token && fields.token !== token) {
res.write('Wrong token!');
return res.end();
}
if (!Array.isArray(files.uploads)) {
files.uploads = [files.uploads];
}
if (fields.path) {
if (!fields.path.match(pathMatchRegExp)) {
res.write('Invalid path!');
return res.end();
}
} else {
fields.path = '';
}
fs.stat(path.join(uploadDir, fields.path), (err, stats) => {
if (err) {
res.write('Path does not exist!');
return res.end();
}
files.uploads.forEach((file) => {
if (!file) return;
const newPath = path.join(uploadDir, fields.path, file.name);
fs.renameSync(file.path, newPath);
console.log(new Date().toUTCString(), '- file uploaded', newPath);
});
res.write('file uploaded!');
res.end();
});
});
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="upload" method="post" enctype="multipart/form-data">');
res.write('Files: <input type="file" name="uploads" multiple="multiple"><br />');
res.write('Upload path: <input type="text" name="path" value=""><br />');
if (token) {
res.write('Token: <input type="text" name="token" value=""><br />');
}
res.write('<input type="submit" value="Upload!">');
res.write('</form>');
return res.end();
}
}).listen(port, () => {
console.log(`http server listening on port ${port}`);
console.log('upload target dir is', uploadDir);
});
{
"name": "http-server-upload",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"formidable": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/formidable/-/formidable-1.2.1.tgz",
"integrity": "sha512-Fs9VRguL0gqGHkXS5GQiMCr1VhZBxz0JnJs4JmMp/2jL18Fmbzvv7vOFRU+U8TBkHEE/CX1qDXzJplVULgsLeg=="
}
}
}
{
"name": "http-server-upload",
"version": "1.0.0",
"description": "A Simple zero-configuration command-line http server for uploading files",
"bin": "bin/http-server-upload",
"main": "http-server-upload.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"http",
"upload",
"fileupload",
"server"
],
"author": "Peter Müller <peter@crycode.de> (https://crycode.de/)",
"license": "MIT",
"homepage": "https://git.cryhost.de/crycode/http-server-upload",
"repository": {
"type": "git",
"url": "https://git.cryhost.de/crycode/http-server-upload.git"
},
"bugs": {
"url": "https://git.cryhost.de/crycode/http-server-upload/issues"
},
"files": [
"bin/http-server-upload",
"http-server-upload.js",
"README.md"
],
"dependencies": {
"formidable": "^1.2.1"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment