This commit is contained in:
Gaurang Tandon 2018-12-15 12:35:52 +05:30
parent 244af16b64
commit 26d65396b4
3 changed files with 20 additions and 4 deletions

View file

@ -2,8 +2,8 @@ import archiver from 'archiver';
import request from 'request'; import request from 'request';
import moment from 'moment'; import moment from 'moment';
import isUrl from 'is-url'; import isUrl from 'is-url';
import slugify from 'slugify';
import jsdom, { serializeDocument } from 'jsdom'; import jsdom, { serializeDocument } from 'jsdom';
import generateSlug from '../utils/generateSlug';
import Project from '../models/project'; import Project from '../models/project';
import User from '../models/user'; import User from '../models/user';
import { deleteObjectsFromS3, getObjectKey } from './aws.controller'; 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'); 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`); res.attachment(`${project.slug}_${currentTime}.zip`);
zip.pipe(res); zip.pipe(res);

View file

@ -1,6 +1,6 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import shortid from 'shortid'; import shortid from 'shortid';
import slugify from 'slugify'; import generateSlug from '../utils/generateSlug';
const { Schema } = mongoose; const { Schema } = mongoose;
@ -40,7 +40,7 @@ projectSchema.set('toJSON', {
projectSchema.pre('save', function generateSlug(next) { projectSchema.pre('save', function generateSlug(next) {
const project = this; const project = this;
project.slug = slugify(project.name, '_'); project.slug = generateSlug(project.name, '_');
return next(); return next();
}); });

View file

@ -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;