From becc6b8df00c431036b12d0b0a0eacefbbd15c56 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sun, 5 Jan 2020 23:48:50 -0500 Subject: [PATCH 1/3] Add zlib compression to the "text" field. Now, when a file is saved, the original text field is blanked out and the "compressed" binary version is saved instead. Viewing files in Edit page unzips and views them just fine. Does not compress old files unless they are opened and resaved by someone. Have not tested on the "share" or "print" pages yet, but should work. --- server/homebrew.api.js | 4 ++++ server/homebrew.model.js | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index c8c0e9122..acb57c68a 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -1,6 +1,7 @@ const _ = require('lodash'); const HomebrewModel = require('./homebrew.model.js').model; const router = require('express').Router(); +const zlib = require('zlib'); // const getTopBrews = (cb)=>{ // HomebrewModel.find().sort({ views: -1 }).limit(5).exec(function(err, brews) { @@ -47,6 +48,9 @@ router.put('/api/update/:id', (req, res)=>{ HomebrewModel.get({ editId: req.params.id }) .then((brew)=>{ brew = _.merge(brew, req.body); + brew.textBin = zlib.deflateSync(req.body.text); // Compress brew text to binary before saving + brew.text = ''; // Clear out the non-binary text field so its not saved twice + brew.updatedAt = new Date(); if(req.account) brew.authors = _.uniq(_.concat(brew.authors, req.account.username)); diff --git a/server/homebrew.model.js b/server/homebrew.model.js index 587ca37c1..b9cf00319 100644 --- a/server/homebrew.model.js +++ b/server/homebrew.model.js @@ -1,12 +1,14 @@ const mongoose = require('mongoose'); const shortid = require('shortid'); const _ = require('lodash'); +const zlib = require('zlib'); const HomebrewSchema = mongoose.Schema({ shareId : { type: String, default: shortid.generate, index: { unique: true } }, editId : { type: String, default: shortid.generate, index: { unique: true } }, title : { type: String, default: '' }, text : { type: String, default: '' }, + textBin : { type: Buffer }, description : { type: String, default: '' }, tags : { type: String, default: '' }, @@ -51,6 +53,10 @@ HomebrewSchema.statics.get = function(query){ return new Promise((resolve, reject)=>{ Homebrew.find(query, (err, brews)=>{ if(err || !brews.length) return reject('Can not find brew'); + if(!_.isUndefined(brews[0].textBin)) { // Uncompress zipped text field + unzipped = zlib.unzipSync(brews[0].textBin); + brews[0].text = unzipped.toString(); + } return resolve(brews[0]); }); }); From 399a6d82f69c054afc798e167fc35fd08c89e3c1 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sun, 5 Jan 2020 23:59:42 -0500 Subject: [PATCH 2/3] lint --- server/homebrew.api.js | 2 +- server/homebrew.model.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index acb57c68a..d7a74c42d 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -50,8 +50,8 @@ router.put('/api/update/:id', (req, res)=>{ brew = _.merge(brew, req.body); brew.textBin = zlib.deflateSync(req.body.text); // Compress brew text to binary before saving brew.text = ''; // Clear out the non-binary text field so its not saved twice - brew.updatedAt = new Date(); + if(req.account) brew.authors = _.uniq(_.concat(brew.authors, req.account.username)); brew.markModified('authors'); diff --git a/server/homebrew.model.js b/server/homebrew.model.js index b9cf00319..e4385205a 100644 --- a/server/homebrew.model.js +++ b/server/homebrew.model.js @@ -8,7 +8,7 @@ const HomebrewSchema = mongoose.Schema({ editId : { type: String, default: shortid.generate, index: { unique: true } }, title : { type: String, default: '' }, text : { type: String, default: '' }, - textBin : { type: Buffer }, + textBin : { type: Buffer }, description : { type: String, default: '' }, tags : { type: String, default: '' }, From 486841084f78a44fa73f40060fa7cc653ae9d4dd Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 10 Jan 2020 08:59:39 -0500 Subject: [PATCH 3/3] Make compression raw (no wrapper). Otherwise small files are actually larger after compression. --- server/homebrew.api.js | 2 +- server/homebrew.model.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/homebrew.api.js b/server/homebrew.api.js index d7a74c42d..346c6fe00 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -48,7 +48,7 @@ router.put('/api/update/:id', (req, res)=>{ HomebrewModel.get({ editId: req.params.id }) .then((brew)=>{ brew = _.merge(brew, req.body); - brew.textBin = zlib.deflateSync(req.body.text); // Compress brew text to binary before saving + brew.textBin = zlib.deflateRawSync(req.body.text); // Compress brew text to binary before saving brew.text = ''; // Clear out the non-binary text field so its not saved twice brew.updatedAt = new Date(); diff --git a/server/homebrew.model.js b/server/homebrew.model.js index e4385205a..e8d857982 100644 --- a/server/homebrew.model.js +++ b/server/homebrew.model.js @@ -54,7 +54,7 @@ HomebrewSchema.statics.get = function(query){ Homebrew.find(query, (err, brews)=>{ if(err || !brews.length) return reject('Can not find brew'); if(!_.isUndefined(brews[0].textBin)) { // Uncompress zipped text field - unzipped = zlib.unzipSync(brews[0].textBin); + unzipped = zlib.inflateRawSync(brews[0].textBin); brews[0].text = unzipped.toString(); } return resolve(brews[0]);