This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git commit 4d97782c428272739a88aa9cd2b5f73c83359afc Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Tue May 30 10:48:43 2017 +0200 vérification de la taille des fichiers, coté serveur (#55) --- .../pollen/rest/api/v1/PollenResourceApi.java | 5 ++- pollen-services/src/main/config/PollenServices.ini | 6 +++ .../java/org/chorem/pollen/services/UnitHuman.java | 43 ++++++++++++++++++++++ .../services/service/PollenResourceService.java | 32 ++++++++++++++-- .../i18n/pollen-services_en_GB.properties | 2 + .../i18n/pollen-services_fr_FR.properties | 2 + pollen-ui-riot-js/src/main/web/conf.js | 1 + pollen-ui-riot-js/src/main/web/js/Poll.js | 3 +- pollen-ui-riot-js/src/main/web/js/PollForm.js | 8 ++-- .../src/main/web/tag/poll/EditPoll.tag.html | 6 ++- .../src/main/web/tag/poll/Votes.tag.html | 5 ++- 11 files changed, 98 insertions(+), 15 deletions(-) diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java index 25812bc..49124c3 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java @@ -27,6 +27,7 @@ import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.ResourceFileBean; import org.chorem.pollen.services.bean.ResourceMetaBean; import org.chorem.pollen.services.bean.ResourceStreamBean; +import org.chorem.pollen.services.service.InvalidFormException; import org.chorem.pollen.services.service.PollenResourceService; import org.debux.webmotion.server.WebMotionController; import org.debux.webmotion.server.call.UploadFile; @@ -106,7 +107,7 @@ public class PollenResourceApi extends WebMotionController { return pollenResourceService.getMetaResource(resourceId.getEntityId()); } - public PollenEntityRef<PollenResource> createResource(PollenResourceService pollenResourceService, UploadFile resource) { + public PollenEntityRef<PollenResource> createResource(PollenResourceService pollenResourceService, UploadFile resource) throws InvalidFormException { ResourceFileBean resourceBean = new ResourceFileBean(); @@ -122,7 +123,7 @@ public class PollenResourceApi extends WebMotionController { return createRef; } - public PollenEntityRef<PollenResource> editResource(PollenResourceService pollenResourceService, PollenEntityId<PollenResource> resourceId, UploadFile resource) { + public PollenEntityRef<PollenResource> editResource(PollenResourceService pollenResourceService, PollenEntityId<PollenResource> resourceId, UploadFile resource) throws InvalidFormException { ResourceFileBean resourceBean = new ResourceFileBean(); diff --git a/pollen-services/src/main/config/PollenServices.ini b/pollen-services/src/main/config/PollenServices.ini index 38e8001..79de3ed 100644 --- a/pollen-services/src/main/config/PollenServices.ini +++ b/pollen-services/src/main/config/PollenServices.ini @@ -148,3 +148,9 @@ description = pollen.configuration.sendEndPollRemindersCronSchedule key = pollen.sendEndPollRemindersCronSchedule type = string default = "0 0/5 * * * ?" + +[option resourceMaxSize] +description = pollen.configuration.resource.maxSize +key = pollen.resource.maxSize +type = long +defaultValue = 10000000 diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/UnitHuman.java b/pollen-services/src/main/java/org/chorem/pollen/services/UnitHuman.java new file mode 100644 index 0000000..4414c0b --- /dev/null +++ b/pollen-services/src/main/java/org/chorem/pollen/services/UnitHuman.java @@ -0,0 +1,43 @@ +package org.chorem.pollen.services; + +/** + * @author Sylvain Bavencoff - bavencoff@codelutin.com + */ +public enum UnitHuman { + + UNIT("", 0), + KILO("k", 3), + MEGA("M", 6), + GIGA("G", 9), + TERA("T", 12), + PETA("P", 15), + EXA("E", 18), + ZETTA("Z", 21), + YOTTA("Y", 24); + + protected String prefix; + + protected int decimalPower; + + UnitHuman(String prefix, int decimalPower) { + this.prefix = prefix; + this.decimalPower = decimalPower; + } + + public static UnitHuman getUnitHuman(double value) { + int ordinal = (int) Math.floor(Math.log10(value) / 3); + ordinal = Math.max(Math.min(ordinal, UnitHuman.values().length - 1), 0); + return UnitHuman.values()[ordinal]; + } + + public double getUnitValue(double value) { + return value / Math.pow(10, decimalPower); + } + + public String getUnit(String unit) { + return prefix + unit; + } + + + +} diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenResourceService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenResourceService.java index 8c57685..ac248f2 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenResourceService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenResourceService.java @@ -23,6 +23,7 @@ package org.chorem.pollen.services.service; import org.chorem.pollen.persistence.entity.PollenResource; import org.chorem.pollen.services.PollenService; +import org.chorem.pollen.services.UnitHuman; import org.chorem.pollen.services.bean.PollenEntityId; import org.chorem.pollen.services.bean.PollenEntityRef; import org.chorem.pollen.services.bean.ResourceFileBean; @@ -33,6 +34,8 @@ import org.nuiton.topia.persistence.TopiaIdFactory; import java.io.IOException; import java.sql.SQLException; +import static org.nuiton.i18n.I18n.l; + /** * Created on 10/07/14. * @@ -56,11 +59,12 @@ public class PollenResourceService extends PollenServiceSupport implements Polle return toBean(ResourceMetaBean.class, resource); } - public PollenEntityRef<PollenResource> createResource(ResourceFileBean resource) { + public PollenEntityRef<PollenResource> createResource(ResourceFileBean resource) throws InvalidFormException { checkNotNull(resource); checkIsNotPersisted(resource); - // TODO : check resource + ErrorMap errorMap = checkRessource(resource); + errorMap.failIfNotEmpty(); PollenResource savedResource = saveResource(resource); commit(); @@ -68,11 +72,12 @@ public class PollenResourceService extends PollenServiceSupport implements Polle return PollenEntityRef.of(savedResource); } - public PollenEntityRef<PollenResource> editResource(String resourceId, ResourceFileBean resource) { + public PollenEntityRef<PollenResource> editResource(String resourceId, ResourceFileBean resource) throws InvalidFormException { checkNotNull(resourceId); checkIsNotPersisted(resource); - // TODO: check resource + ErrorMap errorMap = checkRessource(resource); + errorMap.failIfNotEmpty(); PollenResource savedResource = saveResource(resource); commit(); @@ -140,4 +145,23 @@ public class PollenResourceService extends PollenServiceSupport implements Polle return resourceId.getReducedId(); } + + protected ErrorMap checkRessource(ResourceFileBean resource) { + ErrorMap errorMap = new ErrorMap(); + if (resource.getSize() > getPollenServiceConfig().getResourceMaxSize()) { + + UnitHuman sizeUnitHuman = UnitHuman.getUnitHuman(resource.getSize()); + UnitHuman maxUnitHuman = UnitHuman.getUnitHuman(getPollenServiceConfig().getResourceMaxSize()); + String message = l(getLocale(), + "pollen.error.resource.maxSize", + resource.getName(), + sizeUnitHuman.getUnitValue(resource.getSize()), + sizeUnitHuman.getUnit("o"), + maxUnitHuman.getUnitValue(getPollenServiceConfig().getResourceMaxSize()), + maxUnitHuman.getUnit("o")); + + errorMap.addError("size", message); + } + return errorMap; + } } diff --git a/pollen-services/src/main/resources/i18n/pollen-services_en_GB.properties b/pollen-services/src/main/resources/i18n/pollen-services_en_GB.properties index 9e6e6b1..15c319c 100644 --- a/pollen-services/src/main/resources/i18n/pollen-services_en_GB.properties +++ b/pollen-services/src/main/resources/i18n/pollen-services_en_GB.properties @@ -20,6 +20,7 @@ pollen.configuration.defaultVoteNotification=Default notification type for the v pollen.configuration.defaultVoteVisibility=Default vote visiblity pollen.configuration.devMode=Dev mode pollen.configuration.logConfigurationFile=Path to log configuration file +pollen.configuration.resource.maxSize=Maximum size of pollen resource pollen.configuration.secret= pollen.configuration.sendEndPollRemindersCronSchedule=Time between two cron jobs of poll end reminder sending pollen.configuration.sendVoteSummariesCronSchedule=Time between two cron jobs of vote summary sending @@ -76,6 +77,7 @@ pollen.error.poll.voteCountingType.mandatory=vote counting type is mandatory pollen.error.poll.voteVisibility.mandatory=vote visibility is mandatory pollen.error.poll.voterList.mandatory.for.groupedPoll=At least one voter list ins mandatory for a grouped poll pollen.error.resource.empty=No resource sent +pollen.error.resource.maxSize=File "%s" of %4.2f %s can't be over %4.2f %s. pollen.error.resource.notExist=Image don't exist pollen.error.user.mailEmpty=email can not be empty pollen.error.user.mailExist=email already exists diff --git a/pollen-services/src/main/resources/i18n/pollen-services_fr_FR.properties b/pollen-services/src/main/resources/i18n/pollen-services_fr_FR.properties index bcf9cd2..5801694 100644 --- a/pollen-services/src/main/resources/i18n/pollen-services_fr_FR.properties +++ b/pollen-services/src/main/resources/i18n/pollen-services_fr_FR.properties @@ -20,6 +20,7 @@ pollen.configuration.defaultVoteNotification=Type de notification par défaut po pollen.configuration.defaultVoteVisibility=Visibilité des votes par défaut pollen.configuration.devMode=Mode développement pollen.configuration.logConfigurationFile=Chemin vers le fichier de configuration des logs +pollen.configuration.resource.maxSize=Taille maximal pour un fichier de resource de Pollen pollen.configuration.secret= pollen.configuration.sendEndPollRemindersCronSchedule=Intervalle entre deux lancements de la tâche d'envoi de mails de rappel de fin de sondage pollen.configuration.sendVoteSummariesCronSchedule=Intervalle entre deux lancements de la tâche d'envoi de mails de résumé des votes pour un sondage @@ -75,6 +76,7 @@ pollen.error.poll.voteCountingType.mandatory=le type de vote est obligatoire pollen.error.poll.voteVisibility.mandatory=la visiblité des votes est obligatoire pollen.error.poll.voterList.mandatory.for.groupedPoll=Au moins une liste de votant est obligatoire pour un sondage groupé pollen.error.resource.empty=Aucune resource envoyé +pollen.error.resource.maxSize=Le fichier « %s » de %4.2f %s ne doit pas dépasser %4.2f %s. pollen.error.resource.notExist=L'image n'existe pas sur le serveur pollen.error.user.mailEmpty=Courriel ne peut pas être vide pollen.error.user.mailExist=Courriel existe déjà diff --git a/pollen-ui-riot-js/src/main/web/conf.js b/pollen-ui-riot-js/src/main/web/conf.js index 02a7e34..19b836c 100644 --- a/pollen-ui-riot-js/src/main/web/conf.js +++ b/pollen-ui-riot-js/src/main/web/conf.js @@ -1,4 +1,5 @@ window.pollenConf = { endPoint: "http://localhost:8888/pollen-rest-api", + defaultErrorTimeout: 15, resourceMaxSize: 10000000 // octets => 10 Mo }; diff --git a/pollen-ui-riot-js/src/main/web/js/Poll.js b/pollen-ui-riot-js/src/main/web/js/Poll.js index 6cdfaaf..8388528 100644 --- a/pollen-ui-riot-js/src/main/web/js/Poll.js +++ b/pollen-ui-riot-js/src/main/web/js/Poll.js @@ -100,8 +100,7 @@ class Poll { if (this.id) { let promise; if (choice.choiceType === "RESOURCE" && choice.choiceValue.type) { - promise = resourceService.create(choice.choiceValue) - .then((result) => { + promise = resourceService.create(choice.choiceValue).then((result) => { choice.choiceValue = result.id; return Promise.resolve(choice); }); diff --git a/pollen-ui-riot-js/src/main/web/js/PollForm.js b/pollen-ui-riot-js/src/main/web/js/PollForm.js index 155ee84..f2f1b6c 100644 --- a/pollen-ui-riot-js/src/main/web/js/PollForm.js +++ b/pollen-ui-riot-js/src/main/web/js/PollForm.js @@ -143,11 +143,9 @@ class PollForm { let fileUploadPromises = []; this.choices.forEach((choice) => { if (choice.choiceType === "RESOURCE" && choice.choiceValue.name) { - let promise = new Promise((resolve, reject) => { - resourceService.create(choice.choiceValue).then((result) => { - choice.choiceValue = result.id; - resolve(result.id); - }); + let promise = resourceService.create(choice.choiceValue).then((result) => { + choice.choiceValue = result.id; + return result.id; }); fileUploadPromises.push(promise); } diff --git a/pollen-ui-riot-js/src/main/web/tag/poll/EditPoll.tag.html b/pollen-ui-riot-js/src/main/web/tag/poll/EditPoll.tag.html index 43135ba..9ff3119 100644 --- a/pollen-ui-riot-js/src/main/web/tag/poll/EditPoll.tag.html +++ b/pollen-ui-riot-js/src/main/web/tag/poll/EditPoll.tag.html @@ -136,7 +136,11 @@ require("./Created.tag.html"); promiseSave.then(() => { let route = require("riot-route"); route("poll/" + this.form.model.id + "/vote/" + this.form.model.permission); - }); + }, errors => { + this.bus.trigger("error", errors); + this.update(); + } + ); }; }; diff --git a/pollen-ui-riot-js/src/main/web/tag/poll/Votes.tag.html b/pollen-ui-riot-js/src/main/web/tag/poll/Votes.tag.html index 0c669fb..2f43735 100644 --- a/pollen-ui-riot-js/src/main/web/tag/poll/Votes.tag.html +++ b/pollen-ui-riot-js/src/main/web/tag/poll/Votes.tag.html @@ -263,7 +263,7 @@ require("./Podium.tag.html"); var selectedChoiceNb = 0; this.poll.choices.forEach(c => { var choiceValue = this.getChoiceVoteValue(c.id + "_voteValue"); - if (choiceValue && choiceValue !== '0') { + if (choiceValue && choiceValue !== "0") { selectedChoiceNb++; } }); @@ -371,6 +371,9 @@ require("./Podium.tag.html"); this.poll.addChoice(this.choiceToAdd).then(() => { this.choiceToAdd = this.poll.initChoice(this.choiceToAdd); this.update(); + }, errors => { + this.bus.trigger("error", errors); + this.update(); }); }; -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.