-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathstorage-object.js
More file actions
160 lines (131 loc) · 3.82 KB
/
storage-object.js
File metadata and controls
160 lines (131 loc) · 3.82 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* storage-object.js: Instance of a single rackspace cloudserver
*
* (C) 2010 Nodejitsu Inc.
* MIT LICENSE
*
*/
var fs = require('fs'),
cloudfiles = require('../cloudfiles'),
common = require('./common');
var StorageObject = exports.StorageObject = function (client, details) {
if (!details) {
throw new Error("StorageObject must be constructed with at least basic details.")
}
this.client = client;
this._setProperties(details);
};
StorageObject.prototype = {
addMetadata: function (metadata, callback) {
var self = this;
var options = {
client: this.client,
uri: this.fullPath,
method: 'POST',
headers: this._createHeaders(metadata)
};
common.rackspace(options, callback, function (body, res) {
self.metadata = metadata;
callback(null, true);
});
},
// Remark: This method is untested
copy: function (container, destination, callback) {
var copyOptions = {
method: 'PUT',
uri: this.fullPath,
headers: {
'X-COPY-DESTINATION': [container, destination].join('/'),
'CONTENT-LENGTH': this.bytes
}
};
common.rackspace(copyOptions, callback, function (body, res) {
callback(null, true);
});
},
destroy: function (callback) {
this.client.destroyFile(this.containerName, this.name, callback);
},
getMetadata: function (callback) {
var options = {
client: this.client,
uri: this.fullPath,
method: 'HEAD',
};
common.rackspace(options, callback, function (body, res) {
var metadata = {};
Object.keys(res.headers).forEach(function (header) {
var match;
if (match = header.match(/x-object-meta-(.*)/i)) {
metadata[match[1]] = res.headers[header];
}
});
callback(null, metadata);
});
},
// Remark: Not fully implemented
removeMetadata: function (keys, callback) {
var newMetadata = {};
Object.keys(this.metadata).forEach(function (key) {
if (keys.indexOf(key) !== -1) {
newMetadata[key] = this.metadata[key];
}
});
// TODO: Finish writing this method
},
save: function (options, callback) {
var self = this;
var fileStream = fs.createWriteStream(options.local, {
flags: options.flags || 'w+',
encoding: options.encoding || null,
mode: options.mode || 0666
});
fs.readFile(this.local, function (err, data) {
if (err) {
return callback(err);
}
function endWrite() {
fileStream.end();
callback(null, options.local);
}
var written = false;
fileStream.on('drain', function () {
if (!written) {
endWrite();
}
});
written = fileStream.write(data);
if (written) {
endWrite();
}
});
},
update: function (data, callback) {
},
get fullPath() {
return this.client.storageUrl(this.containerName, this.name);
},
get containerName() {
return this.container instanceof cloudfiles.Container ? this.container.name : this.container;
},
_setProperties: function (details) {
// TODO: Should probably take this in from details or something.
this.metadata = {};
this.container = details.container || null;
this.name = details.name || null;
this.etag = details.etag || null;
this.hash = details.hash || null;
this.bytes = details.bytes || null;
this.local = details.local || null;
this.contentType = details.content_type || null;
this.lastModified = details.last_modified || null;
},
_createHeaders: function (metadata) {
var headers = {};
Object.keys(metadata).forEach(function (key) {
var header = "x-object-meta-" + key;
headers[header] = metadata[key];
});
return headers;
}
};