Skip to content

Commit 14569b4

Browse files
committed
2 parents c275388 + cf61122 commit 14569b4

File tree

84 files changed

+5388
-138
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+5388
-138
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Files Up and Down
2+
3+
This folder contains sample code from the [Uploading and Downloading Files with Node.js and Oracle Database](https://jsao.io/2019/06/uploading-and-downloading-files-with-node-js-and-oracle-database) blog series. See that post for more details.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
hrPool: {
3+
user: process.env.HR_USER,
4+
password: process.env.HR_PASSWORD,
5+
connectString: process.env.HR_CONNECTIONSTRING,
6+
poolMin: 10,
7+
poolMax: 10,
8+
poolIncrement: 0
9+
}
10+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
port: process.env.HTTP_PORT || 3000
3+
};
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const employees = require('../db_apis/employees.js');
2+
3+
async function get(req, res, next) {
4+
try {
5+
const context = {};
6+
7+
context.id = parseInt(req.params.id, 10);
8+
context.skip = parseInt(req.query.skip, 10);
9+
context.limit = parseInt(req.query.limit, 10);
10+
context.sort = req.query.sort;
11+
context.department_id = parseInt(req.query.department_id, 10);
12+
context.manager_id = parseInt(req.query.manager_id, 10);
13+
14+
const rows = await employees.find(context);
15+
16+
if (req.params.id) {
17+
if (rows.length === 1) {
18+
res.status(200).json(rows[0]);
19+
} else {
20+
res.status(404).end();
21+
}
22+
} else {
23+
res.status(200).json(rows);
24+
}
25+
} catch (err) {
26+
next(err);
27+
}
28+
}
29+
30+
module.exports.get = get;
31+
32+
function getEmployeeFromRec(req) {
33+
const employee = {
34+
first_name: req.body.first_name,
35+
last_name: req.body.last_name,
36+
email: req.body.email,
37+
phone_number: req.body.phone_number,
38+
hire_date: req.body.hire_date,
39+
job_id: req.body.job_id,
40+
salary: req.body.salary,
41+
commission_pct: req.body.commission_pct,
42+
manager_id: req.body.manager_id,
43+
department_id: req.body.department_id
44+
};
45+
46+
return employee;
47+
}
48+
49+
async function post(req, res, next) {
50+
try {
51+
let employee = getEmployeeFromRec(req);
52+
53+
employee = await employees.create(employee);
54+
55+
res.status(201).json(employee);
56+
} catch (err) {
57+
next(err);
58+
}
59+
}
60+
61+
module.exports.post = post;
62+
63+
async function put(req, res, next) {
64+
try {
65+
let employee = getEmployeeFromRec(req);
66+
67+
employee.employee_id = parseInt(req.params.id, 10);
68+
69+
employee = await employees.update(employee);
70+
71+
if (employee !== null) {
72+
res.status(200).json(employee);
73+
} else {
74+
res.status(404).end();
75+
}
76+
} catch (err) {
77+
next(err);
78+
}
79+
}
80+
81+
module.exports.put = put;
82+
83+
async function del(req, res, next) {
84+
try {
85+
const id = parseInt(req.params.id, 10);
86+
87+
const success = await employees.delete(id);
88+
89+
if (success) {
90+
res.status(204).end();
91+
} else {
92+
res.status(404).end();
93+
}
94+
} catch (err) {
95+
next(err);
96+
}
97+
}
98+
99+
module.exports.delete = del;
100+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fileDetails = require('../db_apis/file_details.js');
2+
3+
async function get(req, res, next) {
4+
try {
5+
const context = {};
6+
7+
context.id = parseInt(req.params.id, 10);
8+
9+
const rows = await fileDetails.find(context);
10+
11+
if (req.params.id) {
12+
if (rows.length === 1) {
13+
res.status(200).json(rows[0]);
14+
} else {
15+
res.status(404).end();
16+
}
17+
} else {
18+
res.status(200).json(rows);
19+
}
20+
} catch (err) {
21+
next(err);
22+
}
23+
}
24+
25+
module.exports.get = get;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
const files = require('../db_apis/files.js');
2+
3+
async function post(req, res, next) {
4+
try {
5+
const maxFileSize = 1024 * 1024 * 50; // 50 MB
6+
let contentBuffer = [];
7+
let totalBytesInBuffer = 0;
8+
let contentType = req.headers['content-type'] || 'application/octet';
9+
let fileName = req.headers['x-file-name'];
10+
11+
if (fileName === '') {
12+
res.status(400).json({error: 'The file name must be passed in the via x-file-name header'});
13+
return;
14+
}
15+
16+
req.on('data', chunk => {
17+
contentBuffer.push(chunk);
18+
totalBytesInBuffer += chunk.length;
19+
20+
// Look to see if the file size is too large.
21+
if (totalBytesInBuffer > maxFileSize) {
22+
req.pause();
23+
24+
res.header('Connection', 'close');
25+
res.status(413).json({error: `The file size exceeded the limit of ${maxFileSize} bytes`});
26+
27+
req.connection.destroy();
28+
}
29+
});
30+
31+
// Could happen if the client cancels the upload.
32+
req.on('aborted', function() {
33+
// Nothing to do with buffering, garbage collection will clean everything up.
34+
});
35+
36+
req.on('end', async function() {
37+
contentBuffer = Buffer.concat(contentBuffer, totalBytesInBuffer);
38+
39+
try {
40+
const fileId = await files.create(fileName, contentType, contentBuffer);
41+
42+
res.status(201).json({fileId: fileId});
43+
} catch (err) {
44+
console.error(err);
45+
46+
res.header('Connection', 'close');
47+
res.status(500).json({error: 'Oops, something broke!'});
48+
49+
req.connection.destroy();
50+
}
51+
});
52+
} catch (err) {
53+
next(err);
54+
}
55+
}
56+
57+
module.exports.post = post;
58+
59+
async function get(req, res, next) {
60+
try {
61+
const id = parseInt(req.params.id, 10);
62+
63+
if (isNaN(id)) {
64+
res.status(400).json({error: 'Missing or invalid file id'});
65+
return;
66+
}
67+
68+
const rows = await files.get(id);
69+
70+
if (rows.length === 1) {
71+
res.status(200);
72+
73+
res.set({
74+
'Cache-Control': 'no-cache',
75+
'Content-Type': rows[0].content_type,
76+
'Content-Length': rows[0].file_length,
77+
'Content-Disposition': 'attachment; filename=' + rows[0].file_name
78+
});
79+
80+
res.send(rows[0].blob_data);
81+
} else {
82+
res.status(404).end();
83+
}
84+
} catch (err) {
85+
next(err);
86+
}
87+
}
88+
89+
module.exports.get = get;
90+
91+
async function del(req, res, next) {
92+
try {
93+
const id = parseInt(req.params.id, 10);
94+
95+
if (isNaN(id)) {
96+
res.status(400).json({error: 'Missing or invalid file id'});
97+
return;
98+
}
99+
100+
const success = await files.delete(id);
101+
102+
if (success) {
103+
res.status(204).end();
104+
} else {
105+
res.status(404).end();
106+
}
107+
} catch (err) {
108+
next(err);
109+
}
110+
}
111+
112+
module.exports.delete = del;

0 commit comments

Comments
 (0)