-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubs_provider.js
90 lines (83 loc) · 2.29 KB
/
subs_provider.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
//var mongo = require('mongojs')
//var crypto = require('crypto');
var fs = require('fs');
SubsProvider = function(host, port) {
this.MAX_FILE_SIZE = 33333;
this.ended = {2:true,3:true,5:true,6:true}; // Store homework endedness
this.homeworks = {2:4,3:7,5:5,6:10}; // Store homework sizes
};
SubsProvider.prototype.is_valid_number = function(n){
return !isNaN(parseFloat(n)) && isFinite(n) && parseInt(Number(n)) == Number(n);
}
SubsProvider.prototype.gen_path = function(user_data, hw_data){
console.log("DATA",hw_data);
var username = user_data.username;
var hwid = hw_data.hwid;
var pid = hw_data.pid;
if(!this.is_valid_number(hwid) || !this.is_valid_number(pid)) return "";
hwid = parseInt(hwid);
pid = parseInt(pid);
try {
console.log("making dir");
fs.mkdirSync("sub_data/"+username+"/");
console.log("made");
} catch(e) {
if (e.code != 'EEXIST'){
console.log("EXC: ",e);
throw e;
}
}
if(hwid in this.homeworks && 0 <= pid && pid < this.homeworks[hwid]){
return "sub_data/" + username + "/" + hwid + "." + pid
}
return "";
}
SubsProvider.prototype.get = function(user_data, hw_data, callback) {
var count = 0;
if(hw_data.hwid in this.homeworks){
count = this.homeworks[hw_data.hwid];
}
else{
console.log("HWID problem", hw_data);
callback("Invalid homework ID");
return;
}
var read_data = [];
for(var i = 0; i < count; i++){
hw_data.pid = i;
var path = this.gen_path(user_data, hw_data);
if(path == ""){
console.log("File inaccessible");
callback("File read error");
return;
}
try{
var buf = fs.readFileSync(path, "ASCII");
read_data.push(buf.toString());
}
catch(e){
read_data.push("");
}
}
callback(null, read_data);
};
SubsProvider.prototype.save = function(user_data, hw_data, callback) {
var path = this.gen_path(user_data, hw_data);
if(path == "" || this.ended[hw_data.hwid]){
console.log("Uh oh");
callback("Homework has ended");
return;
}
var text = hw_data.text;
if(text.length > this.MAX_FILE_SIZE){
callback("is too large");
}
fs.writeFile(path, text, function(error){
console.log(error, " E ");
if(error)
callback("An error occurred");
else
callback(null, "success");
});
};
exports.SubsProvider = SubsProvider;