diff --git a/src/main/java/com/olympus/apollo/controllers/FeApi/KSVideoGroupController.java b/src/main/java/com/olympus/apollo/controllers/FeApi/KSVideoGroupController.java new file mode 100644 index 0000000..108f6a6 --- /dev/null +++ b/src/main/java/com/olympus/apollo/controllers/FeApi/KSVideoGroupController.java @@ -0,0 +1,30 @@ +package com.olympus.apollo.controllers.FeApi; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import com.olympus.model.apollo.VideoGroup; +import com.olympus.apollo.services.VideoGroupService; + +@RestController +@RequestMapping("/fe-api/video-group") +public class KSVideoGroupController { + + @Autowired + private VideoGroupService videoGroupService; + + + @GetMapping("/project") + public List getVideoGroupByProjectId(@RequestParam String projectId) { + return videoGroupService.findByProjectId(projectId); + } + + @GetMapping("/{id}") + public VideoGroup getVideoGroupById(@PathVariable String id) { + return videoGroupService.getVideoGroupById(id); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/olympus/apollo/controllers/FeApi/KsDocumentController.java b/src/main/java/com/olympus/apollo/controllers/FeApi/KsDocumentController.java index 210a4bb..cb83b40 100644 --- a/src/main/java/com/olympus/apollo/controllers/FeApi/KsDocumentController.java +++ b/src/main/java/com/olympus/apollo/controllers/FeApi/KsDocumentController.java @@ -3,20 +3,14 @@ package com.olympus.apollo.controllers.FeApi; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.UrlResource; import org.springframework.web.bind.annotation.*; import com.olympus.model.apollo.KSDocument; import com.olympus.apollo.repository.KSDocumentRepository; import com.olympus.apollo.services.KSDocumentService; import org.springframework.core.io.Resource; -import org.springframework.core.io.UrlResource; -import java.nio.file.Files; -import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; -import java.nio.file.Path; -import java.nio.file.Paths; @RestController @@ -47,9 +41,9 @@ public class KsDocumentController { @PostMapping("/downloadKSDocument") -public ResponseEntity downloadFile(@RequestBody KSDocument doc) { + public ResponseEntity downloadFile(@RequestBody KSDocument doc) { - return ksDocumentService.downloadKSDocument(doc); + return ksDocumentService.downloadKSDocument(doc); } diff --git a/src/main/java/com/olympus/apollo/controllers/FeApi/KsVideoController.java b/src/main/java/com/olympus/apollo/controllers/FeApi/KsVideoController.java new file mode 100644 index 0000000..1569b62 --- /dev/null +++ b/src/main/java/com/olympus/apollo/controllers/FeApi/KsVideoController.java @@ -0,0 +1,56 @@ +package com.olympus.apollo.controllers.FeApi; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import com.olympus.model.apollo.KSVideo; +import com.olympus.apollo.repository.KSVideoRepository; +import com.olympus.apollo.services.KSVideoService; +import org.springframework.core.io.Resource; +import org.springframework.http.ResponseEntity; + + +@RestController +@RequestMapping("/fe-api/ksvideos") +public class KsVideoController { + + @Autowired + private KSVideoRepository ksVideoREpository; + + @Autowired + private KSVideoService ksVideoService; + + @GetMapping("") + public List getVideos() { + + List result = ksVideoService.findByProjectNameAndApplicationName(); + + return result; + } + @GetMapping("/{id}") + public KSVideo getVideo(@PathVariable String id) { + + KSVideo result = ksVideoREpository.findById(id).get(); + + return result; + } + @GetMapping("/group/{groupId}") + public List getVideoByGroupId(@PathVariable String groupId) { + + List result = ksVideoService.findByGroupId(groupId); + + return result; + } + + + + @PostMapping("/downloadKSVideo") + public ResponseEntity downloadFile(@RequestBody KSVideo doc) { + + return ksVideoService.downloadKSVideo(doc); + + } + +} diff --git a/src/main/java/com/olympus/apollo/controllers/TestController.java b/src/main/java/com/olympus/apollo/controllers/TestController.java index e18d125..a22ffff 100644 --- a/src/main/java/com/olympus/apollo/controllers/TestController.java +++ b/src/main/java/com/olympus/apollo/controllers/TestController.java @@ -62,6 +62,11 @@ public class TestController { return ksIngestor.setDocumentInQueueIngestion(id); } + @GetMapping("test/index_video/{id}") + public IngestionOutput ingestVideoById(@PathVariable String id) { + return ksIngestor.setVideoInQueueIngestion(id); + } + @GetMapping("test/query_vector") public List testSimilaritySearch(@RequestParam String query, @RequestParam String filterQuery) { return ksIngestor.testSimilaritySearch(query, filterQuery); diff --git a/src/main/java/com/olympus/apollo/controllers/VideoController.java b/src/main/java/com/olympus/apollo/controllers/VideoController.java new file mode 100644 index 0000000..3dbeaf3 --- /dev/null +++ b/src/main/java/com/olympus/apollo/controllers/VideoController.java @@ -0,0 +1,69 @@ +package com.olympus.apollo.controllers; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + + +import com.olympus.apollo.repository.KSVideoRepository; +import com.olympus.apollo.services.StorageService; +import com.olympus.dto.VideoUploadDTO; +import com.olympus.model.apollo.KSVideoIngestionInfo; +import com.olympus.model.apollo.KSVideo; + +@RestController +public class VideoController { + + @Autowired + private StorageService storageService; + @Autowired + private KSVideoRepository ksVideoRepository; + + @PostMapping("/upload_video") + public ResponseEntity handleVideoUpload( + @RequestParam("file") MultipartFile file, + @ModelAttribute VideoUploadDTO videoUploadDTO + ) { + String filePath = storageService.store(file,videoUploadDTO.getKsProjectName()); + + KSVideo ksVideo = new KSVideo(); + ksVideo.setFilePath(filePath); + ksVideo.setFileName(file.getOriginalFilename()); + ksVideo.setName(file.getOriginalFilename()); + ksVideo.setDescription(videoUploadDTO.getDescription()); + ksVideo.setIngestionStatus("LOADED"); + + Date now = new Date(); + ksVideo.setIngestionDate(now); + ksVideo.setIngestionDateFormat(new SimpleDateFormat("MM/dd/yy").format(now)); + + + KSVideoIngestionInfo ksVideoIngestionInfo = new KSVideoIngestionInfo(); + ksVideoIngestionInfo.setType(videoUploadDTO.getType()); // != null ? type : "MD_Video" + + HashMap metadata = new HashMap<>(); + metadata.put("KsApplicationName", videoUploadDTO.getKsApplicationName()); + metadata.put("KsDoctype", videoUploadDTO.getKsDocType()); + metadata.put("KsDocSource", videoUploadDTO.getKsDocSource()); + metadata.put("KsFileSource", file.getOriginalFilename()); + metadata.put("KsVideoGroupId", videoUploadDTO.getKsVideoGroupId()); + metadata.put("KsProjectName", videoUploadDTO.getKsProjectName()); + + ksVideoIngestionInfo.setMetadata(metadata); + ksVideoIngestionInfo.setNumberOfChunkToEmbed(videoUploadDTO.getNumberOfChunkToEmbed()); + ksVideoIngestionInfo.setChunkDurationInSeconds(videoUploadDTO.getChunkDurationInSeconds()); + + ksVideo.setIngestionInfo(ksVideoIngestionInfo); + ksVideoRepository.save(ksVideo); + return ResponseEntity.ok(ksVideo); + } + + +} diff --git a/src/main/java/com/olympus/apollo/controllers/VideoGroupController.java b/src/main/java/com/olympus/apollo/controllers/VideoGroupController.java new file mode 100644 index 0000000..45a12d8 --- /dev/null +++ b/src/main/java/com/olympus/apollo/controllers/VideoGroupController.java @@ -0,0 +1,61 @@ +package com.olympus.apollo.controllers; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import com.olympus.model.apollo.VideoGroup; +import com.olympus.apollo.repository.VideoGroupRepository; +import com.olympus.apollo.services.VideoGroupService; +import com.olympus.dto.VideoGroupDTO; + +@RestController +public class VideoGroupController { + + @Autowired + private VideoGroupRepository videoGroupRepository; + + @Autowired + private VideoGroupService videoGroupService; + + @PostMapping("/create_video_group") + public ResponseEntity handleVideoGroupCreation( + @ModelAttribute VideoGroupDTO videoGroupDTO + ) { + VideoGroup videoGroup = new VideoGroup(); + videoGroup.setName(videoGroupDTO.getName()); + videoGroup.setType(videoGroupDTO.getType()); + videoGroup.setProjectId(videoGroupDTO.getProjectId()); + videoGroup.setApplicationId(videoGroupDTO.getApplicationId()); + + Date now = new Date(); + videoGroup.setDate(now); + videoGroup.setDateFormat(new SimpleDateFormat("MM/dd/yy").format(now)); + videoGroupRepository.save(videoGroup); + return ResponseEntity.ok(videoGroup); + } + + @DeleteMapping("/delete/{id}") + public ResponseEntity deleteVideoGroup(@PathVariable String id) { + try { + boolean deleted = videoGroupService.deleteVideoGroup(id); + if (!deleted) { + return ResponseEntity.notFound().build(); + } + + return ResponseEntity.ok(Map.of( + "status", "OK", + "message", "Video group deleted successfully" + )); + } catch (Exception e) { + return ResponseEntity.internalServerError().body(Map.of( + "status", "ERROR", + "message", "Failed to delete video group: " + e.getMessage() + )); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/olympus/apollo/repository/KSVideoRepository.java b/src/main/java/com/olympus/apollo/repository/KSVideoRepository.java new file mode 100644 index 0000000..d3cfe75 --- /dev/null +++ b/src/main/java/com/olympus/apollo/repository/KSVideoRepository.java @@ -0,0 +1,26 @@ +package com.olympus.apollo.repository; + +import java.util.List; + +import org.springframework.data.domain.Sort; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; +import org.springframework.web.bind.annotation.CrossOrigin; + +import com.olympus.model.apollo.KSVideo; + +@RepositoryRestResource(collectionResourceRel = "ksvideos", path = "ksvideos") +@CrossOrigin +public interface KSVideoRepository extends MongoRepository { + + @Query("{ 'ingestionInfo.metadata.KsProjectName': ?0, 'ingestionInfo.metadata.KsApplicationName': ?1 }") + List findByProjectNameAndApplicationName(String projectName, String applicationName, Sort sort); + + @Query("{ 'ingestionInfo.metadata.KsProjectName': ?0 }") + public List findByProjectName(String projectName, Sort sort); + + @Query("{ 'ingestionInfo.metadata.KsVideoGroupId': ?0 }") + public List findByGroupId(String groupId, Sort sort); + +} diff --git a/src/main/java/com/olympus/apollo/repository/VideoGroupRepository.java b/src/main/java/com/olympus/apollo/repository/VideoGroupRepository.java new file mode 100644 index 0000000..123831c --- /dev/null +++ b/src/main/java/com/olympus/apollo/repository/VideoGroupRepository.java @@ -0,0 +1,23 @@ +package com.olympus.apollo.repository; + +import java.util.List; +import java.util.Optional; + +import org.springframework.data.domain.Sort; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; +import org.springframework.web.bind.annotation.CrossOrigin; + +import com.olympus.model.apollo.VideoGroup; + + +@RepositoryRestResource(collectionResourceRel = "video_groups", path = "video_groups") +@CrossOrigin +public interface VideoGroupRepository extends MongoRepository { + + @Query("{ 'projectId': ?0 }") + public List findByProjectId(String projectId, Sort sort); + + public Optional findById(String id); +} \ No newline at end of file diff --git a/src/main/java/com/olympus/apollo/services/KSIngestor.java b/src/main/java/com/olympus/apollo/services/KSIngestor.java index 8a3e922..2a08941 100644 --- a/src/main/java/com/olympus/apollo/services/KSIngestor.java +++ b/src/main/java/com/olympus/apollo/services/KSIngestor.java @@ -24,10 +24,12 @@ import org.springframework.stereotype.Service; import com.olympus.apollo.repository.KSDocumentRepository; import com.olympus.apollo.repository.KSTextsRepository; +import com.olympus.apollo.repository.KSVideoRepository; import com.olympus.dto.IngestionOutput; import com.olympus.model.apollo.KSDocument; import com.olympus.model.apollo.KSIngestionInfo; import com.olympus.model.apollo.KSTexts; +import com.olympus.model.apollo.KSVideo; @Service @@ -39,6 +41,9 @@ public class KSIngestor { @Autowired private KSTextsRepository ksTextsRepository; + @Autowired + private KSVideoRepository ksVideoRepository; + @Autowired private FileSystemStorageService storageService; @@ -338,4 +343,26 @@ public class KSIngestor { return docs; } + + public IngestionOutput setVideoInQueueIngestion(String id) { + IngestionOutput ingestionOutput= new IngestionOutput(); + Optional optionalVideo = ksVideoRepository.findById(id); + if (optionalVideo.isPresent()) { + KSVideo ksVideo = optionalVideo.get(); + if ("LOADED".equals(ksVideo.getIngestionStatus()) || "ERROR".equals(ksVideo.getIngestionStatus())) { + ksVideo.setIngestionStatus("INGESTION_QUEUE"); + ksVideoRepository.save(ksVideo); + ingestionOutput.setMessage("Video added to ingestion queue..."); + ingestionOutput.setStatus("INGESTION_QUEUE"); + return ingestionOutput; + + } else { + ingestionOutput.setMessage("OOPS: Video is already Injected"); + return ingestionOutput; + } + } else { + ingestionOutput.setMessage("OOPS: Video Not found"); + return ingestionOutput; + } + } } diff --git a/src/main/java/com/olympus/apollo/services/KSVideoService.java b/src/main/java/com/olympus/apollo/services/KSVideoService.java new file mode 100644 index 0000000..d52eb75 --- /dev/null +++ b/src/main/java/com/olympus/apollo/services/KSVideoService.java @@ -0,0 +1,78 @@ +package com.olympus.apollo.services; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.core.io.UrlResource; +import org.springframework.data.domain.Sort; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; + +import com.olympus.apollo.repository.KSVideoRepository; +import com.olympus.apollo.security.entity.User; +import com.olympus.model.apollo.KSVideo; + +@Service +public class KSVideoService { + private Logger logger = LoggerFactory.getLogger(KSVideoService.class); + + @Autowired + private KSVideoRepository ksVideoRepository; + + public List findByProjectNameAndApplicationName() { + User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + + try { + return ksVideoRepository.findByProjectName(principal.getSelectedProject().getInternal_name(), Sort.by(Sort.Direction.DESC, "ingestionDate")); + } catch (Exception e) { + logger.error("Error in findByProjectNameAndApplicationName function: " + e.getMessage()); + } + + return null; + } + + public List findByGroupId(String groupId) { + return ksVideoRepository.findByGroupId(groupId, Sort.by(Sort.Direction.DESC, "ingestionDate")); + } + + public ResponseEntity downloadKSVideo(KSVideo video) { + + logger.info("downloadKSVideo function:"); + + try { + // Percorso al file + Path filePath = Paths.get(video.getFilePath()).normalize(); + Resource resource = new UrlResource(filePath.toUri()); + + if (!resource.exists()) { + return ResponseEntity.notFound().build(); + } + + // Determina il tipo MIME dinamicamente + String contentType = Files.probeContentType(filePath); + if (contentType == null) { + // Tipo MIME predefinito se non determinabile + contentType = "application/octet-stream"; + } + + // Configurazione della risposta HTTP + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_TYPE, contentType) // Tipo MIME dinamico + .body(resource); + } catch (Exception e) { + return ResponseEntity.internalServerError().build(); + } + } +} + + + + diff --git a/src/main/java/com/olympus/apollo/services/VideoGroupService.java b/src/main/java/com/olympus/apollo/services/VideoGroupService.java new file mode 100644 index 0000000..3965e3b --- /dev/null +++ b/src/main/java/com/olympus/apollo/services/VideoGroupService.java @@ -0,0 +1,44 @@ +package com.olympus.apollo.services; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; + +import com.olympus.apollo.repository.VideoGroupRepository; +import com.olympus.model.apollo.VideoGroup; + +@Service +public class VideoGroupService { + + private Logger logger = LoggerFactory.getLogger(VideoGroupService.class); + + @Autowired + private VideoGroupRepository videoGroupRepository; + + public VideoGroup getVideoGroupById(String id) { + return videoGroupRepository.findById(id).get(); + } + + public List findByProjectId(String projectId) { + return videoGroupRepository.findByProjectId(projectId, Sort.by(Sort.Direction.DESC, "id")); + } + + public boolean deleteVideoGroup(String id) { + try { + VideoGroup videoGroup = videoGroupRepository.findById(id).orElse(null); + if (videoGroup == null) { + return false; + } + + videoGroupRepository.delete(videoGroup); + return true; + } catch (Exception e) { + logger.error("Error in deleteVideoGroup function: " + e.getMessage()); + return false; + } + } +} \ No newline at end of file