flattens upload routes into collections
This commit is contained in:
@@ -101,16 +101,16 @@ function registerCollections() {
|
||||
},
|
||||
resolve: create(collection),
|
||||
};
|
||||
}
|
||||
|
||||
this.Mutation.fields[`update${singularLabel}`] = {
|
||||
type: collection.graphQL.type,
|
||||
args: {
|
||||
id: { type: new GraphQLNonNull(GraphQLString) },
|
||||
data: { type: collection.graphQL.updateMutationInputType },
|
||||
},
|
||||
resolve: update(collection),
|
||||
};
|
||||
this.Mutation.fields[`update${singularLabel}`] = {
|
||||
type: collection.graphQL.type,
|
||||
args: {
|
||||
id: { type: new GraphQLNonNull(GraphQLString) },
|
||||
data: { type: collection.graphQL.updateMutationInputType },
|
||||
},
|
||||
resolve: update(collection),
|
||||
};
|
||||
}
|
||||
|
||||
this.Mutation.fields[`delete${singularLabel}`] = {
|
||||
type: collection.graphQL.type,
|
||||
|
||||
@@ -2,7 +2,6 @@ const mongoose = require('mongoose');
|
||||
const collectionRoutes = require('./routes');
|
||||
const buildSchema = require('./buildSchema');
|
||||
const sanitize = require('./sanitize');
|
||||
const uploadRoutes = require('../uploads/routes');
|
||||
|
||||
function registerCollections() {
|
||||
this.config.collections.forEach((collection) => {
|
||||
@@ -42,10 +41,6 @@ function registerCollections() {
|
||||
config: sanitize(this.collections, collection),
|
||||
};
|
||||
|
||||
if (collection.upload) {
|
||||
this.router.use(uploadRoutes(this.collections[collection.slug]));
|
||||
}
|
||||
|
||||
this.router.use(collectionRoutes(this.collections[collection.slug]));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
const mkdirp = require('mkdirp');
|
||||
|
||||
const executePolicy = require('../../users/executePolicy');
|
||||
const executeFieldHooks = require('../../fields/executeHooks');
|
||||
const { validateCreate } = require('../../fields/validateCreate');
|
||||
|
||||
const { MissingFile } = require('../../errors');
|
||||
const resizeAndSave = require('../../uploads/imageResizer');
|
||||
const getSafeFilename = require('../../uploads/getSafeFilename');
|
||||
|
||||
const create = async (args) => {
|
||||
try {
|
||||
// /////////////////////////////////////
|
||||
@@ -24,8 +30,47 @@ const create = async (args) => {
|
||||
|
||||
options.data = await executeFieldHooks(options, args.config.fields, args.data, 'beforeCreate', args.data);
|
||||
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 4. Execute before collection hook
|
||||
// 4. Upload and resize any files that may be present
|
||||
// /////////////////////////////////////
|
||||
|
||||
if (args.config.upload) {
|
||||
const fileData = {};
|
||||
|
||||
if (!args.req.files || Object.keys(args.req.files).length === 0) {
|
||||
throw new MissingFile();
|
||||
}
|
||||
|
||||
const { staticDir, imageSizes } = options.req.collection.config.upload;
|
||||
|
||||
try {
|
||||
await mkdirp(staticDir);
|
||||
const fsSafeName = await getSafeFilename(staticDir, options.req.files.file.name);
|
||||
|
||||
try {
|
||||
await options.req.files.file.mv(`${staticDir}/${fsSafeName}`);
|
||||
|
||||
fileData.filename = fsSafeName;
|
||||
|
||||
if (imageSizes) {
|
||||
fileData.sizes = await resizeAndSave(options.config, fsSafeName);
|
||||
}
|
||||
|
||||
options.data = {
|
||||
...options.data,
|
||||
...fileData,
|
||||
};
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 5. Execute before collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
const { beforeCreate } = args.config.hooks;
|
||||
@@ -35,12 +80,7 @@ const create = async (args) => {
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// TODO - if upload is present on the collection,
|
||||
// upload the file here
|
||||
// /////////////////////////////////////
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 5. Perform database operation
|
||||
// 6. Perform database operation
|
||||
// /////////////////////////////////////
|
||||
|
||||
const {
|
||||
@@ -64,7 +104,7 @@ const create = async (args) => {
|
||||
result = result.toJSON({ virtuals: true });
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 6. Execute after collection hook
|
||||
// 7. Execute after collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
const { afterCreate } = args.config.hooks;
|
||||
@@ -74,7 +114,7 @@ const create = async (args) => {
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 7. Return results
|
||||
// 8. Return results
|
||||
// /////////////////////////////////////
|
||||
|
||||
return result;
|
||||
|
||||
@@ -3,6 +3,9 @@ const executeFieldHooks = require('../../fields/executeHooks');
|
||||
const { NotFound } = require('../../errors');
|
||||
const validate = require('../../fields/validateUpdate');
|
||||
|
||||
const resizeAndSave = require('../../uploads/imageResizer');
|
||||
const getSafeFilename = require('../../uploads/getSafeFilename');
|
||||
|
||||
const update = async (args) => {
|
||||
try {
|
||||
// /////////////////////////////////////
|
||||
@@ -57,13 +60,45 @@ const update = async (args) => {
|
||||
result.setLocale(locale, fallbackLocale);
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 6. Upload and resize any files that may be present
|
||||
// /////////////////////////////////////
|
||||
|
||||
if (args.config.upload) {
|
||||
const fileData = {};
|
||||
|
||||
const { staticDir, imageSizes } = args.config.upload;
|
||||
|
||||
if (args.req.files || args.req.files.file) {
|
||||
const fsSafeName = await getSafeFilename(staticDir, options.req.files.file.name);
|
||||
|
||||
try {
|
||||
await options.req.files.file.mv(`${staticDir}/${fsSafeName}`);
|
||||
|
||||
fileData.filename = fsSafeName;
|
||||
|
||||
if (imageSizes) {
|
||||
fileData.sizes = await resizeAndSave(options.config, fsSafeName);
|
||||
}
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
options.data = {
|
||||
...options.data,
|
||||
...fileData,
|
||||
};
|
||||
}
|
||||
|
||||
Object.assign(result, data);
|
||||
|
||||
await result.save();
|
||||
|
||||
result = result.toJSON({ virtuals: true });
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 6. Execute after collection hook
|
||||
// 7. Execute after collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
const { afterUpdate } = args.config.hooks;
|
||||
@@ -73,7 +108,7 @@ const update = async (args) => {
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 7. Return results
|
||||
// 8. Return results
|
||||
// /////////////////////////////////////
|
||||
|
||||
return result;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
const express = require('express');
|
||||
const fileUpload = require('express-fileupload');
|
||||
|
||||
const requestHandlers = require('./requestHandlers');
|
||||
const bindCollectionMiddleware = require('./bindCollection');
|
||||
|
||||
@@ -9,8 +11,15 @@ const {
|
||||
const router = express.Router();
|
||||
|
||||
const registerRoutes = ({ Model, config }) => {
|
||||
router.all(`/${config.slug}*`,
|
||||
bindCollectionMiddleware({ Model, config }));
|
||||
const middleware = [
|
||||
bindCollectionMiddleware({ Model, config }),
|
||||
];
|
||||
|
||||
if (config.upload) {
|
||||
middleware.push(fileUpload());
|
||||
}
|
||||
|
||||
router.all(`/${config.slug}*`, ...middleware);
|
||||
|
||||
router.route(`/${config.slug}`)
|
||||
.get(find)
|
||||
|
||||
10
src/errors/MissingFile.js
Normal file
10
src/errors/MissingFile.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const httpStatus = require('http-status');
|
||||
const APIError = require('./APIError');
|
||||
|
||||
class MissingFile extends APIError {
|
||||
constructor() {
|
||||
super('No files were uploaded.', httpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MissingFile;
|
||||
@@ -7,6 +7,7 @@ const MissingUseAsTitle = require('./MissingUseAsTitle');
|
||||
const NotFound = require('./NotFound');
|
||||
const Forbidden = require('./Forbidden');
|
||||
const ValidationError = require('./ValidationError');
|
||||
const MissingFile = require('./MissingFile');
|
||||
|
||||
module.exports = {
|
||||
APIError,
|
||||
@@ -18,4 +19,5 @@ module.exports = {
|
||||
NotFound,
|
||||
Forbidden,
|
||||
ValidationError,
|
||||
MissingFile,
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ const buildModel = require('./buildModel');
|
||||
const sanitize = require('./sanitize');
|
||||
const routes = require('./routes');
|
||||
|
||||
function registerGlobals() {
|
||||
function initGlobals() {
|
||||
this.config.globals = sanitize(this.config.globals);
|
||||
|
||||
if (this.config.globals) {
|
||||
@@ -15,4 +15,4 @@ function registerGlobals() {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = registerGlobals;
|
||||
module.exports = initGlobals;
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
module.exports = {
|
||||
filename: { type: String },
|
||||
mimeType: { type: String },
|
||||
size: { type: Number },
|
||||
_id: false,
|
||||
};
|
||||
@@ -1,8 +0,0 @@
|
||||
module.exports = {
|
||||
sizes: [{
|
||||
name: { type: String },
|
||||
height: { type: Number },
|
||||
width: { type: Number },
|
||||
_id: false,
|
||||
}],
|
||||
};
|
||||
@@ -1,7 +1,6 @@
|
||||
const { promisify } = require('util');
|
||||
const fs = require('fs');
|
||||
|
||||
const sanitize = require('sanitize-filename');
|
||||
const fs = require('fs');
|
||||
|
||||
const stat = promisify(fs.stat);
|
||||
|
||||
@@ -23,24 +22,22 @@ const incrementName = (name) => {
|
||||
return `${incrementedName}.${extension}`;
|
||||
};
|
||||
|
||||
const utilities = {
|
||||
async getFileSystemSafeFileName(staticDir, desiredFilename) {
|
||||
let modifiedFilename = desiredFilename;
|
||||
const exists = async (fileName) => {
|
||||
console.log(`Checking for ${fileName} existence...`);
|
||||
try {
|
||||
await stat(fileName);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
while (await exists(`${staticDir}/${modifiedFilename}`)) {
|
||||
modifiedFilename = incrementName(modifiedFilename);
|
||||
async function getSafeFileName(staticDir, desiredFilename) {
|
||||
let modifiedFilename = desiredFilename;
|
||||
const exists = async (fileName) => {
|
||||
console.log(`Checking for ${fileName} existence...`);
|
||||
try {
|
||||
await stat(fileName);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
return modifiedFilename;
|
||||
},
|
||||
};
|
||||
};
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
while (await exists(`${staticDir}/${modifiedFilename}`)) {
|
||||
modifiedFilename = incrementName(modifiedFilename);
|
||||
}
|
||||
return modifiedFilename;
|
||||
}
|
||||
|
||||
module.exports = utilities;
|
||||
module.exports = getSafeFileName;
|
||||
@@ -1,92 +0,0 @@
|
||||
const {
|
||||
GraphQLString,
|
||||
GraphQLNonNull,
|
||||
GraphQLInt,
|
||||
} = require('graphql');
|
||||
|
||||
const formatName = require('../../graphql/utilities/formatName');
|
||||
const {
|
||||
create, find, findByID, deleteResolver, update,
|
||||
} = require('../../collections/graphql/resolvers');
|
||||
|
||||
const buildPaginatedListType = require('../../graphql/schema/buildPaginatedListType');
|
||||
|
||||
function registerUpload() {
|
||||
const {
|
||||
config: {
|
||||
labels: {
|
||||
singular,
|
||||
plural,
|
||||
},
|
||||
fields,
|
||||
},
|
||||
} = this.Upload;
|
||||
|
||||
const singularLabel = formatName(singular);
|
||||
const pluralLabel = formatName(plural);
|
||||
|
||||
this.Upload.graphQL = {};
|
||||
|
||||
this.Upload.graphQL.type = this.buildObjectType(
|
||||
singularLabel,
|
||||
fields,
|
||||
singularLabel,
|
||||
{
|
||||
id: { type: GraphQLString },
|
||||
},
|
||||
);
|
||||
|
||||
this.Upload.graphQL.whereInputType = this.buildWhereInputType(
|
||||
singularLabel,
|
||||
fields,
|
||||
singularLabel,
|
||||
);
|
||||
|
||||
this.Upload.graphQL.mutationInputType = new GraphQLNonNull(this.buildMutationInputType(
|
||||
singularLabel,
|
||||
fields,
|
||||
singularLabel,
|
||||
));
|
||||
|
||||
this.Query.fields[singularLabel] = {
|
||||
type: this.Upload.graphQL.type,
|
||||
args: {
|
||||
id: { type: GraphQLString },
|
||||
locale: { type: this.types.localeInputType },
|
||||
fallbackLocale: { type: this.types.fallbackLocaleInputType },
|
||||
},
|
||||
resolve: findByID(this.Upload),
|
||||
};
|
||||
|
||||
this.Query.fields[pluralLabel] = {
|
||||
type: buildPaginatedListType(pluralLabel, this.Upload.graphQL.type),
|
||||
args: {
|
||||
where: { type: this.Upload.graphQL.whereInputType },
|
||||
locale: { type: this.types.localeInputType },
|
||||
fallbackLocale: { type: this.types.fallbackLocaleInputType },
|
||||
page: { type: GraphQLInt },
|
||||
limit: { type: GraphQLInt },
|
||||
sort: { type: GraphQLString },
|
||||
},
|
||||
resolve: find(this.Upload),
|
||||
};
|
||||
|
||||
this.Mutation.fields[`update${singularLabel}`] = {
|
||||
type: this.Upload.graphQL.type,
|
||||
args: {
|
||||
id: { type: new GraphQLNonNull(GraphQLString) },
|
||||
data: { type: this.Upload.graphQL.mutationInputType },
|
||||
},
|
||||
resolve: update(this.Upload),
|
||||
};
|
||||
|
||||
this.Mutation.fields[`delete${singularLabel}`] = {
|
||||
type: this.Upload.graphQL.type,
|
||||
args: {
|
||||
id: { type: new GraphQLNonNull(GraphQLString) },
|
||||
},
|
||||
resolve: deleteResolver(this.Upload),
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = registerUpload;
|
||||
@@ -1,35 +0,0 @@
|
||||
const mongoose = require('mongoose');
|
||||
const collectionRoutes = require('../collections/routes');
|
||||
const uploadRoutes = require('./routes');
|
||||
const baseUploadFields = require('./baseUploadFields');
|
||||
const baseImageFields = require('./baseImageFields');
|
||||
const buildCollectionSchema = require('../collections/buildSchema');
|
||||
|
||||
function registerUpload() {
|
||||
// TODO: mongooseHidden on our upload model is hiding all the fields
|
||||
const uploadSchema = buildCollectionSchema(
|
||||
this.config.upload,
|
||||
this.config,
|
||||
{ discriminatorKey: 'type' },
|
||||
);
|
||||
|
||||
uploadSchema.add(baseUploadFields);
|
||||
|
||||
const imageSchema = new mongoose.Schema(baseImageFields, {
|
||||
discriminatorKey: 'type',
|
||||
});
|
||||
|
||||
this.Upload = {
|
||||
Model: mongoose.model(this.config.upload.labels.singular, uploadSchema),
|
||||
config: this.config.upload,
|
||||
};
|
||||
|
||||
// TODO: image type hard coded, but in the future we need some way of customizing how uploads are handled in customizable pattern
|
||||
this.Upload.Model.discriminator('image', imageSchema);
|
||||
|
||||
this.router.use(uploadRoutes(this.config, this.Upload));
|
||||
|
||||
this.router.use(collectionRoutes(this.Upload));
|
||||
}
|
||||
|
||||
module.exports = registerUpload;
|
||||
@@ -1,104 +0,0 @@
|
||||
const mkdirp = require('mkdirp');
|
||||
const httpStatus = require('http-status');
|
||||
const resizeAndSave = require('./images/imageResizer');
|
||||
const findByID = require('../collections/operations/findByID');
|
||||
const { NotFound } = require('../errors');
|
||||
const { getFileSystemSafeFileName } = require('./utilities');
|
||||
|
||||
async function fileTypeHandler(config, uploadConfig, savedFilename) {
|
||||
const fileData = {};
|
||||
fileData.name = savedFilename;
|
||||
if (uploadConfig.imageSizes) {
|
||||
fileData.sizes = await resizeAndSave(config, uploadConfig, savedFilename);
|
||||
}
|
||||
return fileData;
|
||||
}
|
||||
|
||||
const update = async (req, res, next, config) => {
|
||||
const query = {
|
||||
Model: req.collection.Model,
|
||||
id: req.params.id,
|
||||
locale: req.locale,
|
||||
};
|
||||
const doc = await findByID(query, { returnRawDoc: true });
|
||||
if (!doc) {
|
||||
res.status(httpStatus.NOT_FOUND)
|
||||
.json(new NotFound());
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.files && req.files.file) {
|
||||
doc.filename = req.files.file.name;
|
||||
const outputFilepath = `${config.staticDir}/${req.files.file.name}`;
|
||||
const moveError = await req.files.file.mv(outputFilepath);
|
||||
if (moveError) {
|
||||
res.status(httpStatus.INTERNAL_SERVER_ERROR)
|
||||
.json(moveError);
|
||||
return;
|
||||
}
|
||||
const handlerData = await fileTypeHandler(config, req.uploadConfig, req.files.file);
|
||||
Object.assign(doc, handlerData);
|
||||
}
|
||||
|
||||
doc.save((saveError) => {
|
||||
if (saveError) {
|
||||
res.status(httpStatus.INTERNAL_SERVER_ERROR)
|
||||
.json({ error: saveError });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(httpStatus.OK)
|
||||
.json({
|
||||
message: 'success',
|
||||
result: doc.toJSON({ virtuals: true }),
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const upload = async (req, res, next, config) => {
|
||||
if (!req.files || Object.keys(req.files).length === 0) {
|
||||
res.status(httpStatus.BAD_REQUEST)
|
||||
.json({ error: 'No files were uploaded.' });
|
||||
return;
|
||||
}
|
||||
|
||||
mkdirp(config.staticDir, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(httpStatus.INTERNAL_SERVER_ERROR)
|
||||
.json({ error: 'Upload failed.' });
|
||||
}
|
||||
});
|
||||
|
||||
const fsSafeName = await getFileSystemSafeFileName(config.staticDir, req.files.file.name);
|
||||
const moveError = await req.files.file.mv(`${config.staticDir}/${fsSafeName}`);
|
||||
if (moveError) {
|
||||
res.status(httpStatus.INTERNAL_SERVER_ERROR)
|
||||
.send(moveError);
|
||||
return;
|
||||
}
|
||||
|
||||
const uploadData = await fileTypeHandler(config, req.uploadConfig, fsSafeName);
|
||||
|
||||
req.collection.Model.create({
|
||||
...req.body,
|
||||
filename: uploadData.name,
|
||||
...uploadData,
|
||||
}, (mediaCreateError, result) => {
|
||||
if (mediaCreateError) {
|
||||
return res.status(httpStatus.INTERNAL_SERVER_ERROR)
|
||||
.json({ error: mediaCreateError });
|
||||
}
|
||||
|
||||
return res.status(httpStatus.CREATED)
|
||||
.json({
|
||||
message: 'success',
|
||||
result: result.toJSON({ virtuals: true }),
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
update,
|
||||
upload,
|
||||
};
|
||||
@@ -1,23 +0,0 @@
|
||||
const express = require('express');
|
||||
const fileUpload = require('express-fileupload');
|
||||
const { upload, update } = require('./requestHandlers');
|
||||
const bindCollectionMiddleware = require('../collections/bindCollection');
|
||||
const authenticate = require('../express/middleware/authenticate');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const uploadRoutes = (collection) => {
|
||||
const { config } = collection;
|
||||
|
||||
router.all(`/${config.slug}*`,
|
||||
fileUpload(),
|
||||
authenticate,
|
||||
bindCollectionMiddleware(collection));
|
||||
|
||||
router.route(`/${config.slug}`).post(upload);
|
||||
router.route(`/${config.slug}/:id`).put(update);
|
||||
|
||||
return router;
|
||||
};
|
||||
|
||||
module.exports = uploadRoutes;
|
||||
Reference in New Issue
Block a user