138 lines
3.3 KiB
JavaScript
138 lines
3.3 KiB
JavaScript
|
|
import { Meteor } from 'meteor/meteor';
|
||
|
|
import { Mongo } from 'meteor/mongo';
|
||
|
|
import { check } from 'meteor/check';
|
||
|
|
import SimpleSchema from 'simpl-schema';
|
||
|
|
|
||
|
|
let Slideshow = new Mongo.Collection('Slideshow');
|
||
|
|
let SlideshowImage = new Mongo.Collection("SlideshowImage");
|
||
|
|
|
||
|
|
Slideshow.attachSchema(new SimpleSchema({
|
||
|
|
name: {
|
||
|
|
type: String,
|
||
|
|
label: "Name",
|
||
|
|
optional: false,
|
||
|
|
trim: true,
|
||
|
|
index: 1,
|
||
|
|
unique: true
|
||
|
|
},
|
||
|
|
images: { //A JSON array of image URL's.
|
||
|
|
type: Array,
|
||
|
|
label: "Images",
|
||
|
|
optional: false,
|
||
|
|
defaultValue: []
|
||
|
|
},
|
||
|
|
'images.$': {
|
||
|
|
type: String
|
||
|
|
},
|
||
|
|
createdAt: {
|
||
|
|
type: Date,
|
||
|
|
label: "Created On",
|
||
|
|
optional: true
|
||
|
|
},
|
||
|
|
updatedAt: {
|
||
|
|
type: Date,
|
||
|
|
label: "Updated On",
|
||
|
|
optional: true
|
||
|
|
}
|
||
|
|
}));
|
||
|
|
|
||
|
|
SlideshowImage.attachSchema(new SimpleSchema({
|
||
|
|
image: {
|
||
|
|
type: String,
|
||
|
|
label: "Image",
|
||
|
|
optional: false,
|
||
|
|
trim: false
|
||
|
|
}
|
||
|
|
}));
|
||
|
|
|
||
|
|
if(Meteor.isServer) Meteor.publish('slideshow', function() {
|
||
|
|
return Slideshow.find({});
|
||
|
|
});
|
||
|
|
|
||
|
|
if(Meteor.isServer) {
|
||
|
|
WebApp.connectHandlers.use("/slideshow-image/", (req, res, next) => {
|
||
|
|
try {
|
||
|
|
let id = req.url.substr(1);
|
||
|
|
|
||
|
|
let slide = SlideshowImage.findOne(id);
|
||
|
|
|
||
|
|
if(slide) {
|
||
|
|
let i = slide.image.indexOf(";base64,");
|
||
|
|
let header = slide.image.substr(5, i);
|
||
|
|
let content = slide.image.substr(i + 8);
|
||
|
|
let buffer = new Buffer(content, 'base64');
|
||
|
|
|
||
|
|
res.writeHead(200, {'Content-Type': header});
|
||
|
|
res.write(buffer);
|
||
|
|
res.end();
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
//TODO: Write out a bad image picture.
|
||
|
|
}
|
||
|
|
} catch(err) {
|
||
|
|
console.log(err);
|
||
|
|
res.end();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
Meteor.methods({
|
||
|
|
addSlideshowImage: function(slideshowId, image) {
|
||
|
|
check(slideshowId, String);
|
||
|
|
check(image, String);
|
||
|
|
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
let id = SlideshowImage.insert({image});
|
||
|
|
|
||
|
|
if(id) {
|
||
|
|
Slideshow.update(slideshowId, {$push: {images: id}});
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
throw new Meteor.Error(400, "Image storage failed.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
},
|
||
|
|
removeSlideshowImage: function(slideshowId, imageId) {
|
||
|
|
check(slideshowId, String);
|
||
|
|
check(imageId, String);
|
||
|
|
|
||
|
|
//Note: there is currently no way in mongo to remove an element at an index.
|
||
|
|
Slideshow.update(slideshowId, {$pull: {images: imageId}});
|
||
|
|
},
|
||
|
|
swapSlideshowImages: function(slideshowId, firstIndex, secondIndex) {
|
||
|
|
check(slideshowId, String);
|
||
|
|
check(firstIndex, Number);
|
||
|
|
check(secondIndex, Number);
|
||
|
|
|
||
|
|
let slideshow = Slideshow.findOne(slideshowId);
|
||
|
|
let temp = slideshow.images[firstIndex];
|
||
|
|
|
||
|
|
slideshow.images[firstIndex] = slideshow.images[secondIndex];
|
||
|
|
slideshow.images[secondIndex] = temp;
|
||
|
|
|
||
|
|
Slideshow.update(slideshowId, {$set: {images: slideshow.images}});
|
||
|
|
},
|
||
|
|
addSlideshow: function(name) {
|
||
|
|
check(name, String);
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
const createdAt = new Date();
|
||
|
|
const updatedAt = createdAt;
|
||
|
|
const images = [];
|
||
|
|
|
||
|
|
//Returns the id of the object created.
|
||
|
|
return Slideshow.insert({name, images, createdAt, updatedAt});
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
},
|
||
|
|
removeSlideshow: function(_id) {
|
||
|
|
check(_id, String);
|
||
|
|
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||
|
|
Slideshow.remove({_id});
|
||
|
|
}
|
||
|
|
else throw new Meteor.Error(403, "Not authorized.");
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export default Slideshow;
|