This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
forked from dylanlott/storj-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (154 loc) · 4.73 KB
/
index.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const assert = require('assert');
const path = require('path');
const express = require('express');
const errors = require('storj-service-error-types');
const middleware = require('storj-service-middleware');
const busboy = require('connect-busboy');
const fs = require('fs-extra');
const os = require('os');
const { Environment } = require('storj');
const app = express();
const rawbody = middleware.rawbody;
const multer = require('multer');
const url = require('url');
const server = (config) => {
const storj = new Environment({
bridgeUrl: config.bridgeUrl,
bridgeUser: config.bridgeUser,
bridgePass: config.bridgePass,
encryptionKey: config.encryptionKey,
logLevel: config.logLevel
});
const destination = config.destination || 'uploads/';
const server = express();
const handleError = (err, req, res) => res.status(500).send(err);
const storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, destination)
},
filename: function(req, file, callback) {
callback(null, req.body.fileTitle || fileTitle)
}
});
// form-data key must match the value passed to `single()`
const upload = multer({ storage: storage }).single('file');
const fileTitle = 'storj-file-' + Date.now() + '.jpg';
const uploadFilePath = './uploads/' + fileTitle;
const downloadFilePath = config.downloadPath || './downloads'
/*
* BASIC REQUESTS
*/
server.get('/', (req, res) => {
storj.getInfo((err, result) => {
if (err) handleError(err, req, res);
return res.status(200).send(result);
});
});
/*
* BUCKETS
*/
server.get('/buckets', (req, res) => {
storj.getBuckets((err, result) => {
if (err) {
res.status(500).send(errors.InternalError());
}
return res.status(200).send(result);
});
});
server.post('/buckets', rawbody, (req, res) => {
storj.createBucket(req.body.bucketName, (err, result) => {
if (err) {
return res.status(500).send(errors.InternalError());
}
return res.status(201).send(result);
});
});
server.delete('/buckets/:id', (req, res) => {
console.log('req.params', req.params);
storj.deleteBucket(req.params.id, (err, result) => {
if (err) {
res.status(500).send(errors.InternalError());
}
return res.status(203).send(result);
});
});
/*
* FILES
*/
server.get('/buckets/:id', (req, res) => {
storj.listFiles(req.params.id, (err, result) => {
if (err) handleError(err, req, res);
return res.status(200).send(result);
});
});
server.post('/buckets/:id', (req, res, next) => {
// accepts a single file
let bucketId = req.params.id;
function uploadFile() {
let uploadFilePromise = new Promise((resolve, reject) => {
upload(req, res, function(err) {
if (err) {
console.log('multer error uploading file', err);
return reject(err);
} else {
resolve(req);
console.log('file successfully uploaded to local server!');
}
});
});
return uploadFilePromise;
}
function sendToBridge() {
const extension = req.file.mimetype.split('/')
console.log('extension: ', extension[1])
const formattedFilename = `${req.body.filename || fileTitle}.${extension[1]}`
storj.storeFile(bucketId, uploadFilePath, {
filename: formattedFilename,
progressCallback: (progress, uploadedBytes, totalBytes) => {
console.log('Progress: %d, UploadedBytes: %d, TotalBytes: %d',
progress, uploadedBytes, totalBytes);
},
finishedCallback: (err, fileId) => {
if (err) {
console.log(err);
return next(err);
}
console.log('File upload complete:', fileId);
return res.status(201).send({
file_id: fileId,
message: 'success'
});
}
});
}
uploadFile()
.then(sendToBridge)
.catch((err) => {
return next(err);
});
});
server.get('/buckets/:bucketId/files/:fileId', (req, res) => {
const filePath = `${downloadFilePath}/${req.params.fileId}`
console.log('downloading to ', filePath);
storj.resolveFile(req.params.bucketId, req.params.filedId, downloadFilePath, {
progressCallback: function (progress, downloadedBytes, totalBytes) {
console.log('progress: ', progress);
},
finishedCallback: function (err) {
if (err) console.error('Error downloading file: ', err);
return res.status(200).send('file downloaded')
},
overwrite: true
});
});
server.delete('/buckets/:bucketId/files/:fileId', (req, res) => {
storj.deleteFile(req.params.bucketId, req.params.fileId, (err, result) => {
if (err) {
console.log(`error deleting file ${req.params.fileId}: ${err}`);
}
console.log('RESULT: ', result);
});
});
return server;
}
module.exports = server;