-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
114 lines (101 loc) · 3.2 KB
/
index.ts
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
import express from 'express';
import bodyParser from 'body-parser'
import companion from '@uppy/companion'
import * as path from "path";
const session = require('express-session')
const fs = require('fs')
const cors = require('cors')
const port = 8080
const DATA_DIR = path.join(__dirname, 'uploads')
// Companion requires body-parser and express-session middleware.
// You can add it like this if you use those throughout your app.
//
// If you are using something else in your app, you can add these
// middleware in the same subpath as Companion instead.
const app = express();
const corsOptions = {
origin: '*',
credentials: true,
methods: ['GET', 'HEAD', 'POST', 'PUT', 'OPTIONS', 'PATCH', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization', 'Uppy-Versions', 'Accept'],
exposedHeaders: ['Access-Control-Allow-Headers'],
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
app.use(cors(corsOptions))
app.use(bodyParser.json())
app.use(session({
secret: 'some-secret-II',
resave: true,
saveUninitialized: true,
}))
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin || '*')
next()
})
// Routes
app.get('/', (req, res) => {
res.setHeader('Content-Type', 'text/plain')
res.send('Welcome to Companion')
})
// Initialize uppy
const uppyOptions = {
providerOptions: {
s3: {
getKey: (req, filename, _metadata) => `${req.user.id}/${filename}`,
key: process.env.COMPANION_AWS_KEY,
secret: process.env.COMPANION_AWS_SECRET,
bucket: process.env.COMPANION_AWS_BUCKET,
region: process.env.COMPANION_AWS_REGION,
},
// you can also add options for additional providers here
},
server: {
host: 'localhost:8080',
protocol: 'http', // 'http' || 'https'
path: '/companion',
},
filePath: DATA_DIR,
secret: 'some-secret',
uploadUrls: 'http://localhost:3000',
debug: true,
allowLocalUrls: true, // Only enable in development
maxFileSize: 1000000000,
corsOrigins: true,
}
// Create the data directory here for test purposes only
try {
fs.accessSync(DATA_DIR)
} catch (err) {
fs.mkdirSync(DATA_DIR)
}
process.on('exit', () => {
fs.rmSync(DATA_DIR, { recursive: true, force: true })
})
try {
const server = app.listen(port, (err?: any) => {
if (err) throw err;
console.log(`> Ready on localhost:${port}`);
console.log('> Welcome to Companion!')
})
companion.socket(server);
app.options('*', cors(corsOptions));
app.use('/companion', companion.app(uppyOptions));
} catch (error) {
console.log(error);
}
// add companion emitter to keep track of the state of the upload
const companionApp = companion.app(uppyOptions)
const { companionEmitter: emitter }: any = companionApp
emitter.on('upload-start', ({ token }) => {
console.log('Upload started', token)
function onUploadEvent ({ action, payload }) {
if (action === 'success') {
emitter.off(token, onUploadEvent) // avoid listener leak
console.log('Upload finished', token, payload.url)
} else if (action === 'error') {
emitter.off(token, onUploadEvent) // avoid listener leak
console.error('Upload failed', payload)
}
}
emitter.on(token, onUploadEvent)
})