Commit c18ecdb37151992553ce408e72f13af40326311b

Authored by gibransodre
1 parent ef935bb9
Exists in master

Validação de tipo de arquivos.

WebContent/WEB-INF/jsp/planoTrabalho/planoTrabalho.jsp
... ... @@ -33,7 +33,7 @@
33 33 <input type="text" id="valorHash" name="valorHash" size="60" maxlength="60" required><br><br>
34 34 <label for="file">Arquivo: </label>
35 35 <input type="file" id="file" name="file" accept=
36   -"application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint,
  36 +"application/msword, application/vnd.ms-excel, application/vnd.oasis.opendocument.text ,
37 37 text/plain, application/pdf, image/*" required><br><br>
38 38 <input type="submit" class="button" value="Enviar">
39 39 </fieldset>
... ...
src/br/com/controller/PlanoTrabalhoController.java
... ... @@ -14,59 +14,69 @@ import br.com.model.entity.Questionario;
14 14  
15 15 @Resource
16 16 public class PlanoTrabalhoController {
17   -
  17 +
18 18 private Validator validator;
19 19 private Result result;
20 20 private ServletContext application;
21 21 private String path;
22   -
23   - public PlanoTrabalhoController(Result result, Validator validator, ServletContext application)
24   - {
  22 +
  23 + public PlanoTrabalhoController(Result result, Validator validator, ServletContext application) {
25 24 this.validator = validator;
26 25 this.application = application;
27 26 this.path = this.application.getRealPath("") + "/WEB-INF/conexao.properties";
28 27 this.result = result;
29 28 }
30   -
  29 +
31 30 @Path("/planoTrabalho-site")
32   - public void planoTrabalho()
33   - {
  31 + public void planoTrabalho() {
34 32  
35 33 }
36   -
  34 +
37 35 @Path("/plano-trabalho-confirmacao")
38   - public void planoTrabalhoConfirmacao()
39   - {
40   -
  36 + public void planoTrabalhoConfirmacao() {
  37 +
41 38 }
42   -
  39 +
43 40 @Path("/salvar-plano-trabalho")
44   - public void enviarPlanoTrabalho(String valorHash, UploadedFile file)
45   - {
46   - HashDao hashDao = new HashDao();
47   - Questionario questionario = new Questionario();
48   - questionario = hashDao.verificarHash(path, valorHash);
  41 + public void enviarPlanoTrabalho(String valorHash, UploadedFile file) {
  42 + String tipoArquivo = file.getContentType();
49 43 String mensagem = "Não foi possível enviar o plano de trabalho!";
50 44 boolean status = false;
51   -
52   - if(questionario != null && questionario.getDtInicioQuestionario() != null)
53   - {
54   - PlanoTrabalhoDao planoTrabalhoDao = new PlanoTrabalhoDao();
55   -
56   - if(planoTrabalhoDao.salvarPlanoTrabalho(valorHash, file.getFile(), questionario))
57   - {
58   - mensagem = "Plano de trabalho enviado com sucesso!";
59   - status = true;
60   - }
61   - }
62   - else
63   - {
64   - mensagem = "Favor verificar o Código Hash!";
  45 +
  46 + if (!tipoArquivo.equalsIgnoreCase("application/msword")
  47 + && !tipoArquivo.equalsIgnoreCase("application/vnd.ms-excel")
  48 + && !tipoArquivo.equalsIgnoreCase("application/vnd.oasis.opendocument.text")
  49 + && !tipoArquivo.equalsIgnoreCase("text/plain")
  50 + && !tipoArquivo.equalsIgnoreCase("application/pdf")
  51 + && !tipoArquivo.equalsIgnoreCase("image/png")
  52 + && !tipoArquivo.equalsIgnoreCase("image/gif")
  53 + && !tipoArquivo.equalsIgnoreCase("image/jpeg")) {
  54 +
  55 + mensagem = "Formato de Arquivo inválido!";
  56 + status = false;
  57 +
  58 + } else {
  59 + HashDao hashDao = new HashDao();
  60 + Questionario questionario = new Questionario();
  61 + questionario = hashDao.verificarHash(path, valorHash);
  62 +
  63 +
  64 + if (questionario != null && questionario.getDtInicioQuestionario() != null) {
  65 + PlanoTrabalhoDao planoTrabalhoDao = new PlanoTrabalhoDao();
  66 +
  67 + if (planoTrabalhoDao.salvarPlanoTrabalho(valorHash, file.getFile(), questionario)) {
  68 + mensagem = "Plano de trabalho enviado com sucesso!";
  69 + status = true;
  70 + }
  71 +
  72 + else {
  73 + mensagem = "Favor verificar o Código Hash!";
  74 + }
  75 + }
65 76 }
66   - result.include("mensagem", mensagem);
67   - result.include("status", status);
68   - this.validator.add(new ValidationMessage("", ""));
69   - this.validator.onErrorUsePageOf(PlanoTrabalhoController.class).planoTrabalhoConfirmacao();
  77 + result.include("mensagem", mensagem);
  78 + result.include("status", status);
  79 + this.validator.add(new ValidationMessage("", ""));
  80 + this.validator.onErrorUsePageOf(PlanoTrabalhoController.class).planoTrabalhoConfirmacao();
70 81 }
71   -
72 82 }
... ...