Skip to content

Commit

Permalink
First go at getting savefiles.org upload to work
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeymond committed Mar 24, 2024
1 parent 8724aa7 commit b0a6377
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 233 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Publish Package to npmjs
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
package-lock.json
yarn.lock
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
# Multer Proxy Storage

This is a custom multer storage engine that proxy the received file into a remote server.
The proxy is forwarded as multipart/form-data.
This is a custom multer storage engine that proxy the received file into a savefiles.org drive.
The data is forwarded as multipart/form-data.

## Install

```
npm install --save multer-proxy-storage
npm install --save multer-savefilesorg-storage
```

## Usage

In this example we are forwarding user uploaded file into remote server http://www.example.com/upload, with the file
In this example we are forwarding user uploaded file into savefiles.org drive https://savefiles.org/api/v1/uploads, with the file
identified with the parameter 'file'.

``` javascript
var multer = require('multer')
var MulterProxyStorage = require('multer-proxy-storage')
var {MulterSaveFilesOrgStorage} = require('multer-savefilesorg-storage')

this.routePost('/uploadFile',
(req, res, next) => {
multer({
storage: MulterProxyStorage(
storage: MulterSaveFilesOrgStorage(
{
serverPath: `http://www.example.com/upload`,
fileParamName: 'file'
serverPath: `https://savefiles.org/api/v1/uploads`,
apiAccessToken: '<create access token under account settings>',
fileParamName: 'file' // If left blank, this defaults to 'file',
relativePath: '/' // If left blank, this defaults to root of drive '/',
}),
preservePath: true
}).array('file')(req, res, next)
Expand All @@ -34,4 +36,4 @@ this.routePost('/uploadFile',
```

## License
Multer-Proxy-Storage is released under the [MIT](License) license.
multer-savefilesorg-storage is released under the [MIT](License) license.
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
var multerProxyStorage = require('./lib/multer-proxy-storage.js')

module.exports = multerProxyStorage
exports.MulterSaveFilesOrgStorage = require('./lib/multer-savefilesorg-storage.js')
51 changes: 0 additions & 51 deletions lib/multer-proxy-storage.js

This file was deleted.

61 changes: 61 additions & 0 deletions lib/multer-savefilesorg-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var axios = require('axios')
var concat = require('concat-stream')
var FormData = require('form-data')

// Maximum time to upload to the server
const UPLOAD_TIMEOUT = 5000

/*
This is a custom multer storage engine that orxy the received data into a savefiles.org drive.
The data is forwarded as multipart/form-data.
opts: {
serverPath: 'https://savefiles.org/api/v1/uploads',
apiAccessToken: '<create access token under account settings>',
fileParamName: 'file' // If left blank, this defaults to 'file',
relativePath: '/' // If left blank, this defaults to root of drive '/',
}
SaveFilesOrg is expected to return:
HTTP Code 201
or
HTTP Code Non-201, which is considered an error
*/
class MulterSaveFilesOrgStorage {
constructor(opts) {
this.opts = opts
}

_handleFile(req, file, cb) {
var form = new FormData()
// Use filepath to specify the file's fullpath. If we use filename, it'll be cut down into only the filename
form.append(this.opts.fileParamName || 'file', file.stream, { filepath: file.originalname })
form.append('relativePath', this.opts.relativePath || '/')
form.pipe(concat({ encoding: 'buffer' }, data => {
axios.post(
this.opts.serverPath,
data,
{
headers: {
...form.getHeaders(),
Authorization: `Bearer ${this.opts.apiAccessToken}`
},
timeout: UPLOAD_TIMEOUT
}
).then(resp => {
req.serverResp = resp.data
cb(null)
}).catch(err => {
cb(err)
})
}))
}

_removeFile(req, file, cb) {
cb(null)
}
}

module.exports = opts => {
return new MulterSaveFilesOrgStorage(opts)
}
157 changes: 0 additions & 157 deletions package-lock.json

This file was deleted.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"name": "multer-proxy-storage",
"version": "1.0.2",
"description": "Forward multipart/form-data file upload into a specified server.",
"name": "multer-savefilesorg-storage",
"version": "1.0.0",
"description": "Forward multipart/form-data file upload into a savefiles.org drive.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": {
"name": "Denny Harijanto",
"email": "denny@nusantara-cloud.com",
"url": "http://www.nusantara-cloud.com"
"name": "Michael Hammond",
"email": "mickeymond@gmail.com",
"url": "https://mickeymond.xyz"
},
"homepage": "https://github.com/nusantara-cloud/multer-proxy-storage",
"homepage": "https://github.com/mickeymond/multer-savefilesorg-storage",
"repository": {
"bugs": {
"url": "https://github.com/nusantara-cloud/multer-proxy-storage"
"url": "https://github.com/mickeymond/multer-savefilesorg-storage/issues"
},
"type": "git",
"url": "git://github.com/nusantara-cloud/multer-proxy-storage.git"
"url": "git://github.com/mickeymond/multer-savefilesorg-storage.git"
},
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"concat-stream": "^1.6.2",
"form-data": "^2.3.2"
"axios": "^1.6.8",
"concat-stream": "^2.0.0",
"form-data": "^4.0.0"
}
}
}

0 comments on commit b0a6377

Please sign in to comment.