Update file transfer mode

This commit is contained in:
2025-05-14 11:37:38 +02:00
parent 4e48a7ac23
commit 412cfbd039
2 changed files with 133 additions and 71 deletions

View File

@@ -1,11 +1,19 @@
package com.olympus.apollo.controllers; 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.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping; 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.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.olympus.apollo.exception.StorageException;
import com.olympus.apollo.exception.StorageFileNotFoundException; import com.olympus.apollo.exception.StorageFileNotFoundException;
import com.olympus.apollo.repository.KSDocumentRepository; import com.olympus.apollo.repository.KSDocumentRepository;
import com.olympus.apollo.repository.KSIngestionInfoRepository;
import com.olympus.apollo.repository.KSTextsRepository; import com.olympus.apollo.repository.KSTextsRepository;
import com.olympus.apollo.services.DeletionService; import com.olympus.apollo.services.DeletionService;
import com.olympus.apollo.services.KSIngestor; import com.olympus.apollo.services.KSIngestor;
import com.olympus.apollo.services.StorageService;
import com.olympus.dto.ExternalFileIngestionDTO; import com.olympus.dto.ExternalFileIngestionDTO;
import com.olympus.dto.FileUploadDTO; import com.olympus.dto.FileUploadDTO;
import com.olympus.model.apollo.KSDocument; import com.olympus.model.apollo.KSDocument;
@@ -34,13 +41,8 @@ import com.olympus.model.apollo.KSTexts;
@RestController @RestController
public class KSFileController { public class KSFileController {
@Autowired
private StorageService storageService;
@Autowired @Autowired
private KSDocumentRepository ksDocumentREpository; private KSDocumentRepository ksDocumentREpository;
@Autowired
private KSIngestionInfoRepository ksIngestionInfoRepository;
@Autowired @Autowired
private KSTextsRepository ksTextsRepository; private KSTextsRepository ksTextsRepository;
@Autowired @Autowired
@@ -48,48 +50,76 @@ public class KSFileController {
@Autowired @Autowired
private DeletionService deletionService; private DeletionService deletionService;
private static final Logger logger = LoggerFactory.getLogger(KSFileController.class);
private String document_path="/mnt/apollo_storage/documents";
@PostMapping("/upload") @PostMapping("/upload")
public ResponseEntity<KSDocument> handleFileUpload( public ResponseEntity<KSDocument> handleFileUpload(
@RequestParam("file") MultipartFile file, @RequestParam("file") MultipartFile file,
@ModelAttribute FileUploadDTO fileUploadDTO @ModelAttribute FileUploadDTO fileUploadDTO
) { ) {
Boolean isVideo = false;
String filePath = storageService.store(file,fileUploadDTO.getKsProjectName(), isVideo);
KSDocument ksDocument = new KSDocument(); try (InputStream inputStream = file.getInputStream()) {
ksDocument.setFilePath(filePath); Path folderPath = Paths.get(document_path).resolve(fileUploadDTO.getKsProjectName()).normalize().toAbsolutePath();
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()));
Date now = new Date(); Files.createDirectories(folderPath);
ksDocument.setIngestionDate(now); logger.info("[STORE] Created folder: {}", folderPath);
KSIngestionInfo ksIngestionInfo = new KSIngestionInfo(); String filePath = folderPath.resolve(file.getOriginalFilename()).toString();
ksIngestionInfo.setType(fileUploadDTO.getType()); // != null ? type : "MD_DOCUMENT" 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<String, String> metadata = new HashMap<>(); if (file.isEmpty()) {
metadata.put("KsApplicationName", fileUploadDTO.getKsApplicationName()); logger.error("[STORE] File is empty: {}", file.getOriginalFilename());
metadata.put("KsDoctype", fileUploadDTO.getKsDocType()); throw new StorageException("Failed to store empty file.");
metadata.put("KsDocSource", fileUploadDTO.getKsDocSource()); }
metadata.put("KsFileSource", file.getOriginalFilename());
metadata.put("KsProjectName", fileUploadDTO.getKsProjectName());
ksIngestionInfo.setMetadata(metadata); logger.info("[STORE] Storing file in: {}", filePath);
ksIngestionInfo.setDefaultChunkSize(fileUploadDTO.getDefaultChunkSize()); }
ksIngestionInfo.setMinChunkSize(fileUploadDTO.getMinChunkSize());
ksIngestionInfo.setMaxNumberOfChunks(fileUploadDTO.getMaxNumberOfChunks());
ksIngestionInfo.setMinChunkSizeToEmbed(fileUploadDTO.getMinChunkSizeToEmbed());
//ksIngestionInfoRepository.save(ksIngestionInfo); KSDocument ksDocument = new KSDocument();
ksDocument.setIngestionInfo(ksIngestionInfo); ksDocument.setFilePath(filePath);
ksDocumentREpository.save(ksDocument); 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"; Date now = new Date();
return ResponseEntity.ok(ksDocument); ksDocument.setIngestionDate(now);
KSIngestionInfo ksIngestionInfo = new KSIngestionInfo();
ksIngestionInfo.setType(fileUploadDTO.getType()); // != null ? type : "MD_DOCUMENT"
HashMap<String, String> 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);
}
} }

