diff --git a/README.md b/README.md index cf5f364..e7ad48a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,17 @@ # Storage-service Сервис обмена файлами большого размера -## API +# Realization +Файлы хранятся не в базе данных, а в самостоятельном хранилице на сервере, +в базе хранятся адреса файлов, их описания, + + +# API - getFile - postFile - deleteFile -- listFiles \ No newline at end of file +- listFiles + +# Servlets + +- viewFile \ No newline at end of file diff --git a/pom.xml b/pom.xml index ae4372a..a81b46a 100644 --- a/pom.xml +++ b/pom.xml @@ -15,8 +15,8 @@ UTF-8 - 1.7 - 1.7 + 17 + 17 @@ -88,6 +88,30 @@ maven-deploy-plugin 2.8.2 + + + true + + + + org.wildfly.plugins + wildfly-maven-plugin + 4.1.0.Final + + + deploy + + deploy + + + + + molokoin.ru + 9990 + esoe + psalm6912 + storage.war + diff --git a/src/main/scripts/prepare.sql b/src/main/docs/scripts/prepare.sql similarity index 100% rename from src/main/scripts/prepare.sql rename to src/main/docs/scripts/prepare.sql diff --git a/src/main/java/ru/molokoin/storage/api/RestStorageService.java b/src/main/java/ru/molokoin/storage/api/RestStorageService.java index 27ceb40..e45464a 100644 --- a/src/main/java/ru/molokoin/storage/api/RestStorageService.java +++ b/src/main/java/ru/molokoin/storage/api/RestStorageService.java @@ -1,9 +1,20 @@ package ru.molokoin.storage.api; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; import java.util.Collection; +import java.util.Set; import jakarta.ejb.EJB; +import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletResponse; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -12,8 +23,9 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import ru.molokoin.storage.beans.HardDrive; +import ru.molokoin.storage.beans.StorageFace; import ru.molokoin.storage.entities.ContentEntity; -import ru.molokoin.storage.services.StorageFace; @Path("content") public class RestStorageService { @@ -30,14 +42,72 @@ public class RestStorageService { * * @return */ + // @GET + // @Produces(MediaType.APPLICATION_XML) + // public Collection getInfo(){ + // System.out.println("Передача данных обо всех файлах ..."); + // Collection cce = storage.getInfo(); + // return cce; + // } + + /** + * Получение сведений о контенте из файловой системы + * @return + */ @GET @Produces(MediaType.APPLICATION_XML) public Collection getInfo(){ - System.out.println("Передача данных обо всех файлах ..."); - Collection cce = storage.getInfo(); + System.out.println("Передача данных о контенте из файловой системы ..."); + Collection cce = new ArrayList<>(); + try { + Set list = HardDrive.listFiles(HardDrive.getRoot()); + int i = 1; + for (String path : list) { + ContentEntity ce = new ContentEntity(); + ce.setId((long)i); + i++; + ce.setFilename(Paths.get(path).toString()); + ce.setLocation(HardDrive.getRoot()); + cce.add(ce); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } return cce; } + @GET + @Path("q/{id}") + @Produces(MediaType.APPLICATION_XML) + //@Produces(MediaType.MULTIPART_FORM_DATA) + public ContentEntity getInfoById(@PathParam("id") Integer id, @Context HttpServletResponse response){ + Collection cce = new ArrayList<>(); + ContentEntity ce = new ContentEntity(); + try { + Set list = HardDrive.listFiles(HardDrive.getRoot()); + int i = 1; + for (String path : list) { + if (id == i) { + ce.setId((long)i); + ce.setFilename(Paths.get(path).toString()); + ce.setLocation(HardDrive.getRoot()); + cce.add(ce); + } + i++; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return ce; + } + + + + + + // @GET // @Path("{id}") // @Produces(MediaType.APPLICATION_OCTET_STREAM) @@ -49,12 +119,54 @@ public class RestStorageService { // } // @GET + // @Path("{id}") // @Produces(MediaType.APPLICATION_OCTET_STREAM) - // public Response getFile() { - // File file = ... // Initialize this to the File path you want to serve. - // return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM) - // .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) //optional - // .build(); + // public File getFile(@PathParam("id") Integer id, @Context HttpServletResponse response) { + // ContentEntity ce = getInfoById(1, response); + + // File file = new File(ce.getLocation()); // Initialize this to the File path you want to serve. + // // return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM) + // // .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) //optional + // // .build(); + // return file; // } + /** + * Получение файла (*.pdf) на просмотр по id + * !!! добавить возможность открытия файлов других типов + * @param id + * @param response + * @return + * @throws IOException + */ + @GET + @Path("{id}") + public Response viewContent(@PathParam("id") Integer id, + @Context HttpServletResponse response) throws IOException { + try { + System.out.println(">>>>>>>>>>>>>>>> SHOW the FILE <<<<<<<<<<<<<<<"); + ContentEntity ce = getInfoById(id, response); + java.nio.file.Path path = Paths.get(ce.getLocation() + File.separator + ce.getFilename()); + System.out.println("------- PATH: " + path.toString()); + long size = Files.size(path); + response.setContentLength((int) size); + // response.setContentType("text/markdown"); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/pdf"); + response.setHeader("Content-disposition","inline; filename=\"" + ce.getFilename() + "\""); + ServletOutputStream outStream = response.getOutputStream(); + byte[] buffer = new byte[1024]; + FileInputStream fis = new FileInputStream(path.toFile()); + // HardDrive.getBais(id); + int length = 0; + while ((fis != null) && ((length = fis.read(buffer)) != -1)) { + outStream.write(buffer, 0, length); + } + fis.close(); + outStream.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + return Response.ok().build(); + } } diff --git a/src/main/java/ru/molokoin/storage/beans/HardDrive.java b/src/main/java/ru/molokoin/storage/beans/HardDrive.java new file mode 100644 index 0000000..4411bae --- /dev/null +++ b/src/main/java/ru/molokoin/storage/beans/HardDrive.java @@ -0,0 +1,78 @@ +package ru.molokoin.storage.beans; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import ru.molokoin.storage.entities.ContentEntity; + +/** + * Методы работы с данными на жестком диске + */ +public class HardDrive { + public static String root = "/srv/apps/home/exchange"; + + public static String getRoot() { + return root; + } + public static void setRoot(String root) { + HardDrive.root = root; + } + /** + * Метод, возвращающий список файлов в корне хранилища приложения + * @return + * @throws IOException + */ + public static Set listFiles(String path) throws IOException{ + Stream stream = Files.list(Paths.get(path)); + Set list = stream + .filter(file -> !Files.isDirectory(file)) + .map(Path::getFileName) + .map(Path::toString) + .collect(Collectors.toSet()); + stream.close(); + return list; + } + + private static HashMap getMap(String path) throws IOException{ + Stream stream = Files.list(Paths.get(path)); + HashMap map = new HashMap<>(); + return map; + } + + + public static void showFiles(File[] files) { + for (File file : files) { + if (file.isDirectory()) { + System.out.println("Directory: " + file.getAbsolutePath()); + showFiles(file.listFiles()); // Calls same method again. + } else { + System.out.println("File: " + file.getAbsolutePath()); + } + } + } + + public static Path getFileById(int id) throws IOException{ + Path path = null; + Set set = listFiles(root); + int i = 0; + for (String s : set) { + i++; + System.out.println("CURRENT FILE >> " + s); + if (i == id){ + path = Paths.get(s); + System.out.println("SUCCESS >> " + path.toString()); + } + } + return path; + } + +} diff --git a/src/main/java/ru/molokoin/storage/services/Storage.java b/src/main/java/ru/molokoin/storage/beans/Storage.java similarity index 59% rename from src/main/java/ru/molokoin/storage/services/Storage.java rename to src/main/java/ru/molokoin/storage/beans/Storage.java index 2b63bf8..f663a80 100644 --- a/src/main/java/ru/molokoin/storage/services/Storage.java +++ b/src/main/java/ru/molokoin/storage/beans/Storage.java @@ -1,4 +1,4 @@ -package ru.molokoin.storage.services; +package ru.molokoin.storage.beans; import java.util.ArrayList; import java.util.List; @@ -6,21 +6,34 @@ import java.util.List; import jakarta.ejb.Singleton; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; +import jakarta.persistence.Query; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; import jakarta.ws.rs.core.Response; import ru.molokoin.storage.entities.ContentEntity; @Singleton public class Storage implements StorageFace{ - @PersistenceContext (unitName="Storage") + @PersistenceContext (unitName="default") private EntityManager em; + @Override + public List findAll(Class clazz) { + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(clazz); + //Metamodel m = em.getMetamodel(); + Root obj = cq.from(clazz); + return em.createQuery(cq.select(obj)).getResultList(); + } + @Override public List getInfo() { - List list = new ArrayList<>(); System.out.println("getInfo()"); //Обращаемся к базе и запрашиваем сведения - - + String sql = "SELECT id, filename, location FROM Storage"; + Query query = em.createNativeQuery(sql, ContentEntity.class); + List list = query.getResultList(); return list; } diff --git a/src/main/java/ru/molokoin/storage/services/StorageFace.java b/src/main/java/ru/molokoin/storage/beans/StorageFace.java similarity index 90% rename from src/main/java/ru/molokoin/storage/services/StorageFace.java rename to src/main/java/ru/molokoin/storage/beans/StorageFace.java index 4f471e4..46bb90c 100644 --- a/src/main/java/ru/molokoin/storage/services/StorageFace.java +++ b/src/main/java/ru/molokoin/storage/beans/StorageFace.java @@ -1,4 +1,4 @@ -package ru.molokoin.storage.services; +package ru.molokoin.storage.beans; import java.util.List; @@ -12,6 +12,8 @@ import ru.molokoin.storage.entities.ContentEntity; */ @Local public interface StorageFace { + + List findAll(Class clazz); /** * Коллекция сведений о файлах, доступных в хранилице * полученная из базы данных diff --git a/src/main/java/ru/molokoin/storage/entities/ContentEntity.java b/src/main/java/ru/molokoin/storage/entities/ContentEntity.java index e5a318d..9cc8fbd 100644 --- a/src/main/java/ru/molokoin/storage/entities/ContentEntity.java +++ b/src/main/java/ru/molokoin/storage/entities/ContentEntity.java @@ -17,7 +17,7 @@ import jakarta.xml.bind.annotation.XmlRootElement; */ @Entity @XmlRootElement(name = "storage") -@Table(name = "Storage", schema = "home", catalog = "")//поправить схему +@Table(name = "Storage", schema = "home", catalog = "")//поправить схему? public class ContentEntity implements Serializable{ @Id //уникальный идентификатор строки @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -33,8 +33,6 @@ public class ContentEntity implements Serializable{ @Column(name = "location", length = 1000) String location; //Путь к файлу на локальной машине - byte[] content; //массив байткода - содержимое файла - public Long getId() { return id; } @@ -59,14 +57,6 @@ public class ContentEntity implements Serializable{ this.location = location; } - public byte[] getContent() { - return content; - } - - public void setContent(byte[] content) { - this.content = content; - } - @Override public int hashCode() { final int prime = 31; diff --git a/src/main/resources/META-INF/persistance.xml b/src/main/resources/META-INF/persistence.xml similarity index 87% rename from src/main/resources/META-INF/persistance.xml rename to src/main/resources/META-INF/persistence.xml index 28f7f16..a0d01c9 100644 --- a/src/main/resources/META-INF/persistance.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -1,6 +1,6 @@ - + Подключение к базе molokoin.ru:3306/home java:/home ru.molokoin.storage.entities.ContentEntity diff --git a/target/classes/META-INF/persistance.xml b/target/classes/META-INF/persistence.xml similarity index 87% rename from target/classes/META-INF/persistance.xml rename to target/classes/META-INF/persistence.xml index 28f7f16..a0d01c9 100644 --- a/target/classes/META-INF/persistance.xml +++ b/target/classes/META-INF/persistence.xml @@ -1,6 +1,6 @@ - + Подключение к базе molokoin.ru:3306/home java:/home ru.molokoin.storage.entities.ContentEntity diff --git a/target/classes/ru/molokoin/storage/api/RestConfig.class b/target/classes/ru/molokoin/storage/api/RestConfig.class index f5c1c85..291999f 100644 Binary files a/target/classes/ru/molokoin/storage/api/RestConfig.class and b/target/classes/ru/molokoin/storage/api/RestConfig.class differ diff --git a/target/classes/ru/molokoin/storage/api/RestStorageService.class b/target/classes/ru/molokoin/storage/api/RestStorageService.class index 9c5de45..e87b662 100644 Binary files a/target/classes/ru/molokoin/storage/api/RestStorageService.class and b/target/classes/ru/molokoin/storage/api/RestStorageService.class differ diff --git a/target/classes/ru/molokoin/storage/beans/HardDrive.class b/target/classes/ru/molokoin/storage/beans/HardDrive.class new file mode 100644 index 0000000..0b2ed93 Binary files /dev/null and b/target/classes/ru/molokoin/storage/beans/HardDrive.class differ diff --git a/target/classes/ru/molokoin/storage/beans/Storage.class b/target/classes/ru/molokoin/storage/beans/Storage.class new file mode 100644 index 0000000..90f0dbc Binary files /dev/null and b/target/classes/ru/molokoin/storage/beans/Storage.class differ diff --git a/target/classes/ru/molokoin/storage/beans/StorageFace.class b/target/classes/ru/molokoin/storage/beans/StorageFace.class new file mode 100644 index 0000000..4036863 Binary files /dev/null and b/target/classes/ru/molokoin/storage/beans/StorageFace.class differ diff --git a/target/classes/ru/molokoin/storage/entities/ContentEntity.class b/target/classes/ru/molokoin/storage/entities/ContentEntity.class index 68779bb..8136903 100644 Binary files a/target/classes/ru/molokoin/storage/entities/ContentEntity.class and b/target/classes/ru/molokoin/storage/entities/ContentEntity.class differ diff --git a/target/classes/ru/molokoin/storage/services/Storage.class b/target/classes/ru/molokoin/storage/services/Storage.class deleted file mode 100644 index a4d275a..0000000 Binary files a/target/classes/ru/molokoin/storage/services/Storage.class and /dev/null differ diff --git a/target/classes/ru/molokoin/storage/services/StorageFace.class b/target/classes/ru/molokoin/storage/services/StorageFace.class deleted file mode 100644 index f3f2038..0000000 Binary files a/target/classes/ru/molokoin/storage/services/StorageFace.class and /dev/null differ diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 0000000..61ee866 --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,4 @@ +#Created by Apache Maven 3.8.5 +groupId=ru.molokoin +artifactId=storage +version=1.0 diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..9f289e4 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,6 @@ +ru\molokoin\storage\entities\ContentEntity.class +ru\molokoin\storage\beans\StorageFace.class +ru\molokoin\storage\api\RestStorageService.class +ru\molokoin\storage\api\RestConfig.class +ru\molokoin\storage\beans\HardDrive.class +ru\molokoin\storage\beans\Storage.class diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..aabb5cb --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,6 @@ +C:\Users\Strannik\Documents\esoe\code\storage\src\main\java\ru\molokoin\storage\beans\Storage.java +C:\Users\Strannik\Documents\esoe\code\storage\src\main\java\ru\molokoin\storage\beans\StorageFace.java +C:\Users\Strannik\Documents\esoe\code\storage\src\main\java\ru\molokoin\storage\entities\ContentEntity.java +C:\Users\Strannik\Documents\esoe\code\storage\src\main\java\ru\molokoin\storage\api\RestConfig.java +C:\Users\Strannik\Documents\esoe\code\storage\src\main\java\ru\molokoin\storage\beans\HardDrive.java +C:\Users\Strannik\Documents\esoe\code\storage\src\main\java\ru\molokoin\storage\api\RestStorageService.java diff --git a/target/storage.war b/target/storage.war new file mode 100644 index 0000000..f4309ed Binary files /dev/null and b/target/storage.war differ diff --git a/target/storage/WEB-INF/classes/META-INF/persistence.xml b/target/storage/WEB-INF/classes/META-INF/persistence.xml new file mode 100644 index 0000000..a0d01c9 --- /dev/null +++ b/target/storage/WEB-INF/classes/META-INF/persistence.xml @@ -0,0 +1,9 @@ + + + + Подключение к базе molokoin.ru:3306/home + java:/home + ru.molokoin.storage.entities.ContentEntity + true + + \ No newline at end of file diff --git a/target/storage/WEB-INF/classes/ru/molokoin/storage/api/RestConfig.class b/target/storage/WEB-INF/classes/ru/molokoin/storage/api/RestConfig.class new file mode 100644 index 0000000..291999f Binary files /dev/null and b/target/storage/WEB-INF/classes/ru/molokoin/storage/api/RestConfig.class differ diff --git a/target/storage/WEB-INF/classes/ru/molokoin/storage/api/RestStorageService.class b/target/storage/WEB-INF/classes/ru/molokoin/storage/api/RestStorageService.class new file mode 100644 index 0000000..5e619b1 Binary files /dev/null and b/target/storage/WEB-INF/classes/ru/molokoin/storage/api/RestStorageService.class differ diff --git a/target/storage/WEB-INF/classes/ru/molokoin/storage/beans/HardDrive.class b/target/storage/WEB-INF/classes/ru/molokoin/storage/beans/HardDrive.class new file mode 100644 index 0000000..64d2f9a Binary files /dev/null and b/target/storage/WEB-INF/classes/ru/molokoin/storage/beans/HardDrive.class differ diff --git a/target/storage/WEB-INF/classes/ru/molokoin/storage/beans/Storage.class b/target/storage/WEB-INF/classes/ru/molokoin/storage/beans/Storage.class new file mode 100644 index 0000000..90f0dbc Binary files /dev/null and b/target/storage/WEB-INF/classes/ru/molokoin/storage/beans/Storage.class differ diff --git a/target/storage/WEB-INF/classes/ru/molokoin/storage/beans/StorageFace.class b/target/storage/WEB-INF/classes/ru/molokoin/storage/beans/StorageFace.class new file mode 100644 index 0000000..4036863 Binary files /dev/null and b/target/storage/WEB-INF/classes/ru/molokoin/storage/beans/StorageFace.class differ diff --git a/target/storage/WEB-INF/classes/ru/molokoin/storage/entities/ContentEntity.class b/target/storage/WEB-INF/classes/ru/molokoin/storage/entities/ContentEntity.class new file mode 100644 index 0000000..8136903 Binary files /dev/null and b/target/storage/WEB-INF/classes/ru/molokoin/storage/entities/ContentEntity.class differ diff --git a/target/storage/WEB-INF/lib/jakarta.activation-api-2.1.0.jar b/target/storage/WEB-INF/lib/jakarta.activation-api-2.1.0.jar new file mode 100644 index 0000000..b125985 Binary files /dev/null and b/target/storage/WEB-INF/lib/jakarta.activation-api-2.1.0.jar differ diff --git a/target/storage/WEB-INF/lib/jakarta.ws.rs-api-3.1.0.jar b/target/storage/WEB-INF/lib/jakarta.ws.rs-api-3.1.0.jar new file mode 100644 index 0000000..80670a1 Binary files /dev/null and b/target/storage/WEB-INF/lib/jakarta.ws.rs-api-3.1.0.jar differ diff --git a/target/storage/WEB-INF/lib/jakarta.xml.bind-api-4.0.0.jar b/target/storage/WEB-INF/lib/jakarta.xml.bind-api-4.0.0.jar new file mode 100644 index 0000000..b10d606 Binary files /dev/null and b/target/storage/WEB-INF/lib/jakarta.xml.bind-api-4.0.0.jar differ diff --git a/target/storage/WEB-INF/web.xml b/target/storage/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/target/storage/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/target/storage/index.jsp b/target/storage/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/target/storage/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ +