Skip to content

Commit

Permalink
Snapshots rework
Browse files Browse the repository at this point in the history
Changed the way snapshots are handeld. They are now stored as binary files on the mongodb and not as base64 strings

Fixed the "Get Snapshot" response
Fixed a bug where the "Get Snapshot" request closes all connections
  • Loading branch information
OlliSchu committed Sep 29, 2020
1 parent 618c067 commit a0021a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
4 changes: 2 additions & 2 deletions api/2.1/Documents/Controller/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var cache = undefined;

function setConnection(id) {

connection = cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
connection = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
Expand Down Expand Up @@ -107,7 +107,7 @@ exports.documents_post = (req, res, next) => {
req.on("end", function() {

req.rawBody = data;

console.log(data)
const document = new Document({
_id: new mongoose.Types.ObjectId(),
guid: uuid.v4(),
Expand Down
59 changes: 24 additions & 35 deletions api/2.1/Viewpoints/Controller/viewpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var cache = undefined;

function setConnection(id) {

connection = cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
connection = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
Expand Down Expand Up @@ -48,33 +48,11 @@ exports.viewpoints_get = async (req, res, next) => {
Comments = conn.model("Comments", require("../../Comments/Models/comments"))
module.exports = conn;

var viewpointsArr = []

await Comments.find({topic_guid: topicId})
.select("viewpoint_guid -_id")
.exec()
.then(doc => {
for(var key in doc) {
data = doc[key]["viewpoint_guid"]
// console.log(data)
if (data) {
viewpointsArr.push(data)

}

}
})
.catch(err => {
res.status(500).json({
error: err
});
});
// console.log(viewpointsArr)

Viewpoints.find({guid: { $in: viewpointsArr}})
.select("index guid orthogonal_camera perspective_camera lines clipping_planes bitmaps snapshot components -_id")
Viewpoints.find({topic_guid: topicId})
.select("index guid orthogonal_camera perspective_camera lines clipping_planes bitmaps components -_id")
.exec()
.then(doc => {
//console.log(doc)
res.status(200).json(doc);
})
.catch(err => {
Expand Down Expand Up @@ -138,21 +116,21 @@ exports.viewpoint_get_snapshot = (req, res, next) => {


Viewpoints.findOne({guid: viewpointId})
.select("snapshot -_id")
.select("-_id")
.exec()
.then(doc => {

var data = doc.snapshot.snapshot_data;
var buff = new Buffer.from(data, "base64")

res.status(200).type("png").send(buff);
var buff = new Buffer.from(data.toString(),"base64")
console.log(buff)
res.status(200).send(buff);

})
.catch(err => {
res.status(500).json({
error: err
});
});
mongoose.connection.close()
};

exports.viewpoint_get_bitmap = (req, res, next) => {
Expand Down Expand Up @@ -305,8 +283,18 @@ exports.viewpoint_create = (req, res, next) => {
}

// console.log(bitmapsArr)
var baseString = req.body.snapshot.snapshot_data

var data = new Buffer.from(baseString)


const snapshot = {
snapshot_type: req.body.snapshot.snapshot_type,
snapshot_data: data
}

const comment = new Viewpoints({
res.status(200);
const viewpoint = new Viewpoints({
_id: new mongoose.Types.ObjectId(),
guid: uuid.v4(),
date: timestamp,
Expand All @@ -315,11 +303,12 @@ exports.viewpoint_create = (req, res, next) => {
lines: req.body.lines,
clipping_planes: req.body.clipping_planes,
bitmaps: bitmapsArr,
snapshot: req.body.snapshot,
components: req.body.components
snapshot: snapshot,
components: req.body.components,
topic_guid: topicId
});

comment
viewpoint
.save()
.then(result => {
//console.log(result);
Expand Down
15 changes: 12 additions & 3 deletions api/2.1/Viewpoints/Models/viewpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,14 @@ const snapshotSchema = new Schema({
type: String,
enum: ["jpg", "png"]
},
snapshot_data: {
/*snapshot_data: {
type: String,
format: "base64"
}
}*/
snapshot_data: {
type: Buffer,
required: true
},
}, {_id: false});

const component_listSchema = [ componentSchema ];
Expand Down Expand Up @@ -183,7 +187,12 @@ const viewpointsSchema = new Schema({

snapshot: snapshotSchema,

components: componentsSchema
components: componentsSchema,

topic_guid: {
type: String,
required: true
},

});

Expand Down

0 comments on commit a0021a7

Please sign in to comment.