esoe 6 months ago
parent
commit
ac896bccc3
  1. 5
      storage_rs/docker-compose.yaml
  2. 10
      storage_rs/pom.xml
  3. 52
      storage_rs/src/main/java/ru/molokoin/storage_rs/controller/StorageController.java
  4. 104
      storage_rs/src/main/java/ru/molokoin/storage_rs/model/XlsxCell.java
  5. 137
      storage_rs/src/main/java/ru/molokoin/storage_rs/model/XlsxDocument.java
  6. 40
      storage_rs/src/main/java/ru/molokoin/storage_rs/service/StorageService.java
  7. 9
      storage_rs/src/main/java/ru/molokoin/storage_rs/service/StorageServiceFace.java
  8. 24
      storage_rs/src/main/resources/templates/documents.html
  9. 24
      storage_rs/src/main/resources/templates/upload.html
  10. 0
      storage_rs/src/main/resources/templates/view.html

5
storage_rs/docker-compose.yaml

@ -10,7 +10,10 @@ services: @@ -10,7 +10,10 @@ services:
- 82:8282
restart: unless-stopped
volumes:
- ./uploads:${DATA}
- tech_storage_rs:${DATA}
volumes:
tech_storage_rs:
external: true
networks:
default:
external:

10
storage_rs/pom.xml

@ -22,6 +22,16 @@ @@ -22,6 +22,16 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

52
storage_rs/src/main/java/ru/molokoin/storage_rs/controller/StorageController.java

@ -5,11 +5,15 @@ import java.util.List; @@ -5,11 +5,15 @@ import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import ru.molokoin.storage_rs.model.Document;
import ru.molokoin.storage_rs.model.XlsxCell;
import ru.molokoin.storage_rs.model.XlsxDocument;
import ru.molokoin.storage_rs.service.StorageServiceFace;
/**
@ -20,6 +24,7 @@ import ru.molokoin.storage_rs.service.StorageServiceFace; @@ -20,6 +24,7 @@ import ru.molokoin.storage_rs.service.StorageServiceFace;
* файлов пользователя/сторонего сервиса
*/
@Controller
@RequestMapping(path = "/", consumes = {"*/*"})
public class StorageController {
/**
* получаем в переменную root
@ -37,15 +42,6 @@ public class StorageController { @@ -37,15 +42,6 @@ public class StorageController {
this.storage = storage;
}
/**
* Отдаем пользователю страничку загрузки файлов на сервер
* @return
*/
@GetMapping("/upload")
public String uploadFace(){
return "upload";
}
/**
* Загружаем файл пользователя и возвращаем список файлов на сервере
* @return
@ -53,13 +49,47 @@ public class StorageController { @@ -53,13 +49,47 @@ public class StorageController {
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file){
storage.store(file);
return "list";
return "redirect:/uploads";
}
/**
* Возвращаем пользователю список файлов с сервера
* @param model
* @return
*/
@GetMapping("/uploads")
public String list(Model model){
model.addAttribute("documents", storage.list());
return "list";
return "documents";
}
/**
* Удаление файла из хранилища
* @param name
* @return
*/
@GetMapping("/delete/{name}")
public String delete(@PathVariable String name) {
storage.delete(name);
return "redirect:/uploads";
}
/**
* Удаление файла из хранилища
* @param name
* @return
*/
@GetMapping("/view/{name}")
public String view(@PathVariable String name) {
Document doc = storage.find(name);
XlsxDocument xlsx = new XlsxDocument(doc);
for (List<XlsxCell> row : xlsx.getData().values()) {
for (XlsxCell xlsxCell : row) {
System.out.println(xlsxCell.getContent());
}
}
return "redirect:/uploads";
}
}

104
storage_rs/src/main/java/ru/molokoin/storage_rs/model/XlsxCell.java

