@Post @Path("/apps") @Permission(value = {Role.ADMIN, Role.MEMBER}) public void create(Apps apps, UploadedFile war) { try { PropertiesUtil props = new PropertiesUtil("src/main/resources/path.properties"); String pathDeploy = props.getValor("tomcat.webapps"); if (war != null) apps.setNameWar(war.getFileName().substring(0, war.getFileName().indexOf("."))); validator.validate(apps); uploadValidate(war); validator.onErrorUsePageOf(this).newApps(); File warFile = new File(pathDeploy + war.getFileName()); IOUtils.copyLarge(war.getFile(), new FileOutputStream(warFile)); apps.setUser(this.userSession.getUser()); this.result.include("message", "Upload concluído com sucesso."); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } repository.create(apps); result.redirectTo(this).index(); }
private void uploadValidate(UploadedFile war) { if (war == null) { this.validator.add(new ValidationMessage("Não pode ser nulo.", "war")); } else { if (!war.getFileName().endsWith(".war")) { this.validator.add(new ValidationMessage("Necessita ser um war.", "war")); } if (war.getFileName().trim().equals("")) { this.validator.add(new ValidationMessage("Necessita ter um nome.", "war")); } if (war.getFileName().contains(" ")) { this.validator.add(new ValidationMessage("Necessita ter um nome.", "war")); } if (war.getFileName().length() > 40) { this.validator.add(new ValidationMessage("Nome do .war muito grande.", "war")); } if (haveSpecialChar(war.getFileName())) { this.validator.add( new ValidationMessage("Nome do .war não pode conter caracteres especiais.", "war")); } } }
/** * Save image to avatar dir * * @param avatar * @param uploadedFile * @return */ private File saveImage(Avatar avatar, UploadedFile uploadedFile) { String configKey = getAvatarPathConfigKey(avatar); if (configKey != null && uploadedFile != null) { UploadUtils upload = new UploadUtils(uploadedFile); String imageName = String.format( "%s.%s", MD5.hash(uploadedFile.getFileName() + System.currentTimeMillis()), upload.getExtension()); String filePath = String.format( "%s/%s/%s", this.config.getApplicationPath(), this.config.getValue(configKey), imageName); upload.saveUploadedFile(filePath); return new File(filePath); } return null; }