Skip to content

Commit 7fa6bf0

Browse files
authored
Event form icons fix
1 parent d7284c0 commit 7fa6bf0

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

service/src/api/form.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ function compareDisplayName(a, b) {
3232
}
3333

3434
function getUserFields(form) {
35-
return form.fields.filter(function(field) {
35+
return form.fields.filter(function (field) {
3636
if (field.archived) return false;
3737

38-
return form.userFields.some(function(memberField) {
38+
return form.userFields.some(function (memberField) {
3939
return memberField === field.name;
4040
});
4141
});
4242
}
4343

44-
Form.prototype.populateUserFields = function(callback) {
44+
Form.prototype.populateUserFields = function (callback) {
4545
var event = this._event;
4646
var form = this._form;
4747

4848
var forms = form ? [form] : event.forms;
4949
var formsUserFields = [];
50-
forms.forEach(function(form) {
50+
forms.forEach(function (form) {
5151
var userFields = getUserFields(form);
5252
if (userFields.length) {
5353
formsUserFields.push(userFields);
@@ -67,7 +67,7 @@ Form.prototype.populateUserFields = function(callback) {
6767
clean-up.
6868
*/
6969
const teamIds = (event.teamIds || []).map(x => x._id ? x._id : x)
70-
Team.getTeams({ teamIds }, function(err, teams) {
70+
Team.getTeams({ teamIds }, function (err, teams) {
7171
if (err) {
7272
console.error(err);
7373
return callback(err);
@@ -76,8 +76,8 @@ Form.prototype.populateUserFields = function(callback) {
7676
var choices = [];
7777
var users = {};
7878

79-
teams.forEach(function(team) {
80-
team.userIds.forEach(function(user) {
79+
teams.forEach(function (team) {
80+
team.userIds.forEach(function (user) {
8181
users[user.displayName] = user.displayName;
8282
});
8383
});
@@ -93,12 +93,12 @@ Form.prototype.populateUserFields = function(callback) {
9393
}
9494

9595
// Update the choices for user field
96-
formsUserFields.forEach(function(userFields) {
97-
userFields.forEach(function(userField) {
96+
formsUserFields.forEach(function (userFields) {
97+
userFields.forEach(function (userField) {
9898
userField.choices = choices.slice();
9999

100100
if (!userField.required && userField.type === 'dropdown') {
101-
userField.choices.unshift({id: 0, value: 0, title: ""});
101+
userField.choices.unshift({ id: 0, value: 0, title: "" });
102102
}
103103
});
104104
});
@@ -107,15 +107,15 @@ Form.prototype.populateUserFields = function(callback) {
107107
});
108108
};
109109

110-
Form.prototype.export = function(formId, callback) {
110+
Form.prototype.export = function (formId, callback) {
111111
var iconBasePath = new api.Icon(this._event._id).getBasePath();
112112
var formBasePath = path.join(iconBasePath, formId.toString());
113113

114114
var archive = archiver('zip');
115115
archive.directory(formBasePath, 'form/icons');
116-
archive.append("", {name: 'icons/', prefix: 'form'});
116+
archive.append("", { name: 'icons/', prefix: 'form' });
117117

118-
var forms = this._event.forms.filter(function(form) {
118+
var forms = this._event.forms.filter(function (form) {
119119
return form._id === formId;
120120
});
121121

@@ -125,7 +125,7 @@ Form.prototype.export = function(formId, callback) {
125125
return callback(err);
126126
}
127127

128-
archive.append(JSON.stringify(forms[0]), {name: "form/form.json"});
128+
archive.append(JSON.stringify(forms[0]), { name: "form/form.json" });
129129
archive.finalize();
130130

131131
callback(null, {
@@ -134,7 +134,7 @@ Form.prototype.export = function(formId, callback) {
134134
});
135135
};
136136

137-
Form.prototype.validate = function(file, callback) {
137+
Form.prototype.validate = function (file, callback) {
138138
let archiveError = new Error('Form archive file is invalid, please choose a valid file.');
139139
archiveError.status = 400;
140140

@@ -169,19 +169,27 @@ Form.prototype.validate = function(file, callback) {
169169
callback(null, form);
170170
};
171171

172-
Form.prototype.importIcons = function(file, form, callback) {
172+
Form.prototype.importIcons = function (file, form, callback) {
173173
var event = this._event;
174174
var zip = new Zip(file.path);
175175

176176
var iconsEntry = zip.getEntry('form/icons/');
177177
if (iconsEntry) {
178178
var iconPath = path.join(new api.Icon(event._id).getBasePath(), form._id.toString()) + path.sep;
179179

180-
zip.extractEntryTo(iconsEntry, iconPath, false, false);
180+
// Extract all entries within form/icons/
181+
zip.getEntries().forEach(function (entry) {
182+
if (entry.entryName.startsWith('form/icons/') && !entry.isDirectory) {
183+
var relativePath = entry.entryName.substring('form/icons/'.length);
184+
var targetDir = path.join(iconPath, path.dirname(relativePath));
185+
var fileName = path.basename(relativePath);
186+
zip.extractEntryTo(entry, targetDir, false, true, false, fileName);
187+
}
188+
});
181189

182190
// for each file in each directory
183191
var walker = walk.walk(iconPath);
184-
walker.on("file", function(filePath, stat, next) {
192+
walker.on("file", function (filePath, stat, next) {
185193
var primary = null;
186194
var variant = null;
187195
var regex = new RegExp(iconPath + path.sep + "+(.*)");
@@ -193,11 +201,11 @@ Form.prototype.importIcons = function(file, form, callback) {
193201
variant = variants.shift();
194202
}
195203

196-
new api.Icon(event._id, form._id, primary, variant).add({name: stat.name}, function(err) {
204+
new api.Icon(event._id, form._id, primary, variant).add({ name: stat.name }, function (err) {
197205
next(err);
198206
});
199207
});
200-
walker.on("end", function() {
208+
walker.on("end", function () {
201209
callback(null);
202210
});
203211
} else {
@@ -262,7 +270,7 @@ function cleanForm(form) {
262270
return form.userFields.includes(field.name);
263271
});
264272

265-
form.userFields = [...userFields.reduce((fields, field) => {
273+
form.userFields = [...userFields.reduce((fields, field) => {
266274
if (form.userFields.includes(field.name)) {
267275
fields.add(fieldName(field.id));
268276
}

0 commit comments

Comments
 (0)