Update file transfer mode
This commit is contained in:
@@ -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,14 +50,38 @@ 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);
|
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 ksDocument = new KSDocument();
|
||||||
ksDocument.setFilePath(filePath);
|
ksDocument.setFilePath(filePath);
|
||||||
@@ -90,6 +116,10 @@ public class KSFileController {
|
|||||||
|
|
||||||
// return "OK";
|
// return "OK";
|
||||||
return ResponseEntity.ok(ksDocument);
|
return ResponseEntity.ok(ksDocument);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return ResponseEntity.status(500).body(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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,25 +28,46 @@ 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();
|
||||||
|
|
||||||
|
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 ksVideo = new KSVideo();
|
||||||
ksVideo.setFilePath(filePath);
|
ksVideo.setFilePath(filePath);
|
||||||
ksVideo.setFileName(file.getOriginalFilename());
|
ksVideo.setFileName(file.getOriginalFilename());
|
||||||
ksVideo.setName(file.getOriginalFilename());
|
ksVideo.setName(file.getOriginalFilename());
|
||||||
ksVideo.setDescription(videoUploadDTO.getDescription());
|
ksVideo.setDescription(videoUploadDTO.getDescription());
|
||||||
ksVideo.setIngestionStatus("INGESTION_QUEUE");
|
ksVideo.setIngestionStatus("INGESTIONo_QUEUE");
|
||||||
|
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
ksVideo.setIngestionDate(now);
|
ksVideo.setIngestionDate(now);
|
||||||
@@ -65,7 +93,11 @@ public class VideoController {
|
|||||||
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user