Skip to content

Commit

Permalink
@uppy/companion: fix default getKey for non-standalone too (#3945)
Browse files Browse the repository at this point in the history
And update docs.
  • Loading branch information
mifi committed Aug 4, 2022
1 parent 96252e5 commit 1a32764
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
4 changes: 3 additions & 1 deletion examples/digitalocean-spaces/server.js
Expand Up @@ -2,6 +2,8 @@ const fs = require('node:fs')
const path = require('node:path')
const budo = require('budo')
const router = require('router')
const crypto = require('node:crypto')

const companion = require('../../packages/@uppy/companion')

/**
Expand Down Expand Up @@ -32,7 +34,7 @@ const { app: companionApp } = companion.app({
s3: {
// This is the crucial part; set an endpoint template for the service you want to use.
endpoint: 'https://{region}.digitaloceanspaces.com',
getKey: (req, filename) => `uploads/${filename}`,
getKey: (req, filename) => `${crypto.randomUUID()}-${filename}`,

key: process.env.COMPANION_AWS_KEY,
secret: process.env.COMPANION_AWS_SECRET,
Expand Down
3 changes: 2 additions & 1 deletion packages/@uppy/companion/src/config/companion.js
Expand Up @@ -2,6 +2,7 @@ const ms = require('ms')
const fs = require('node:fs')
const { isURL } = require('validator')
const logger = require('../server/logger')
const { defaultGetKey } = require('../server/helpers/utils')

const defaultOptions = {
server: {
Expand All @@ -13,7 +14,7 @@ const defaultOptions = {
endpoint: 'https://{service}.{region}.amazonaws.com',
conditions: [],
useAccelerateEndpoint: false,
getKey: (req, filename) => filename,
getKey: defaultGetKey,
expires: ms('5 minutes') / 1000,
},
allowLocalUrls: false,
Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/companion/src/server/helpers/utils.js
Expand Up @@ -164,3 +164,5 @@ module.exports.requestStream = async (req, convertResponseToError) => {

return { stream: resp }
}

module.exports.defaultGetKey = (req, filename) => `${crypto.randomUUID()}-${filename}`
2 changes: 1 addition & 1 deletion packages/@uppy/companion/src/standalone/helper.js
Expand Up @@ -72,7 +72,7 @@ const getConfigFromEnv = () => {
},
s3: {
key: process.env.COMPANION_AWS_KEY,
getKey: (req, filename) => `${crypto.randomUUID()}-${filename}`,
getKey: utils.defaultGetKey,
secret: getSecret('COMPANION_AWS_SECRET'),
bucket: process.env.COMPANION_AWS_BUCKET,
endpoint: process.env.COMPANION_AWS_ENDPOINT,
Expand Down
10 changes: 6 additions & 4 deletions website/src/docs/companion.md
Expand Up @@ -326,7 +326,7 @@ const options = {
},
},
s3: {
getKey: (req, filename, metadata) => filename,
getKey: (req, filename, metadata) => `${crypto.randomUUID()}-${filename}`,
key: '***',
secret: '***',
bucket: 'bucket-name',
Expand Down Expand Up @@ -453,24 +453,26 @@ Get the key name for a file. The key is the file path to which the file will be
* `filename`, the original name of the uploaded file;
* `metadata`, user-provided metadata for the file. See the [`@uppy/aws-s3`](https://uppy.io/docs/aws-s3/#metaFields) docs. The `@uppy/aws-s3-multipart` plugin unconditionally sends all metadata fields, so they all are available here.

If your bucket is public, you should include a cryptographically random token in the uploaded name for security (hence the default `crypto.randomUUID()`).

This function should return a string `key`. The `req` parameter can be used to upload to a user-specific folder in your bucket, for example:

```js
app.use(authenticationMiddleware)
app.use(companion.app({
s3: {
getKey: (req, filename, metadata) => `${req.user.id}/${filename}`,
getKey: (req, filename, metadata) => `${req.user.id}/${crypto.randomUUID()}-${filename}`,
/* auth options */
},
}))
```

The default implementation returns the `filename`, so all files will be uploaded to the root of the bucket as their original file name.
The default implementation uploads all files to the root of the bucket as their original file name, prefixed with a random UUID.

```js
app.use(companion.app({
s3: {
getKey: (req, filename, metadata) => filename,
getKey: (req, filename, metadata) => `${crypto.randomUUID()}-${filename}`,
},
}))
```
Expand Down

0 comments on commit 1a32764

Please sign in to comment.