forked from cheekujodhpur/ipho2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploaded.txt
93 lines (87 loc) · 3.55 KB
/
uploaded.txt
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
app.post('/uploadedT1',function(req,res)
{
if(file_size_ex == true)
{
file_size_ex = false;
done = false;
res.redirect('/');
return;
}
if(done==true)
{
//the /tmp path of the file
var temp_path = req.files.user_file.path;
//file extension
var file_extension = req.files.user_file.extension;
//the name of the uploaded file
var file_name = req.files.user_file.name
//the new location to which the file will be copied according to
//the user's ip
var new_location = __dirname + '/uploads/'+ req.ip + '/' + file_name;
MongoClient.connect("mongodb://localhost:27017/test",function(err,db)
{
if(err)
{
console.log(err);
return 0;
}
if(req.ip != null)
{
var ip = req.ip.toString();
}
else
{
console.log("Null IP Error.Carry on");
return;
}
console.log("Connection established to the server at mongodb://localhost:27017/test in response to " + ip.toString());
var collection = db.collection('users');
collection.find({"ip":ip}).toArray(function(err,items)
{
if(items == null | items.length == 0)
{
return;
}
if(items[0].logged)
{
var type = items[0].type;
if(type == 1)
{
//LEADERS
var country_code = items[0].country_code
file_name = "T1_" + country_code.toString() + "." + file_extension;
new_location = __dirname + '/uploads/'+ req.ip + '/' + file_name;
var common_new_location = __dirname + '/common/T1'+ '/' + file_name;
//copy the file to the new_location from the temp_path
fs.copy(temp_path.toString(), new_location.toString(), function(err)
{
if (err)
{
return console.error(err);
}
//print the uploaded file metadata on the console
console.log(req.files.user_file);
console.log(file_name + " successfully copied to /uploads/" + req.ip.toString() + "/");
});
fs.copy(temp_path.toString(), common_new_location.toString(), function(err)
{
if (err)
{
return console.error(err);
}
//print the uploaded file metadata on the console
console.log(req.files.user_file);
console.log(file_name + " successfully copied to /common/T1");
});
var uploads = db.collection('uploads');
uploads.update({"ip":ip},{$set:{"T1":true}},function(err,result){db.close();});
//redirect the client to his homepage
db.close();
res.redirect('/')
done = false;
}
}
});
});
}
});