progress made for handling media by configuration
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -225,4 +225,4 @@ demo**/*.css
|
|||||||
dist
|
dist
|
||||||
|
|
||||||
# Ignore demo/uploads
|
# Ignore demo/uploads
|
||||||
demo/uploads
|
demo/media
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ module.exports = {
|
|||||||
staticDir: 'demo/media',
|
staticDir: 'demo/media',
|
||||||
type: 'image',
|
type: 'image',
|
||||||
accept: ['.jpg', '.jpeg', '.png'],
|
accept: ['.jpg', '.jpeg', '.png'],
|
||||||
|
// TODO: discuss if some sizes are required, what if not resizing?
|
||||||
imageSizes: [
|
imageSizes: [
|
||||||
{
|
{
|
||||||
name: 'tablet',
|
name: 'tablet',
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ module.exports = {
|
|||||||
name: 'slides',
|
name: 'slides',
|
||||||
label: 'Slides',
|
label: 'Slides',
|
||||||
type: 'repeater',
|
type: 'repeater',
|
||||||
|
id: false,
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
name: 'content',
|
name: 'content',
|
||||||
|
|||||||
22
src/index.js
22
src/index.js
@@ -5,7 +5,8 @@ import bodyParser from 'body-parser';
|
|||||||
import methodOverride from 'method-override';
|
import methodOverride from 'method-override';
|
||||||
import jwtStrategy from './auth/jwt';
|
import jwtStrategy from './auth/jwt';
|
||||||
import fileUpload from 'express-fileupload';
|
import fileUpload from 'express-fileupload';
|
||||||
// import mediaRoutes from './media/media.routes';
|
import {upload as uploadMedia, update as updateMedia} from './media/requestHandlers';
|
||||||
|
import mediaConfig from './media/media.config';
|
||||||
import emailRoutes from './auth/passwordResets/email.routes';
|
import emailRoutes from './auth/passwordResets/email.routes';
|
||||||
import autopopulate from './mongoose/autopopulate.plugin';
|
import autopopulate from './mongoose/autopopulate.plugin';
|
||||||
import paginate from './mongoose/paginate.plugin';
|
import paginate from './mongoose/paginate.plugin';
|
||||||
@@ -21,7 +22,6 @@ import fieldToSchemaMap from './mongoose/fieldToSchemaMap';
|
|||||||
import authValidate from './auth/validate';
|
import authValidate from './auth/validate';
|
||||||
import authRequestHandlers from './auth/requestHandlers';
|
import authRequestHandlers from './auth/requestHandlers';
|
||||||
import passwordResetConfig from './auth/passwordResets/passwordReset.config';
|
import passwordResetConfig from './auth/passwordResets/passwordReset.config';
|
||||||
import mediaConfig from './media/media.config';
|
|
||||||
import validateCollection from './utilities/validateCollection';
|
import validateCollection from './utilities/validateCollection';
|
||||||
import validateGlobal from './utilities/validateGlobal';
|
import validateGlobal from './utilities/validateGlobal';
|
||||||
import setModelLocaleMiddleware from './mongoose/setModelLocale.middleware';
|
import setModelLocaleMiddleware from './mongoose/setModelLocale.middleware';
|
||||||
@@ -43,7 +43,7 @@ class Payload {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
options.app.use(fileUpload());
|
options.app.use(fileUpload({}));
|
||||||
const staticUrl = options.config.staticUrl ? options.config.staticUrl : `/${options.config.staticDir}`;
|
const staticUrl = options.config.staticUrl ? options.config.staticUrl : `/${options.config.staticDir}`;
|
||||||
options.app.use(staticUrl, express.static(options.config.staticDir));
|
options.app.use(staticUrl, express.static(options.config.staticDir));
|
||||||
|
|
||||||
@@ -86,8 +86,7 @@ class Payload {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// media
|
// media
|
||||||
// TODO: finish this idea
|
if (config.media) {
|
||||||
if (config.media && config.media.static) {
|
|
||||||
config.fields.push(...mediaConfig.fields);
|
config.fields.push(...mediaConfig.fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,13 +151,18 @@ class Payload {
|
|||||||
setModelLocaleMiddleware()
|
setModelLocaleMiddleware()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// TODO: this feels sloppy, need to discuss media enabled collection handlers
|
||||||
|
let createHandler = config.media ? (req, res, next) => uploadMedia(req, res, next, config.media) : create;
|
||||||
|
let updateHandler = config.media ? (req, res, next) => updateMedia(req, res, next, config.media) : update;
|
||||||
|
// TODO: Do we need a delete?
|
||||||
|
|
||||||
options.router.route(`/${config.slug}`)
|
options.router.route(`/${config.slug}`)
|
||||||
.get(config.policies.read, query)
|
.get(config.policies.read, query)
|
||||||
.post(config.policies.create, create);
|
.post(config.policies.create, createHandler);
|
||||||
|
|
||||||
options.router.route(`/${config.slug}/:id`)
|
options.router.route(`/${config.slug}/:id`)
|
||||||
.get(config.policies.read, findOne)
|
.get(config.policies.read, findOne)
|
||||||
.put(config.policies.update, update)
|
.put(config.policies.update, updateHandler)
|
||||||
.delete(config.policies.destroy, destroy);
|
.delete(config.policies.destroy, destroy);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -172,10 +176,6 @@ class Payload {
|
|||||||
this.globals[config.label] = config;
|
this.globals[config.label] = config;
|
||||||
globalFields[config.slug] = {};
|
globalFields[config.slug] = {};
|
||||||
|
|
||||||
if (config.media) {
|
|
||||||
// TODO: handle media the same way as on a collection
|
|
||||||
}
|
|
||||||
|
|
||||||
config.fields.forEach(field => {
|
config.fields.forEach(field => {
|
||||||
const fieldSchema = fieldToSchemaMap[field.type];
|
const fieldSchema = fieldToSchemaMap[field.type];
|
||||||
if (fieldSchema) globalFields[config.slug][field.name] = fieldSchema(field);
|
if (fieldSchema) globalFields[config.slug][field.name] = fieldSchema(field);
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
import mongoose from 'mongoose';
|
|
||||||
import buildQueryPlugin from '../mongoose/buildQuery.plugin';
|
|
||||||
import paginate from '../mongoose/paginate.plugin';
|
|
||||||
import localizationPlugin from '../localization/localization.plugin';
|
|
||||||
|
|
||||||
const mediaModelLoader = (config) => {
|
|
||||||
const MediaSchema = new mongoose.Schema({
|
|
||||||
name: { type: String, intl: true },
|
|
||||||
caption: { type: String, intl: true },
|
|
||||||
description: { type: String, intl: true },
|
|
||||||
filename: { type: String },
|
|
||||||
sizes: [{
|
|
||||||
height: { type: Number},
|
|
||||||
width: { type: Number},
|
|
||||||
_id: false
|
|
||||||
}]
|
|
||||||
},
|
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
MediaSchema.plugin(paginate);
|
|
||||||
MediaSchema.plugin(buildQueryPlugin);
|
|
||||||
MediaSchema.plugin(localizationPlugin, config.localization);
|
|
||||||
|
|
||||||
return mongoose.model('Media', MediaSchema);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default mediaModelLoader;
|
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
export default {
|
export default {
|
||||||
fields: [{
|
fields: [
|
||||||
name: 'filename',
|
{
|
||||||
type: 'input',
|
name: 'filename',
|
||||||
},
|
type: 'input',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'sizes',
|
name: 'sizes',
|
||||||
type: 'repeater',
|
type: 'repeater',
|
||||||
|
id: false,
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
name: 'height',
|
name: 'height',
|
||||||
type: 'number',
|
type: 'number',
|
||||||
label: 'Height'
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'width',
|
name: 'width',
|
||||||
type: 'number',
|
type: 'number',
|
||||||
label: 'Width'
|
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
import express from 'express';
|
|
||||||
import passport from 'passport';
|
|
||||||
import { upload, update } from './media.controller';
|
|
||||||
import { query } from '../mongoose/requestHandlers';
|
|
||||||
import bindModelMiddleware from '../mongoose/bindModel.middleware';
|
|
||||||
import mediaModelLoader from './Media.model';
|
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
const mediaRoutes = config => {
|
|
||||||
|
|
||||||
const mediaModel = mediaModelLoader(config); // Needs config for intl
|
|
||||||
router.all('*', bindModelMiddleware(mediaModel));
|
|
||||||
|
|
||||||
router
|
|
||||||
.route('')
|
|
||||||
.post(
|
|
||||||
passport.authenticate('jwt', { session: false }),
|
|
||||||
(req, res, next) => upload(req, res, next, config)
|
|
||||||
);
|
|
||||||
|
|
||||||
router
|
|
||||||
.route('/:_id')
|
|
||||||
.put(
|
|
||||||
passport.authenticate('jwt', { session: false }),
|
|
||||||
(req, res, next) => update(req, res, next, config)
|
|
||||||
);
|
|
||||||
|
|
||||||
router.route('')
|
|
||||||
.get(
|
|
||||||
query
|
|
||||||
);
|
|
||||||
|
|
||||||
return router;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default mediaRoutes;
|
|
||||||
@@ -3,42 +3,42 @@ import { resizeAndSave } from './imageResizer';
|
|||||||
import httpStatus from 'http-status';
|
import httpStatus from 'http-status';
|
||||||
import modelById from '../mongoose/resolvers/modelById';
|
import modelById from '../mongoose/resolvers/modelById';
|
||||||
|
|
||||||
export async function update(req, res, next, config) {
|
const update = async (req, res, next, config) => {
|
||||||
req.model.setDefaultLocale(req.locale);
|
req.model.setDefaultLocale(req.locale);
|
||||||
|
|
||||||
const query = {
|
const query = {
|
||||||
Model: req.model,
|
Model: req.model,
|
||||||
id: req.params._id,
|
id: req.params._id,
|
||||||
locale: req.locale,
|
locale: req.locale,
|
||||||
};
|
};
|
||||||
let doc = await modelById(query, { returnRawDoc: true });
|
let doc = await modelById(query, {returnRawDoc: true});
|
||||||
if (!doc)
|
if (!doc)
|
||||||
return res.status(httpStatus.NOT_FOUND).send('Not Found');
|
return res.status(httpStatus.NOT_FOUND).send('Not Found');
|
||||||
|
|
||||||
Object.keys(req.body).forEach(e => {
|
Object.keys(req.body).forEach(e => {
|
||||||
doc[e] = req.body[e];
|
doc[e] = req.body[e];
|
||||||
});
|
|
||||||
|
|
||||||
if (req.files && req.files.file) {
|
|
||||||
doc['filename'] = req.files.file.name;
|
|
||||||
let outputFilepath = `${config.staticDir}/${req.files.file.name}`;
|
|
||||||
let moveError = await req.files.file.mv(outputFilepath);
|
|
||||||
if (moveError) return res.status(500).send(moveError);
|
|
||||||
doc['sizes'] = await resizeAndSave(config, req.files.file);
|
|
||||||
}
|
|
||||||
|
|
||||||
doc.save((saveError) => {
|
|
||||||
if (saveError)
|
|
||||||
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: saveError });
|
|
||||||
|
|
||||||
return res.json({
|
|
||||||
message: 'success',
|
|
||||||
result: doc.toJSON({ virtuals: true })
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function upload(req, res, next, config) {
|
if (req.files && req.files.file) {
|
||||||
|
doc['filename'] = req.files.file.name;
|
||||||
|
let outputFilepath = `${config.staticDir}/${req.files.file.name}`;
|
||||||
|
let moveError = await req.files.file.mv(outputFilepath);
|
||||||
|
if (moveError) return res.status(500).send(moveError);
|
||||||
|
doc['sizes'] = await resizeAndSave(config, req.files.file);
|
||||||
|
}
|
||||||
|
|
||||||
|
doc.save((saveError) => {
|
||||||
|
if (saveError)
|
||||||
|
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: saveError});
|
||||||
|
|
||||||
|
return res.json({
|
||||||
|
message: 'success',
|
||||||
|
result: doc.toJSON({virtuals: true})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const upload = async (req, res, next, config) => {
|
||||||
if (!req.files || Object.keys(req.files).length === 0) {
|
if (!req.files || Object.keys(req.files).length === 0) {
|
||||||
return res.status(400).send('No files were uploaded.');
|
return res.status(400).send('No files were uploaded.');
|
||||||
}
|
}
|
||||||
@@ -63,12 +63,14 @@ export async function upload(req, res, next, config) {
|
|||||||
sizes: outputSizes
|
sizes: outputSizes
|
||||||
}, (mediaCreateError, result) => {
|
}, (mediaCreateError, result) => {
|
||||||
if (mediaCreateError)
|
if (mediaCreateError)
|
||||||
return res.status(500).json({ error: mediaCreateError });
|
return res.status(500).json({error: mediaCreateError});
|
||||||
|
|
||||||
return res.status(201)
|
return res.status(201)
|
||||||
.json({
|
.json({
|
||||||
message: 'success',
|
message: 'success',
|
||||||
result: result.toJSON({ virtuals: true })
|
result: result.toJSON({virtuals: true})
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export {update, upload};
|
||||||
|
|||||||
Reference in New Issue
Block a user