-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
121 lines (104 loc) · 3.06 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
const ipfsClient = require('ipfs-http-client');
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs')
var app = express();
app.use(express.static('public'));
// Port website will run on
app.listen(8080);
//upload to ipfs
function ipfs() {
const ipfs = new ipfsClient([host = 'localhost', port = '5001', protocol = 'http']);
app.set('view engine', 'html')
app.use(fileUpload());
app.post('/public/Upload', (req, res) => {
const file = req.files.file;
const fileName = req.body.fileName;
const filePath = 'files/' + fileName;
file.mv(filePath, async (err) => {
if(err){
console.log('Error: failed to download the file');
return res.status(500).send(err);
}
const fileHash = await addFile(fileName, filePath);
fs.unlink(filePath, (err) => {
if(err){
console.log(err);
}
})
res.render('upload', {fileName, fileHash});
})
});
}
const addFile = async (fileName, filePath) => {
const file = fs.readFileSync(filePath);
const fileAdded = await ipfs.add({path: fileName, content: file});
const fileHash = fileAdded[0].hash;
return fileHash
}
// Connect to CockroachDB through Sequelize.
// Create the "accounts" table.
function insertion() {
const Sequelize = require("sequelize-cockroachdb");
// For secure connection:
const fs = require(['fs']);
var sequelize = new Sequelize({
dialect: "postgres",
username: "andrewzhang76",
password: "LTqvU1pow8hYE1mp",
host: "free-tier.gcp-us-central1.cockroachlabs.cloud",
port: 26257,
database: "EDU-IO.defaultdb",
dialectOptions: {
ssl: {
rejectUnauthorized: false,
// For secure connection:
ca: fs.readFileSync('certs/ca.crt')
.toString()
},
},
logging: false,
});
// Define the Account model for the "accounts" table.
const Account = sequelize.define("accounts", {
id: {
type: Sequelize.STRING,
primaryKey: true,
},
balance: {
type: Sequelize.INTEGER,
},
password:{
type: Sequelize.STRING,
}
});
Account.sync({
force: true,
})
.then(function () {
// Insert two rows into the "accounts" table.
return Account.bulkCreate([
{
id: "[email protected]",
balance: 1000,
password:"zjm010706",
},
{
id: "[email protected]",
balance: 250,
password:"huyujie1207",
},
]);
})
.then(function (accounts) {
// Print out the balances.
accounts.forEach(function (account) {
console.log(account.id + " " + account.balance);
});
process.exit(0);
})
.catch(function (err) {
console.error("error: " + err.message);
process.exit(1);
});
}