Merge branch 'fix-char-restriction' of git://github.com/GaurangTandon/p5.js-web-editor into GaurangTandon-fix-char-restriction

This commit is contained in:
Cassie Tarakajian 2019-02-20 14:49:55 -05:00
commit 4dcec4baa8
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 slugify from 'slugify';
import jsdom, { serializeDocument } from 'jsdom';
import generateFileSystemSafeName from '../utils/generateFileSystemSafeName';
import Project from '../models/project';
import User from '../models/user';
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');
project.slug = slugify(project.name, '_');
res.attachment(`${project.slug}_${currentTime}.zip`);
res.attachment(`${generateFileSystemSafeName(project.slug)}_${currentTime}.zip`);
zip.pipe(res);
function addFileToZip(file, path) {

View file

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