mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-04 22:38:43 +00:00
Merge pull request #5112 from dbolack-ab/issue_5048
Move non-stubbed text compression/decompression to the model/schema
This commit is contained in:
+2
-15
@@ -2,7 +2,6 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { model as HomebrewModel } from './homebrew.model.js';
|
import { model as HomebrewModel } from './homebrew.model.js';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import zlib from 'zlib';
|
|
||||||
import GoogleActions from './googleActions.js';
|
import GoogleActions from './googleActions.js';
|
||||||
import { hbfm } from 'marked-hbfm';
|
import { hbfm } from 'marked-hbfm';
|
||||||
import * as yaml from 'js-yaml';
|
import * as yaml from 'js-yaml';
|
||||||
@@ -268,13 +267,8 @@ const api = {
|
|||||||
if(!googleId) return;
|
if(!googleId) return;
|
||||||
newHomebrew = api.excludeStubProps(newHomebrew);
|
newHomebrew = api.excludeStubProps(newHomebrew);
|
||||||
newHomebrew.googleId = googleId;
|
newHomebrew.googleId = googleId;
|
||||||
} else {
|
|
||||||
// Compress brew text to binary before saving
|
|
||||||
newHomebrew.textBin = zlib.deflateRawSync(newHomebrew.text);
|
|
||||||
// Delete the non-binary text field since it's not needed anymore
|
|
||||||
newHomebrew.text = undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
saved = await newHomebrew.save()
|
saved = await newHomebrew.save()
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.error(err, err.toString(), err.stack);
|
console.error(err, err.toString(), err.stack);
|
||||||
@@ -428,12 +422,7 @@ const api = {
|
|||||||
|
|
||||||
if(brew.googleId) {
|
if(brew.googleId) {
|
||||||
// If the google id exists after all those actions, exclude the props that are stored in google and aren't needed for rendering the brew items
|
// If the google id exists after all those actions, exclude the props that are stored in google and aren't needed for rendering the brew items
|
||||||
brew = api.excludeStubProps(brew);
|
api.excludeStubProps(brew);
|
||||||
} else {
|
|
||||||
// Compress brew text to binary before saving
|
|
||||||
brew.textBin = zlib.deflateRawSync(brew.text);
|
|
||||||
// Delete the non-binary text field since it's not needed anymore
|
|
||||||
brew.text = undefined;
|
|
||||||
}
|
}
|
||||||
brew.updatedAt = new Date();
|
brew.updatedAt = new Date();
|
||||||
brew.version = (brew.version || 1) + 1;
|
brew.version = (brew.version || 1) + 1;
|
||||||
@@ -514,8 +503,6 @@ const api = {
|
|||||||
if(shouldDeleteGoogleBrew) {
|
if(shouldDeleteGoogleBrew) {
|
||||||
// When there are still authors remaining, we delete the google brew but store the full brew in the Homebrewery database
|
// When there are still authors remaining, we delete the google brew but store the full brew in the Homebrewery database
|
||||||
brew.googleId = undefined;
|
brew.googleId = undefined;
|
||||||
brew.textBin = zlib.deflateRawSync(brew.text);
|
|
||||||
brew.text = undefined;
|
|
||||||
}
|
}
|
||||||
brew.markModified('authors'); //Mongo will not properly update arrays without markModified()
|
brew.markModified('authors'); //Mongo will not properly update arrays without markModified()
|
||||||
await brew.save()
|
await brew.save()
|
||||||
|
|||||||
@@ -700,8 +700,16 @@ brew`);
|
|||||||
shareId : expect.any(String),
|
shareId : expect.any(String),
|
||||||
style : undefined,
|
style : undefined,
|
||||||
tags : [],
|
tags : [],
|
||||||
text : undefined,
|
text : '```metadata\n' +
|
||||||
textBin : expect.objectContaining({}),
|
'title: asdf\n' +
|
||||||
|
'bleedSize: {}\n' +
|
||||||
|
'safetySpace: {}\n' +
|
||||||
|
'trimSize: {}\n' +
|
||||||
|
'\n' +
|
||||||
|
'```\n' +
|
||||||
|
'\n' +
|
||||||
|
'asdf',
|
||||||
|
textBin : undefined,
|
||||||
theme : '5ePHB',
|
theme : '5ePHB',
|
||||||
thumbnail : '',
|
thumbnail : '',
|
||||||
title : 'asdf',
|
title : 'asdf',
|
||||||
@@ -1233,8 +1241,7 @@ brew`);
|
|||||||
expect(saveFunc).toHaveBeenCalled();
|
expect(saveFunc).toHaveBeenCalled();
|
||||||
expect(saved.authors).toEqual(['test2']);
|
expect(saved.authors).toEqual(['test2']);
|
||||||
expect(saved.googleId).toEqual(undefined);
|
expect(saved.googleId).toEqual(undefined);
|
||||||
expect(saved.text).toEqual(undefined);
|
expect(saved.text).not.toEqual(undefined);
|
||||||
expect(saved.textBin).not.toEqual(undefined);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should retain google brew and update stub when multiple authors and extra author requests deletion', async ()=>{
|
it('should retain google brew and update stub when multiple authors and extra author requests deletion', async ()=>{
|
||||||
|
|||||||
@@ -32,6 +32,13 @@ const HomebrewSchema = mongoose.Schema({
|
|||||||
lock : { type: Object, index: true }
|
lock : { type: Object, index: true }
|
||||||
}, { versionKey: false });
|
}, { versionKey: false });
|
||||||
|
|
||||||
|
HomebrewSchema.pre('save', ()=>{
|
||||||
|
if(!this.googleId) {
|
||||||
|
this.textBin = zlib.deflateRawSync(this.text);
|
||||||
|
this.text = undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
HomebrewSchema.statics.increaseView = async function(query) {
|
HomebrewSchema.statics.increaseView = async function(query) {
|
||||||
const brew = await Homebrew.findOne(query).exec();
|
const brew = await Homebrew.findOne(query).exec();
|
||||||
brew.lastViewed = new Date();
|
brew.lastViewed = new Date();
|
||||||
@@ -51,6 +58,7 @@ HomebrewSchema.statics.get = async function(query, fields=null){
|
|||||||
if(!_.isNil(brew.textBin)) { // Uncompress zipped text field
|
if(!_.isNil(brew.textBin)) { // Uncompress zipped text field
|
||||||
const unzipped = zlib.inflateRawSync(brew.textBin);
|
const unzipped = zlib.inflateRawSync(brew.textBin);
|
||||||
brew.text = unzipped.toString();
|
brew.text = unzipped.toString();
|
||||||
|
brew.textBin = undefined;
|
||||||
}
|
}
|
||||||
return brew;
|
return brew;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user