Merge branch 'GaurangTandon-fix-char-restriction'

This commit is contained in:
Cassie Tarakajian 2019-02-20 14:57:33 -05:00
commit 3f821ffb32
3 changed files with 40 additions and 17 deletions

View file

@ -4,6 +4,7 @@ import moment from 'moment';
import isUrl from 'is-url'; import isUrl from 'is-url';
import slugify from 'slugify'; import slugify from 'slugify';
import jsdom, { serializeDocument } from 'jsdom'; import jsdom, { serializeDocument } from 'jsdom';
import generateFileSystemSafeName from '../utils/generateFileSystemSafeName';
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';
@ -295,7 +296,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 = slugify(project.name, '_');
res.attachment(`${project.slug}_${currentTime}.zip`); res.attachment(`${generateFileSystemSafeName(project.slug)}_${currentTime}.zip`);
zip.pipe(res); zip.pipe(res);
function addFileToZip(file, path) { function addFileToZip(file, path) {

View file

@ -4,14 +4,17 @@ import slugify from 'slugify';
const { Schema } = mongoose; const { Schema } = mongoose;
const fileSchema = new Schema({ const fileSchema = new Schema(
name: { type: String, default: 'sketch.js' }, {
content: { type: String, default: '' }, name: { type: String, default: 'sketch.js' },
url: { type: String }, content: { type: String, default: '' },
children: { type: [String], default: [] }, url: { type: String },
fileType: { type: String, default: 'file' }, children: { type: [String], default: [] },
isSelectedFile: { type: Boolean } fileType: { type: String, default: 'file' },
}, { timestamps: true, _id: true, usePushEach: true }); isSelectedFile: { type: Boolean }
},
{ timestamps: true, _id: true, usePushEach: true }
);
fileSchema.virtual('id').get(function getFileId() { fileSchema.virtual('id').get(function getFileId() {
return this._id.toHexString(); return this._id.toHexString();
@ -21,14 +24,17 @@ fileSchema.set('toJSON', {
virtuals: true virtuals: true
}); });
const projectSchema = new Schema({ const projectSchema = new Schema(
name: { type: String, default: "Hello p5.js, it's the server" }, {
user: { type: Schema.Types.ObjectId, ref: 'User' }, name: { type: String, default: "Hello p5.js, it's the server" },
serveSecure: { type: Boolean, default: false }, user: { type: Schema.Types.ObjectId, ref: 'User' },
files: { type: [fileSchema] }, serveSecure: { type: Boolean, default: false },
_id: { type: String, default: shortid.generate }, files: { type: [fileSchema] },
slug: { type: String } _id: { type: String, default: shortid.generate },
}, { timestamps: true, usePushEach: true }); slug: { type: String }
},
{ timestamps: true, usePushEach: true }
);
projectSchema.virtual('id').get(function getProjectId() { projectSchema.virtual('id').get(function getProjectId() {
return this._id; return this._id;

View file

@ -0,0 +1,16 @@
/**
* generate file system safe string 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
*/
function generateFileSystemSafeName(string, replacer) {
// from here https://serverfault.com/a/242134
const INVALID_CHARS_REGEX = /[*/?:\\<>|"\u0000-\u001F]/g; // eslint-disable-line
const slug = string.replace(INVALID_CHARS_REGEX, replacer || '');
return slug;
}
export default generateFileSystemSafeName;