From 236e3b40478e26a2b920c2aad0100b96aa5e1d13 Mon Sep 17 00:00:00 2001 From: manalejandro Date: Sun, 15 Jul 2018 03:45:44 +0200 Subject: [PATCH] v0.2.0 --- pom.xml | 2 +- .../arjion/controllers/MainController.java | 98 +++++++++++--- .../manalejandro/arjion/model/Documento.java | 104 +++++++++++++-- .../arjion/services/MainService.java | 12 +- .../arjion/services/MainServiceImpl.java | 46 +++++++ .../com/manalejandro/arjion/vo/DetailVO.java | 22 ++++ .../manalejandro/arjion/vo/DocumentoVO.java | 26 ++++ src/main/resources/application.properties | 4 +- src/main/resources/elasticsearch/mapping.json | 40 ++++-- src/main/resources/static/css/main.css | 4 + src/main/resources/templates/detail.html | 46 +++++++ src/main/resources/templates/exists.html | 18 +++ src/main/resources/templates/index.html | 122 +++++++++++------- 13 files changed, 446 insertions(+), 98 deletions(-) create mode 100644 src/main/java/com/manalejandro/arjion/vo/DetailVO.java create mode 100644 src/main/resources/templates/detail.html create mode 100644 src/main/resources/templates/exists.html diff --git a/pom.xml b/pom.xml index 42ebdfd..24999ab 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.manalejandro arjion - 0.1.0-SNAPSHOT + 0.2.0-SNAPSHOT war arjion diff --git a/src/main/java/com/manalejandro/arjion/controllers/MainController.java b/src/main/java/com/manalejandro/arjion/controllers/MainController.java index dea9756..75dfb89 100644 --- a/src/main/java/com/manalejandro/arjion/controllers/MainController.java +++ b/src/main/java/com/manalejandro/arjion/controllers/MainController.java @@ -1,13 +1,20 @@ package com.manalejandro.arjion.controllers; +import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.Normalizer; import java.util.ArrayList; +import javax.servlet.http.HttpServletResponse; + import com.manalejandro.arjion.model.Archivo; +import com.manalejandro.arjion.model.Documento; +import com.manalejandro.arjion.services.MainService; +import com.manalejandro.arjion.vo.DetailVO; import com.manalejandro.arjion.vo.DocumentoVO; import org.apache.tika.config.TikaConfig; @@ -18,7 +25,12 @@ import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.AutoDetectParser; import org.apache.tika.parser.ParseContext; import org.apache.tika.sax.BodyContentHandler; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -32,12 +44,21 @@ import org.xml.sax.SAXException; @Controller public class MainController { + private final MainService mainService; + @Value("${arjion.uploadpath}") private String uploadpath; + @Autowired + public MainController(MainService mainService) { + this.mainService = mainService; + } + @RequestMapping(path = "/") public String indexPage(final Model model) { DocumentoVO documentoVO = new DocumentoVO(); + documentoVO.setCount(mainService.count()); + documentoVO.setDocumentos(mainService.findAllDocumento()); model.addAttribute("documentoVO", documentoVO); return "index"; } @@ -51,24 +72,67 @@ public class MainController { public String uploadPage(final Model model, @RequestParam("archivos") MultipartFile[] archivos) throws IOException, TikaException, SAXException { DocumentoVO documentoVO = new DocumentoVO(); - documentoVO.setArchivos(new ArrayList()); - TikaConfig tikaConfig = TikaConfig.getDefaultConfig(); - for (int i = 0; i < archivos.length; i++) { - byte[] bytes = archivos[i].getBytes(); - String normalized = Normalizer.normalize(archivos[i].getOriginalFilename(), Normalizer.Form.NFD), - filename = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); - Path path = Paths.get(uploadpath + filename); - Files.write(path, bytes); - Metadata metadata = new Metadata(); - AutoDetectParser parser = new AutoDetectParser(tikaConfig); - ContentHandler handler = new BodyContentHandler(-1); - TikaInputStream stream = TikaInputStream.get(bytes); - parser.parse(stream, handler, metadata, new ParseContext()); - LanguageIdentifier identifier = new LanguageIdentifier(handler.toString()); - documentoVO.getArchivos().add(new Archivo(filename, String.valueOf(archivos[i].getSize()), - metadata.toString(), handler.toString(), identifier.getLanguage())); + documentoVO.setCount(mainService.count()); + documentoVO.setDocumentos(mainService.findAllDocumento()); + if (archivos.length > 0) { + documentoVO.setArchivos(new ArrayList()); + // Recupera la conficuración de Tika + TikaConfig tikaConfig = TikaConfig.getDefaultConfig(); + // Itera los archivos recibidos + for (int i = 0; i < archivos.length; i++) { + byte[] bytes = archivos[i].getBytes(); + // Normaliza el título de los archivos + String normalized = Normalizer.normalize(archivos[i].getOriginalFilename(), Normalizer.Form.NFD), + filename = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); + Path path = Paths.get(uploadpath + filename); + // Instancias necesarias + Metadata metadata = new Metadata(); + AutoDetectParser parser = new AutoDetectParser(tikaConfig); + // Usa -1 para no tener límite de 100000 chars + ContentHandler handler = new BodyContentHandler(-1); + // Castea los bytes al Stream de Tika + TikaInputStream stream = TikaInputStream.get(bytes); + // Parsea el contenido + parser.parse(stream, handler, metadata, new ParseContext()); + // Identifica el idioma del archivo + LanguageIdentifier identifier = new LanguageIdentifier(handler.toString()); + // Almacena en elasticsearch + if (!mainService.save(new Documento(filename, Long.valueOf(archivos[i].getSize()).intValue(), + metadata.toString(), handler.toString(), identifier.getLanguage()))) { + return "exists"; + } else { + // Guarda el archivo en el directorio configurado en las properties + Files.write(path, bytes); + } + // Añade los parámetros al VO para mostrar en la vista + documentoVO.getArchivos().add(new Archivo(filename, String.valueOf(archivos[i].getSize()), + metadata.toString(), handler.toString(), identifier.getLanguage())); + } } model.addAttribute("documentoVO", documentoVO); return "index"; } -} + + @GetMapping(path = "/detail") + public String detail(final Model model, @RequestParam(value = "nombre", required = true) String nombre) { + DetailVO detailVO = new DetailVO(); + detailVO.setDocumento(mainService.findOne(nombre)); + model.addAttribute("detailVO", detailVO); + return "detail"; + } + + @GetMapping(path = "/download") + public ResponseEntity download(final HttpServletResponse response, + @RequestParam(value = "filename", required = true) String filename) + throws IOException, MalformedURLException { + File file = new File(uploadpath + filename); + Path path = Paths.get(file.getAbsolutePath()); + ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path)); + String type = file.toURL().openConnection().guessContentTypeFromName(filename); + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.add("content-disposition", "attachment; filename=" + filename); + responseHeaders.add("Content-Type", type); + return ResponseEntity.ok().contentLength(file.length()).headers(responseHeaders) + .contentType(MediaType.parseMediaType("application/octet-stream")).body(resource); + } +} \ No newline at end of file diff --git a/src/main/java/com/manalejandro/arjion/model/Documento.java b/src/main/java/com/manalejandro/arjion/model/Documento.java index f7060cc..c20dff0 100644 --- a/src/main/java/com/manalejandro/arjion/model/Documento.java +++ b/src/main/java/com/manalejandro/arjion/model/Documento.java @@ -12,21 +12,97 @@ import com.fasterxml.jackson.annotation.JsonProperty; @Setting(settingPath = "/elasticsearch/settings.json") @Mapping(mappingPath = "/elasticsearch/mapping.json") public class Documento { - @Id - public Integer id; + @Id + public String nombre; + public Integer tamano; + public String metadata; + public String contenido; + public String lenguaje; - @JsonCreator - public Documento(@JsonProperty("id") Integer id) { - super(); - this.id = id; - } + @JsonCreator + public Documento(@JsonProperty("nombre") String nombre, @JsonProperty("tamano") Integer tamano, + @JsonProperty("metadata") String metadata, @JsonProperty("contenido") String contenido, + @JsonProperty("lenguaje") String lenguaje) { + super(); + this.nombre = nombre; + this.tamano = tamano; + this.metadata = metadata; + this.contenido = contenido; + this.lenguaje = lenguaje; + } - @JsonProperty("id") - public Integer getId() { - return id; - } + /** + * @return the nombre + */ + @JsonProperty("nombre") + public String getNombre() { + return nombre; + } - public void setId(Integer id) { - this.id = id; - } + /** + * @param nombre the nombre to set + */ + public void setNombre(String nombre) { + this.nombre = nombre; + } + + /** + * @return the tamano + */ + @JsonProperty("tamano") + public Integer getTamano() { + return tamano; + } + + /** + * @param tamano the tamano to set + */ + public void setTamano(Integer tamano) { + this.tamano = tamano; + } + + /** + * @return the metadata + */ + @JsonProperty("metadata") + public String getMetadata() { + return metadata; + } + + /** + * @param metadata the metadata to set + */ + public void setMetadata(String metadata) { + this.metadata = metadata; + } + + /** + * @return the contenido + */ + @JsonProperty("contenido") + public String getContenido() { + return contenido; + } + + /** + * @param contenido the contenido to set + */ + public void setContenido(String contenido) { + this.contenido = contenido; + } + + /** + * @return the lenguaje + */ + @JsonProperty("lenguaje") + public String getLenguaje() { + return lenguaje; + } + + /** + * @param lenguaje the lenguaje to set + */ + public void setLenguaje(String lenguaje) { + this.lenguaje = lenguaje; + } } diff --git a/src/main/java/com/manalejandro/arjion/services/MainService.java b/src/main/java/com/manalejandro/arjion/services/MainService.java index ce8570e..c28a140 100644 --- a/src/main/java/com/manalejandro/arjion/services/MainService.java +++ b/src/main/java/com/manalejandro/arjion/services/MainService.java @@ -1,8 +1,16 @@ package com.manalejandro.arjion.services; -import org.springframework.stereotype.Service; +import java.util.List; + +import com.manalejandro.arjion.model.Documento; -@Service public interface MainService { + public boolean save(Documento doc); + + public long count(); + + public List findAllDocumento(); + + public Documento findOne(String nombre); } diff --git a/src/main/java/com/manalejandro/arjion/services/MainServiceImpl.java b/src/main/java/com/manalejandro/arjion/services/MainServiceImpl.java index 48b97b0..d7774ae 100644 --- a/src/main/java/com/manalejandro/arjion/services/MainServiceImpl.java +++ b/src/main/java/com/manalejandro/arjion/services/MainServiceImpl.java @@ -1,5 +1,51 @@ package com.manalejandro.arjion.services; +import java.util.ArrayList; +import java.util.List; + +import com.manalejandro.arjion.model.Documento; +import com.manalejandro.arjion.repositories.MainRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service public class MainServiceImpl implements MainService { + private final MainRepository mainRepository; + + @Autowired + public MainServiceImpl(MainRepository mainRepository) { + this.mainRepository = mainRepository; + } + + @Override + public boolean save(Documento doc) { + if (!mainRepository.existsById(doc.nombre)) { + if (mainRepository.save(doc) != null) + return true; + else + return false; + } else + return false; + } + + @Override + public long count() { + return mainRepository.count(); + } + + @Override + public List findAllDocumento() { + List docs = new ArrayList(); + mainRepository.findAll().forEach(doc -> { + docs.add((Documento) doc); + }); + return docs; + } + + @Override + public Documento findOne(String nombre) { + return mainRepository.findById(nombre).get(); + } } diff --git a/src/main/java/com/manalejandro/arjion/vo/DetailVO.java b/src/main/java/com/manalejandro/arjion/vo/DetailVO.java new file mode 100644 index 0000000..5faed80 --- /dev/null +++ b/src/main/java/com/manalejandro/arjion/vo/DetailVO.java @@ -0,0 +1,22 @@ +package com.manalejandro.arjion.vo; + +import com.manalejandro.arjion.model.Documento; + +public class DetailVO { + + private Documento documento; + + /** + * @return the documento + */ + public Documento getDocumento() { + return documento; + } + + /** + * @param documento the documento to set + */ + public void setDocumento(Documento documento) { + this.documento = documento; + } +} \ No newline at end of file diff --git a/src/main/java/com/manalejandro/arjion/vo/DocumentoVO.java b/src/main/java/com/manalejandro/arjion/vo/DocumentoVO.java index e1d62bb..e255b25 100644 --- a/src/main/java/com/manalejandro/arjion/vo/DocumentoVO.java +++ b/src/main/java/com/manalejandro/arjion/vo/DocumentoVO.java @@ -3,9 +3,13 @@ package com.manalejandro.arjion.vo; import java.util.List; import com.manalejandro.arjion.model.Archivo; +import com.manalejandro.arjion.model.Documento; public class DocumentoVO { + private List archivos; + private long count; + private List documentos; /** * @return the archivos @@ -20,4 +24,26 @@ public class DocumentoVO { public void setArchivos(List archivos) { this.archivos = archivos; } + + public long getCount() { + return count; + } + + public void setCount(long count) { + this.count = count; + } + + /** + * @return the documentos + */ + public List getDocumentos() { + return documentos; + } + + /** + * @param documentos the documentos to set + */ + public void setDocumentos(List documentos) { + this.documentos = documentos; + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6152e74..3fffee7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -12,5 +12,5 @@ spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false -spring.servlet.multipart.max-file-size=10MB -spring.servlet.multipart.max-request-size=20MB \ No newline at end of file +spring.servlet.multipart.max-file-size=20MB +spring.servlet.multipart.max-request-size=100MB \ No newline at end of file diff --git a/src/main/resources/elasticsearch/mapping.json b/src/main/resources/elasticsearch/mapping.json index 35097cf..e6b8702 100644 --- a/src/main/resources/elasticsearch/mapping.json +++ b/src/main/resources/elasticsearch/mapping.json @@ -1,16 +1,28 @@ { - "documento": { - "properties": { - "@timestamp": { - "type": "date", - "format": "strict_date_optional_time||epoch_millis" - }, - "@version": { - "type": "keyword" - }, - "id": { - "type": "long" - } - } - } + "documento": { + "properties": { + "@timestamp": { + "type": "date", + "format": "strict_date_optional_time||epoch_millis" + }, + "@version": { + "type": "keyword" + }, + "nombre": { + "type": "text" + }, + "tamano": { + "type": "long" + }, + "metadata": { + "type": "text" + }, + "contenido": { + "type": "text" + }, + "lenguaje": { + "type": "keyword" + } + } + } } \ No newline at end of file diff --git a/src/main/resources/static/css/main.css b/src/main/resources/static/css/main.css index e69de29..99e9a97 100644 --- a/src/main/resources/static/css/main.css +++ b/src/main/resources/static/css/main.css @@ -0,0 +1,4 @@ +hr { + width: 100%; + text-align: center; +} \ No newline at end of file diff --git a/src/main/resources/templates/detail.html b/src/main/resources/templates/detail.html new file mode 100644 index 0000000..efc6f7a --- /dev/null +++ b/src/main/resources/templates/detail.html @@ -0,0 +1,46 @@ + + + + + + Arjion + + + + + + + + +
+ +

Arjion

+
+

[[${detailVO.documento.nombre}]]

+
+
+
+
+
+ Tamaño + [[${detailVO.documento.tamano}]] bytes + Lenguaje + [[${detailVO.documento.lenguaje}]] + Metadatos + [[${detailVO.documento.metadata}]] + Contenido +
[[${detailVO.documento.contenido}]]
+ +
+
+
+
+
+ + + + 2018 +
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/exists.html b/src/main/resources/templates/exists.html new file mode 100644 index 0000000..44c5b5c --- /dev/null +++ b/src/main/resources/templates/exists.html @@ -0,0 +1,18 @@ + + + + + + Error + + + + + + +

Error

+

El archivo ya existe o hubo un error

+ + + + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index dfe1b63..11a1fc6 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -2,57 +2,83 @@ - - Arjion - - - - - + + Arjion + + + + + -
-

Arjion

-
-
-
-
- - - - - - - -
-
-
-
-
-
- Nombre - [[${doc.nombre}]] - Tamaño - [[${doc.tamano}]] bytes - Lenguaje - [[${doc.lenguaje}]] - Metadatos - [[${doc.metadata}]] - Contenido -
[[${doc.contenido}]]
-
-
-
-
- 2018 -
+
+ +

Arjion

+
+

[[${documentoVO.count}]] archivos

+
+
+
+
+ + + + + + + +
+
+
+
+
+
+
+
+ Nombre + [[${arc.nombre}]] + Tamaño + [[${arc.tamano}]] bytes + Lenguaje + [[${arc.lenguaje}]] + Metadatos + [[${arc.metadata}]] + Contenido +
[[${arc.contenido}]]
+ +
+
+
+
+
+
+
+
+
+
+ + [[${doc.nombre}]] - + download +
[[${doc.tamano}]] bytes +
[[${doc.lenguaje}]]
+ [[${#strings.abbreviate(doc.metadata,200)}]] + [[${#strings.abbreviate(doc.contenido,200)}]] +
+ +
+
+
+
+ \ No newline at end of file