Update file transfer mode
This commit is contained in:
@@ -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,14 +50,38 @@ 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<KSDocument> handleFileUpload(
|
||||
@RequestParam("file") MultipartFile file,
|
||||
@ModelAttribute FileUploadDTO fileUploadDTO
|
||||
) {
|
||||
Boolean isVideo = false;
|
||||
String filePath = storageService.store(file,fileUploadDTO.getKsProjectName(), isVideo);
|
||||
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
Path folderPath = Paths.get(document_path).resolve(fileUploadDTO.getKsProjectName()).normalize().toAbsolutePath();
|
||||
|
||||
Files.createDirectories(folderPath);
|
||||
logger.info("[STORE] Created folder: {}", folderPath);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
KSDocument ksDocument = new KSDocument();
|
||||
ksDocument.setFilePath(filePath);
|
||||
@@ -90,6 +116,10 @@ public class KSFileController {
|
||||
|
||||
// return "OK";
|
||||
return ResponseEntity.ok(ksDocument);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.status(500).body(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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,25 +28,46 @@ 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<KSVideo> 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();
|
||||
|
||||
Files.createDirectories(folderPath);
|
||||
logger.info("[STORE] Created folder: {}", folderPath);
|
||||
|
||||
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("INGESTION_QUEUE");
|
||||
ksVideo.setIngestionStatus("INGESTIONo_QUEUE");
|
||||
|
||||
Date now = new Date();
|
||||
ksVideo.setIngestionDate(now);
|
||||
@@ -65,7 +93,11 @@ public class VideoController {
|
||||
ksVideo.setIngestionInfo(ksVideoIngestionInfo);
|
||||
ksVideoRepository.save(ksVideo);
|
||||
return ResponseEntity.ok(ksVideo);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.status(500).body(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user