/
remotefile.js
101 lines (79 loc) · 2.46 KB
/
remotefile.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
/* remotefile.js
*
* ++[black[Atomic OS Class: HxRemoteFile]++
*/
var HxRemoteFile = HxFile.extend({
init: function(opts) {
this._super(opts);
this.dev = system.fs.tree.dev.tree.net;
this.read();
// if the buffer is empty after reading from the server then the file likely doesn't exist. create it on the server
if (this.buffer == '') {
this.dev.send(
{
cmd: 'file',
subcmd: 'create',
path: this.name
},
function(response) {
//FIXME: parsing the response is causing an unexpected token 'u' ??
// it doesn't prevent creation so ignoring for now
//console.warn('>>' + response + '<<');
try {
var r = JSON.parse(r);
console.log('HxRemoteFile.init: remote file creation response:');
console.dir(r);
} catch(e) {
// console.log('HxRemoteFile.init: remote file creation: ERROR PARSING RESPONSE:');
// console.dir(e);
}
}
);
}
},
read: function() {
path = this.name;
var self = this;
fileAction = {
cmd: 'file',
subcmd: 'read',
path: path
};
this.dev.send(fileAction, function(response) {
var r = JSON.parse(response);
// fill buffer
self.buffer = r.data;
});
return this.buffer || false;
},
write: function(buf) {
this.buffer = buf;
var path = this.name;
fileAction = {
cmd: 'file',
subcmd: 'write',
path: path,
buffer: buf
};
this.dev.send(fileAction, function(response) {
var r = JSON.parse(response);
console.dir(r);
});
return this;
},
append: function(buf) {
this.buffer += buf;
var path = this.name;
fileAction = {
cmd: 'file',
subcmd: 'append',
path: path,
buffer: buf
};
this.dev.send(fileAction, function(response) {
var r = JSON.parse(response);
console.dir(r);
});
return this;
}
});