diff --git a/src/main/java/com/olympus/apollo/controllers/KSFileController.java b/src/main/java/com/olympus/apollo/controllers/KSFileController.java index a4a4c43..39b78b3 100644 --- a/src/main/java/com/olympus/apollo/controllers/KSFileController.java +++ b/src/main/java/com/olympus/apollo/controllers/KSFileController.java @@ -1,11 +1,19 @@ package com.olympus.apollo.controllers; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -19,13 +27,12 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import com.olympus.apollo.exception.StorageException; import com.olympus.apollo.exception.StorageFileNotFoundException; import com.olympus.apollo.repository.KSDocumentRepository; -import com.olympus.apollo.repository.KSIngestionInfoRepository; import com.olympus.apollo.repository.KSTextsRepository; import com.olympus.apollo.services.DeletionService; import com.olympus.apollo.services.KSIngestor; -import com.olympus.apollo.services.StorageService; import com.olympus.dto.ExternalFileIngestionDTO; import com.olympus.dto.FileUploadDTO; import com.olympus.model.apollo.KSDocument; @@ -34,13 +41,8 @@ import com.olympus.model.apollo.KSTexts; @RestController public class KSFileController { - @Autowired - private StorageService storageService; @Autowired private KSDocumentRepository ksDocumentREpository; - @Autowired - private KSIngestionInfoRepository ksIngestionInfoRepository; - @Autowired private KSTextsRepository ksTextsRepository; @Autowired @@ -48,48 +50,76 @@ public class KSFileController { @Autowired private DeletionService deletionService; + private static final Logger logger = LoggerFactory.getLogger(KSFileController.class); + + private String document_path="/mnt/apollo_storage/documents"; + @PostMapping("/upload") public ResponseEntity handleFileUpload( @RequestParam("file") MultipartFile file, @ModelAttribute FileUploadDTO fileUploadDTO ) { - Boolean isVideo = false; - String filePath = storageService.store(file,fileUploadDTO.getKsProjectName(), isVideo); - KSDocument ksDocument = new KSDocument(); - ksDocument.setFilePath(filePath); - ksDocument.setFileName(file.getOriginalFilename()); - ksDocument.setName(file.getOriginalFilename()); - ksDocument.setDescription(fileUploadDTO.getDescription()); - ksDocument.setIngestionStatus("INGESTION_QUEUE"); - ksDocument.setIngestionDateFormat(new SimpleDateFormat("MM/dd/yy").format(new Date())); + try (InputStream inputStream = file.getInputStream()) { + Path folderPath = Paths.get(document_path).resolve(fileUploadDTO.getKsProjectName()).normalize().toAbsolutePath(); - Date now = new Date(); - ksDocument.setIngestionDate(now); + Files.createDirectories(folderPath); + logger.info("[STORE] Created folder: {}", folderPath); - KSIngestionInfo ksIngestionInfo = new KSIngestionInfo(); - ksIngestionInfo.setType(fileUploadDTO.getType()); // != null ? type : "MD_DOCUMENT" + String filePath = folderPath.resolve(file.getOriginalFilename()).toString(); + try (OutputStream outputStream = new FileOutputStream(filePath)) { + byte[] buffer = new byte[8 * 1024]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } - HashMap metadata = new HashMap<>(); - metadata.put("KsApplicationName", fileUploadDTO.getKsApplicationName()); - metadata.put("KsDoctype", fileUploadDTO.getKsDocType()); - metadata.put("KsDocSource", fileUploadDTO.getKsDocSource()); - metadata.put("KsFileSource", file.getOriginalFilename()); - metadata.put("KsProjectName", fileUploadDTO.getKsProjectName()); + if (file.isEmpty()) { + logger.error("[STORE] File is empty: {}", file.getOriginalFilename()); + throw new StorageException("Failed to store empty file."); + } - ksIngestionInfo.setMetadata(metadata); - ksIngestionInfo.setDefaultChunkSize(fileUploadDTO.getDefaultChunkSize()); - ksIngestionInfo.setMinChunkSize(fileUploadDTO.getMinChunkSize()); - ksIngestionInfo.setMaxNumberOfChunks(fileUploadDTO.getMaxNumberOfChunks()); - ksIngestionInfo.setMinChunkSizeToEmbed(fileUploadDTO.getMinChunkSizeToEmbed()); + logger.info("[STORE] Storing file in: {}", filePath); + } - //ksIngestionInfoRepository.save(ksIngestionInfo); - ksDocument.setIngestionInfo(ksIngestionInfo); - ksDocumentREpository.save(ksDocument); + KSDocument ksDocument = new KSDocument(); + ksDocument.setFilePath(filePath); + ksDocument.setFileName(file.getOriginalFilename()); + ksDocument.setName(file.getOriginalFilename()); + ksDocument.setDescription(fileUploadDTO.getDescription()); + ksDocument.setIngestionStatus("INGESTION_QUEUE"); + ksDocument.setIngestionDateFormat(new SimpleDateFormat("MM/dd/yy").format(new Date())); - // return "OK"; - return ResponseEntity.ok(ksDocument); + Date now = new Date(); + ksDocument.setIngestionDate(now); + + KSIngestionInfo ksIngestionInfo = new KSIngestionInfo(); + ksIngestionInfo.setType(fileUploadDTO.getType()); // != null ? type : "MD_DOCUMENT" + + HashMap metadata = new HashMap<>(); + metadata.put("KsApplicationName", fileUploadDTO.getKsApplicationName()); + metadata.put("KsDoctype", fileUploadDTO.getKsDocType()); + metadata.put("KsDocSource", fileUploadDTO.getKsDocSource()); + metadata.put("KsFileSource", file.getOriginalFilename()); + metadata.put("KsProjectName", fileUploadDTO.getKsProjectName()); + + ksIngestionInfo.setMetadata(metadata); + ksIngestionInfo.setDefaultChunkSize(fileUploadDTO.getDefaultChunkSize()); + ksIngestionInfo.setMinChunkSize(fileUploadDTO.getMinChunkSize()); + ksIngestionInfo.setMaxNumberOfChunks(fileUploadDTO.getMaxNumberOfChunks()); + ksIngestionInfo.setMinChunkSizeToEmbed(fileUploadDTO.getMinChunkSizeToEmbed()); + + //ksIngestionInfoRepository.save(ksIngestionInfo); + ksDocument.setIngestionInfo(ksIngestionInfo); + ksDocumentREpository.save(ksDocument); + + // return "OK"; + return ResponseEntity.ok(ksDocument); + } catch (Exception e) { + e.printStackTrace(); + return ResponseEntity.status(500).body(null); + } } diff --git a/src/main/java/com/olympus/apollo/controllers/VideoController.java b/src/main/java/com/olympus/apollo/controllers/VideoController.java index 1ee6af5..46fa82e 100644 --- a/src/main/java/com/olympus/apollo/controllers/VideoController.java +++ b/src/main/java/com/olympus/apollo/controllers/VideoController.java @@ -1,8 +1,16 @@ package com.olympus.apollo.controllers; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ModelAttribute; @@ -11,9 +19,8 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; - +import com.olympus.apollo.exception.StorageException; 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; @@ -21,51 +28,76 @@ import com.olympus.model.apollo.KSVideo; @RestController public class VideoController { - @Autowired - private StorageService storageService; @Autowired private KSVideoRepository ksVideoRepository; + private String document_path="/mnt/apollo_storage/videos"; + + private static final Logger logger = LoggerFactory.getLogger(VideoController.class); + @PostMapping("/upload_video") public ResponseEntity handleVideoUpload( @RequestParam("file") MultipartFile file, - @ModelAttribute VideoUploadDTO videoUploadDTO - ) { - Boolean isVideo = true; - String filePath = storageService.store(file,videoUploadDTO.getKsProjectName(), isVideo); + @ModelAttribute VideoUploadDTO videoUploadDTO) + { + try (InputStream inputStream = file.getInputStream()) { + Path folderPath = Paths.get(document_path).resolve(videoUploadDTO.getKsProjectName()).normalize().toAbsolutePath(); - KSVideo ksVideo = new KSVideo(); - ksVideo.setFilePath(filePath); - ksVideo.setFileName(file.getOriginalFilename()); - ksVideo.setName(file.getOriginalFilename()); - ksVideo.setDescription(videoUploadDTO.getDescription()); - ksVideo.setIngestionStatus("INGESTION_QUEUE"); + Files.createDirectories(folderPath); + logger.info("[STORE] Created folder: {}", folderPath); - Date now = new Date(); - ksVideo.setIngestionDate(now); - ksVideo.setIngestionDateFormat(new SimpleDateFormat("MM/dd/yy").format(now)); + String filePath = folderPath.resolve(file.getOriginalFilename()).toString(); + try (OutputStream outputStream = new FileOutputStream(filePath)) { + byte[] buffer = new byte[8 * 1024]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + + if (file.isEmpty()) { + logger.error("[STORE] File is empty: {}", file.getOriginalFilename()); + throw new StorageException("Failed to store empty file."); + } + + logger.info("[STORE] Storing file in: {}", filePath); + } + + KSVideo ksVideo = new KSVideo(); + ksVideo.setFilePath(filePath); + ksVideo.setFileName(file.getOriginalFilename()); + ksVideo.setName(file.getOriginalFilename()); + ksVideo.setDescription(videoUploadDTO.getDescription()); + ksVideo.setIngestionStatus("INGESTIONo_QUEUE"); + + 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" + 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()); + 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()); - ksVideoIngestionInfo.setLanguage(videoUploadDTO.getLanguage()); + ksVideoIngestionInfo.setMetadata(metadata); + ksVideoIngestionInfo.setNumberOfChunkToEmbed(videoUploadDTO.getNumberOfChunkToEmbed()); + ksVideoIngestionInfo.setChunkDurationInSeconds(videoUploadDTO.getChunkDurationInSeconds()); + ksVideoIngestionInfo.setLanguage(videoUploadDTO.getLanguage()); - ksVideo.setIngestionInfo(ksVideoIngestionInfo); - ksVideoRepository.save(ksVideo); - return ResponseEntity.ok(ksVideo); + ksVideo.setIngestionInfo(ksVideoIngestionInfo); + ksVideoRepository.save(ksVideo); + return ResponseEntity.ok(ksVideo); + + } catch (Exception e) { + e.printStackTrace(); + return ResponseEntity.status(500).body(null); + } } - - } +