View File

@@ -1,8 +1,16 @@
package com.olympus.apollo.controllers; 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.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute; 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.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.olympus.apollo.exception.StorageException;
import com.olympus.apollo.repository.KSVideoRepository; import com.olympus.apollo.repository.KSVideoRepository;
import com.olympus.apollo.services.StorageService;
import com.olympus.dto.VideoUploadDTO; import com.olympus.dto.VideoUploadDTO;
import com.olympus.model.apollo.KSVideoIngestionInfo; import com.olympus.model.apollo.KSVideoIngestionInfo;
import com.olympus.model.apollo.KSVideo; import com.olympus.model.apollo.KSVideo;
@@ -21,51 +28,76 @@ import com.olympus.model.apollo.KSVideo;
@RestController @RestController
public class VideoController { public class VideoController {
@Autowired
private StorageService storageService;
@Autowired @Autowired
private KSVideoRepository ksVideoRepository; private KSVideoRepository ksVideoRepository;
private String document_path="/mnt/apollo_storage/videos";
private static final Logger logger = LoggerFactory.getLogger(VideoController.class);
@PostMapping("/upload_video") @PostMapping("/upload_video")
public ResponseEntity<KSVideo> handleVideoUpload( public ResponseEntity<KSVideo> handleVideoUpload(
@RequestParam("file") MultipartFile file, @RequestParam("file") MultipartFile file,
@ModelAttribute VideoUploadDTO videoUploadDTO @ModelAttribute VideoUploadDTO videoUploadDTO)
) { {
Boolean isVideo = true; try (InputStream inputStream = file.getInputStream()) {
String filePath = storageService.store(file,videoUploadDTO.getKsProjectName(), isVideo); Path folderPath = Paths.get(document_path).resolve(videoUploadDTO.getKsProjectName()).normalize().toAbsolutePath();
KSVideo ksVideo = new KSVideo(); Files.createDirectories(folderPath);
ksVideo.setFilePath(filePath); logger.info("[STORE] Created folder: {}", folderPath);
ksVideo.setFileName(file.getOriginalFilename());
ksVideo.setName(file.getOriginalFilename());
ksVideo.setDescription(videoUploadDTO.getDescription());
ksVideo.setIngestionStatus("INGESTION_QUEUE");
Date now = new Date(); String filePath = folderPath.resolve(file.getOriginalFilename()).toString();
ksVideo.setIngestionDate(now); try (OutputStream outputStream = new FileOutputStream(filePath)) {
ksVideo.setIngestionDateFormat(new SimpleDateFormat("MM/dd/yy").format(now)); 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 ksVideoIngestionInfo = new KSVideoIngestionInfo();
ksVideoIngestionInfo.setType(videoUploadDTO.getType()); // != null ? type : "MD_Video" ksVideoIngestionInfo.setType(videoUploadDTO.getType()); // != null ? type : "MD_Video"
HashMap<String, String> metadata = new HashMap<>(); HashMap<String, String> metadata = new HashMap<>();
metadata.put("KsApplicationName", videoUploadDTO.getKsApplicationName()); metadata.put("KsApplicationName", videoUploadDTO.getKsApplicationName());
metadata.put("KsDoctype", videoUploadDTO.getKsDocType()); metadata.put("KsDoctype", videoUploadDTO.getKsDocType());
metadata.put("KsDocSource", videoUploadDTO.getKsDocSource()); metadata.put("KsDocSource", videoUploadDTO.getKsDocSource());
metadata.put("KsFileSource", file.getOriginalFilename()); metadata.put("KsFileSource", file.getOriginalFilename());
metadata.put("KsVideoGroupId", videoUploadDTO.getKsVideoGroupId()); metadata.put("KsVideoGroupId", videoUploadDTO.getKsVideoGroupId());
metadata.put("KsProjectName", videoUploadDTO.getKsProjectName()); metadata.put("KsProjectName", videoUploadDTO.getKsProjectName());
ksVideoIngestionInfo.setMetadata(metadata); ksVideoIngestionInfo.setMetadata(metadata);
ksVideoIngestionInfo.setNumberOfChunkToEmbed(videoUploadDTO.getNumberOfChunkToEmbed()); ksVideoIngestionInfo.setNumberOfChunkToEmbed(videoUploadDTO.getNumberOfChunkToEmbed());
ksVideoIngestionInfo.setChunkDurationInSeconds(videoUploadDTO.getChunkDurationInSeconds()); ksVideoIngestionInfo.setChunkDurationInSeconds(videoUploadDTO.getChunkDurationInSeconds());
ksVideoIngestionInfo.setLanguage(videoUploadDTO.getLanguage()); ksVideoIngestionInfo.setLanguage(videoUploadDTO.getLanguage());
ksVideo.setIngestionInfo(ksVideoIngestionInfo); ksVideo.setIngestionInfo(ksVideoIngestionInfo);
ksVideoRepository.save(ksVideo); ksVideoRepository.save(ksVideo);
return ResponseEntity.ok(ksVideo); return ResponseEntity.ok(ksVideo);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(500).body(null);
}
} }
} }