Skip to content

Commit 6c8d9f9

Browse files
committed
New improvements.
1 parent d972e05 commit 6c8d9f9

4 files changed

Lines changed: 123 additions & 1 deletion

File tree

changes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
- added: `F.config['secret-uid']` as a hidden secret for encrypting/decrypting values
9999
- added: `F.dir(path)` for changing of root directory
100100
- added: `NOSQL()/TABLE().memory(count, [size])` for memory consumption, more in docs
101+
- added: `HttpFile.fs(storage_name, [custom], [callback])` saves a file into the FileStorage
102+
- added: `res.filefs(storage_name, id, [download], [headers], [callback])` returns file from FileStorage
101103

102104
- updated: (IMPORTANT) NoSQL binary divides files to independent directories for 1000 files per directory
103105
- updated: `GROUP()` by adding a new argument `url_prefix`

index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15510,6 +15510,26 @@ function extend_response(PROTO) {
1551015510
return this.$file();
1551115511
};
1551215512

15513+
/**
15514+
* Responds with a file from FileStorage
15515+
* @param {String} name A name of FileStorage
15516+
* @param {String/Number} id
15517+
* @param {String} download Optional, a download name.
15518+
* @param {Object} headers Optional, additional headers.
15519+
* @param {Function} done Optional, callback.
15520+
* @return {Framework}
15521+
*/
15522+
PROTO.filefs = function(name, id, download, headers, callback) {
15523+
var self = this;
15524+
var options = {};
15525+
options.id = id;
15526+
options.download = download;
15527+
options.headers = headers;
15528+
options.done = callback;
15529+
FILESTORAGE(name).res(self, options, $file_notmodified);
15530+
return self;
15531+
};
15532+
1551315533
/**
1551415534
* Responds with a stream
1551515535
* @param {String} contentType
@@ -16315,7 +16335,7 @@ function $file_notmodified(res, name) {
1631516335
if (res.getHeader('Last-Modified'))
1631616336
delete headers['Last-Modified'];
1631716337
else
16318-
headers['Last-Modified'] = name[2];
16338+
headers['Last-Modified'] = name instanceof Array ? name[2] : name;
1631916339

1632016340
if (res.getHeader('Expires'))
1632116341
delete headers.Expires;

internal.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,16 @@ HttpFile.prototype.image = function(im) {
899899
return framework_image.init(this.path, im, this.width, this.height);
900900
};
901901

902+
HttpFile.prototype.fs = function(storagename, custom, callback) {
903+
904+
if (typeof(custom) === 'function') {
905+
callback = custom;
906+
custom = null;
907+
}
908+
909+
return FILESTORAGE(storagename).insert(this.filename, Fs.createReadStream(this.path), custom, callback);
910+
};
911+
902912
// *********************************************************************************
903913
// =================================================================================
904914
// JS CSS + AUTO-VENDOR-PREFIXES

nosql.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,6 +4877,96 @@ Binary.prototype.update = function(id, name, buffer, custom, callback) {
48774877
return cacheid;
48784878
};
48794879

4880+
Binary.prototype.meta = function(id, callback, count) {
4881+
4882+
var self = this;
4883+
4884+
if (count > 3) {
4885+
callback(new Error('File not found.'));
4886+
return self;
4887+
}
4888+
4889+
var isnew = false;
4890+
4891+
if (id > 0)
4892+
isnew = true;
4893+
else if (id[0] === 'B' || id[0] === 'b') {
4894+
id = +id.substring(id.length - DIRECTORYLENGTH);
4895+
isnew = true;
4896+
} else if (id.indexOf('#') === -1)
4897+
id = self.db.name + '#' + id;
4898+
4899+
var filename;
4900+
4901+
if (isnew) {
4902+
filename = Path.join(self.$directory(id), id.toString().padLeft(DIRECTORYLENGTH, '0') + self.ext);
4903+
} else
4904+
filename = framework_utils.join(self.directory, id + self.ext);
4905+
4906+
var stream = Fs.createReadStream(filename, BINARYREADMETA);
4907+
stream.on('error', err => callback(err));
4908+
stream.on('data', function(buffer) {
4909+
var json = buffer.toString('utf8').replace(REGCLEAN, '');
4910+
if (json) {
4911+
callback(null, JSON.parse(json, jsonparser));
4912+
CLEANUP(stream);
4913+
} else
4914+
setTimeout(readfileattempt, 100, self, id, callback, count || 1);
4915+
});
4916+
4917+
return self;
4918+
};
4919+
4920+
Binary.prototype.res = function(res, options, notmodified) {
4921+
4922+
var self = this;
4923+
var isnew = false;
4924+
var id = options.id || '';
4925+
4926+
if (id > 0)
4927+
isnew = true;
4928+
else if (id[0] === 'B' || id[0] === 'b') {
4929+
id = +id.substring(id.length - DIRECTORYLENGTH);
4930+
isnew = true;
4931+
} else if (id.indexOf('#') === -1)
4932+
id = self.db.name + '#' + id;
4933+
4934+
var filename;
4935+
4936+
if (isnew)
4937+
filename = Path.join(self.$directory(id), id.toString().padLeft(DIRECTORYLENGTH, '0') + self.ext);
4938+
else
4939+
filename = framework_utils.join(self.directory, id + self.ext);
4940+
4941+
var stream = Fs.createReadStream(filename, BINARYREADMETA);
4942+
stream.on('error', () => res.throw404());
4943+
stream.on('data', function(buffer) {
4944+
var json = buffer.toString('utf8').replace(REGCLEAN, '');
4945+
if (json) {
4946+
var obj = JSON.parse(json, jsonparser);
4947+
var utc = obj.date ? new Date(+obj.date.substring(0, 4), +obj.date.substring(4, 6), +obj.date.substring(6, 8)).toUTCString() : '';
4948+
if (!options.download && res.req.headers['if-modified-since'] === utc) {
4949+
res.extention = U.getExtension(obj.name);
4950+
notmodified(res, utc);
4951+
} else {
4952+
res.options.type = obj.type;
4953+
res.options.stream = Fs.createReadStream(filename, BINARYREADDATA);
4954+
if (!options.download) {
4955+
if (!options.headers)
4956+
options.headers = {};
4957+
options.headers['Last-Modified'] = utc;
4958+
} else
4959+
res.options.download = options.download;
4960+
res.options.headers = options.headers;
4961+
res.options.done = options.done;
4962+
res.options.compress = options.nocompress ? false : true;
4963+
res.$stream();
4964+
}
4965+
} else
4966+
res.throw404();
4967+
});
4968+
};
4969+
48804970
Binary.prototype.read = function(id, callback, count) {
48814971

48824972
var self = this;

0 commit comments

Comments
 (0)