@ -0,0 +1,104 @@ @@ -0,0 +1,104 @@
package ru.molokoin.storage_rs.model;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public class XlsxCell {
private String content; //содержимое ячейки
private String textColor; //цвет шрифта
private String bgColor; // цвет заливки ячейки
private String textSize; //8-12-14-24 размеры шрифта
private int textWeight; //жирность: bold (900), normal(500),
private String type;// STRING, NUMERIC, BOOLEAN, FORMULA
public XlsxCell(String content){
this.content = content;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
/**
* @return the textColor
*/
public String getTextColor() {
return textColor;
}
/**
* @param textColor the textColor to set
*/
public void setTextColor(String textColor) {
this.textColor = textColor;
}
/**
* @return the bgColor
*/
public String getBgColor() {
return bgColor;
}
/**
* @param bgColor the bgColor to set
*/
public void setBgColor(String bgColor) {
this.bgColor = bgColor;
}
/**
* @return the textSize
*/
public String getTextSize() {
return textSize;
}
/**
* @param textSize the textSize to set
*/
public void setTextSize(String textSize) {
this.textSize = textSize;
}
/**
* @return the textWeight
*/
public int getTextWeight() {
return textWeight;
}
/**
* @param textWeight the textWeight to set
*/
public void setTextWeight(int textWeight) {
this.textWeight = textWeight;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
}

137
storage_rs/src/main/java/ru/molokoin/storage_rs/model/XlsxDocument.java

@ -0,0 +1,137 @@ @@ -0,0 +1,137 @@
package ru.molokoin.storage_rs.model;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public class XlsxDocument {
private Map<Integer, List<XlsxCell>> data;
private Document document;
public XlsxDocument(Document document){
this.document = document;
this.data = init(document);
}
private Map<Integer, List<XlsxCell>> init(Document document) {
FileInputStream file;
Workbook workbook;
try {
file = new FileInputStream(new File(document.getPath()));
workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
data = new HashMap<>();
Integer i = 0;
for (Row row : sheet) {
List<XlsxCell> xlsxCells = null;
XlsxCell xlsxCell = null;
for (Cell cell : row) {
xlsxCells = new ArrayList<>();
switch (cell.getCellType()) {
case STRING: {
xlsxCell = new XlsxCell(cell.getStringCellValue() + "");
} break;
case NUMERIC: {
xlsxCell = new XlsxCell(cell.getNumericCellValue() + "");
} break;
case BOOLEAN: {
xlsxCell = new XlsxCell(cell.getBooleanCellValue() + "");
} break;
case FORMULA: {
xlsxCell = new XlsxCell(cell.getCellFormula() + "");
} break;
default:
}
xlsxCells.add(xlsxCell);
}
data.put(i, xlsxCells);
i++;
}
}catch (IOException e) {
System.out.println("Не читается файл: " + e.getMessage());
}
return data;
}
/**
* @return the data
*/
public Map<Integer, List<XlsxCell>> getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(Map<Integer, List<XlsxCell>> data) {
this.data = data;
}
/**
* @return the document
*/
public Document getDocument() {
return document;
}
/**
* @param document the document to set
*/
public void setDocument(Document document) {
this.document = document;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((data == null) ? 0 : data.hashCode());
result = prime * result + ((document == null) ? 0 : document.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
XlsxDocument other = (XlsxDocument) obj;
if (data == null) {
if (other.data != null)
return false;
} else if (!data.equals(other.data))
return false;
if (document == null) {
if (other.document != null)
return false;
} else if (!document.equals(other.document))
return false;
return true;
}
}

40
storage_rs/src/main/java/ru/molokoin/storage_rs/service/StorageService.java

@ -17,6 +17,7 @@ import org.springframework.stereotype.Service; @@ -17,6 +17,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import jakarta.annotation.PostConstruct;
import ru.molokoin.storage_rs.config.StorageConfiguration;
import ru.molokoin.storage_rs.model.Document;
@ -69,7 +70,7 @@ public class StorageService implements StorageServiceFace{ @@ -69,7 +70,7 @@ public class StorageService implements StorageServiceFace{
for (String file : new ArrayList<>(Arrays.asList(files))) {
Document doc = new Document();
doc.setPath(file);
doc.setPath(root() + "/" + file);
/**
* Извлекаем имя файла
@ -82,13 +83,7 @@ public class StorageService implements StorageServiceFace{ @@ -82,13 +83,7 @@ public class StorageService implements StorageServiceFace{
* файлы, загруженные приложением ошибок вызывать не будут,
* только файлы перемещенные в том из операционной системы.
*/
long size = 0;
try {
Path p = Paths.get(file);
size = Files.size(p);
} catch (IOException e) {
System.out.println(e.getMessage() + " >>> Ошибка! не удалось определить размер файла!");
}
long size = new File(doc.getPath()).length();
doc.setSize(size);
/**
@ -103,6 +98,15 @@ public class StorageService implements StorageServiceFace{ @@ -103,6 +98,15 @@ public class StorageService implements StorageServiceFace{
return docs;
}
/**
* Найти документ по имени
*/
public Document find(String name){
return list().stream().filter(doc -> name.equals(doc.getName()))
.findAny()
.orElse(null);
}
@Override
public void clear() {
// TODO Auto-generated method stub
@ -115,10 +119,24 @@ public class StorageService implements StorageServiceFace{ @@ -115,10 +119,24 @@ public class StorageService implements StorageServiceFace{
throw new UnsupportedOperationException("Unimplemented method 'load'");
}
/**
* Удаление файла из хранилища
* - по имени файла
*/
@Override
public boolean delete(long id) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'delete'");
public boolean delete(String name) {
Path path = Paths.get(find(name).getPath());
if (Files.exists(path)){
try {
Files.delete(path);
} catch (IOException e) {
System.out.println("Не удалось удалить файл " + name);
System.out.println(e.getMessage());
return false;
}
return true;
}
return true;
}
@Override

9
storage_rs/src/main/java/ru/molokoin/storage_rs/service/StorageServiceFace.java

@ -33,6 +33,13 @@ public interface StorageServiceFace { @@ -33,6 +33,13 @@ public interface StorageServiceFace {
*/
public List<Document> list();
/**
* Поиск документа из списка по имени
* @param name
* @return
*/
public Document find(String name);
/**
* Удаление всех файлов, очистка хранилища
*/
@ -50,7 +57,7 @@ public interface StorageServiceFace { @@ -50,7 +57,7 @@ public interface StorageServiceFace {
* @param id
* @return
*/
public boolean delete(long id);
public boolean delete(String name);
/**
* Сохранение файла в хранилище

24
storage_rs/src/main/resources/templates/list.html → storage_rs/src/main/resources/templates/documents.html

@ -18,20 +18,21 @@ xmlns:th="http://www.thymeleaf.org"> @@ -18,20 +18,21 @@ xmlns:th="http://www.thymeleaf.org">
<div class="main-wraper">
<h2>Перечень файлов:</h2>
<form th:action="@{/uploads}" method="get" th:object="${doc}"></form>
<form method="post" th:action="@{/upload}" enctype="multipart/form-data">
<div>
<input type="file" name="file">
</div>
<button type="submit">Upload File</button>
</form>
<table>
<thead>
<tr>
<th>name</th>
<th>path</th>
<th>extension</th>
<th>size</th>
<th>size(bytes)</th>
<th>
<form method="post" th:action="@{/upload}" enctype="multipart/form-data">
<div>
<input type="file" name="file">
</div>
<button type="submit">Upload File</button>
</form>
</th>
</tr>
</thead>
@ -51,9 +52,14 @@ xmlns:th="http://www.thymeleaf.org"> @@ -51,9 +52,14 @@ xmlns:th="http://www.thymeleaf.org">
</td>
<td>
<!-- <form th:action="@{/student/delete/{id}(id=${stu.id})}" th:method="get">
<form th:action="@{/delete/{name}(name=${doc.name})}" th:method="get">
<input type="submit" value="X"/>
</form> -->
</form>
</td>
<td>
<form th:action="@{/view/{name}(name=${doc.name})}" th:method="get">
<input type="submit" value="VIEW"/>
</form>
</td>
</tr>
</tbody>

24
storage_rs/src/main/resources/templates/upload.html

@ -1,24 +0,0 @@ @@ -1,24 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>upload-page</title>
<script src="https://cdn.jsdelivr.net/npm/@webcomponents/webcomponentsjs@2/webcomponents-loader.min.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@1/src/zero-md.min.js"></script>
</head>
<body>
<form method="post" th:action="@{/upload}" enctype="multipart/form-data">
<div>
<input type="file" name="file">
</div>
<button type="submit">Upload File</button>
</form>
<div class="list">
</div>
</body>
</html>

0
storage_rs/src/main/resources/templates/view.html

Loading…
Cancel
Save