-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
140 lines (127 loc) · 3.66 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
var fs = require('fs');
var AWS = require('aws-sdk');
var path = require('path');
let bodyParser = require('body-parser');
const express = require('express')
var QRCode = require('qrcode')
QRCode.toString('I am a pony!',{type:'terminal'}, function (err, url) {
console.log(url)
})
const app = express()
const port = 3300
console.log("Initialize indexing....");
console.log("Indexing complete...")
console.log('listening for any new filesystem change...')
var json =[
{
device: "\\\\.\\PHYSICALDRIVE1",
displayName: "C:",
description: "Generic STORAGE DEVICE",
size: 7823458304,
mountpoints: [
{
path: "C:/Users/Eniyanilavan/Downloads"
}
],
raw: "\\\\.\\PHYSICALDRIVE1",
protected: true,
system: false
}
];
var downloadLocation='';
var uploaded = false;
getFilesRecursive = (folder, callback) => {
var fileContents = fs.readdirSync(folder);
var fileTree = [];
var index = 0;
var totalSize = 0;
fileContents.forEach(function(fileName) {
var err = false;
try {
var stats = fs.lstatSync(folder + "/" + fileName);
} catch (e) {
// console.log(e);
err = true;
}
if (!err) {
if (stats.isDirectory()) {
// console.log("in der",stats);
fileTree[index] = {
name:fileName,
type: "directory"
};
var children = getFilesRecursive(folder + "/" + fileName, function(
size
) {
fileTree[index].size = size;
totalSize += size;
});
fileTree[index].children = children;
} else {
totalSize += stats.size;
fileTree[index] = {
name:fileName,
size: stats.size,
type: "file"
};
}
}
index++;
});
if (callback) callback(totalSize);
// console.log(fileTree);
return fileTree;
};
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/getJson', (req, res) => {
for (var i in json) {
var disk = json[i];
var drive = {};
var path = disk.mountpoints[0].path + "/";
drive.info = disk;
console.log(path);
drive.content = getFilesRecursive(path, function() {
});
drive.info.used = 0;
for (var j in drive.content) {
drive.info.used += drive.content[j].size;
}
var fileContent = JSON.stringify(drive, null, 4);
var filename = disk.displayName.replace(/[^a-z0-9]/gi, "_").toLowerCase();
fs.writeFile("./" + "rootDirectory" + ".json", fileContent, function() {
});
}
res.send(drive);
})
AWS.config.update({
accessKeyId: "<key>",
secretAccessKey: "<key>"
});
app.post("/uploadFile", (req, res) => {
var s3 = new AWS.S3();
var filePath = req.body.filepath;
var file = req.body.file;
console.log(filePath);
s3.upload({ Bucket: 'remote-file-share',ACL:'public-read', Body : fs.createReadStream(filePath), Key : file }, function (err, data) {
if (err) {
console.log("Error", err);
}
if (data) {
console.log("Uploaded in:", data.Location);
downloadLocation = data.Location;
uploaded = true;
res.send({"location":data.Location});
}
}).on('httpUploadProgress', function(evt) {
console.log('Completed ' + (evt.loaded * 100 / evt.total).toFixed() + '% of upload');
});
});
app.get('/getfile',(req,res)=>{
if(uploaded){
uploaded = false;
res.send({"location":downloadLocation});
}
res.send({"location":"not uploaded yet"});
})
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))