diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 88b990a3..0a98d253 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -2,8 +2,8 @@ import archiver from 'archiver'; import request from 'request'; import moment from 'moment'; import isUrl from 'is-url'; -import slugify from 'slugify'; import jsdom, { serializeDocument } from 'jsdom'; +import generateSlug from '../utils/generateSlug'; import Project from '../models/project'; import User from '../models/user'; import { deleteObjectsFromS3, getObjectKey } from './aws.controller'; @@ -294,7 +294,7 @@ function buildZip(project, req, res) { }); const currentTime = moment().format('YYYY_MM_DD_HH_mm_ss'); - project.slug = slugify(project.name, '_'); + project.slug = generateSlug(project.name, '_'); res.attachment(`${project.slug}_${currentTime}.zip`); zip.pipe(res); diff --git a/server/models/project.js b/server/models/project.js index c1a255d8..23b93796 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; import shortid from 'shortid'; -import slugify from 'slugify'; +import generateSlug from '../utils/generateSlug'; const { Schema } = mongoose; @@ -40,7 +40,7 @@ projectSchema.set('toJSON', { projectSchema.pre('save', function generateSlug(next) { const project = this; - project.slug = slugify(project.name, '_'); + project.slug = generateSlug(project.name, '_'); return next(); }); diff --git a/server/utils/generateSlug.js b/server/utils/generateSlug.js new file mode 100644 index 00000000..0e67b8a2 --- /dev/null +++ b/server/utils/generateSlug.js @@ -0,0 +1,16 @@ +/** + * generate short slug for a given string + * that can be used as a valid file name + * in all operating systems + * @param {String} string + * @param {String} replacer (optional) character to replace invalid characters + */ +export function generateSlug(string, replacer) { + // from here https://serverfault.com/a/242134 + const INVALID_CHARS_REGEX = /[*/?:\\<>|"\u0000-\u001F]/g; + const slug = string.replace(INVALID_CHARS_REGEX, replacer || ''); + + return slug; +} + +export default generateSlug;