This commit is contained in:
@@ -22,13 +22,10 @@ import ru.soune.nocopy.service.file.ZipService;
|
||||
import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
||||
import ru.soune.nocopy.util.FileUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@@ -58,17 +55,12 @@ public class FileController {
|
||||
private CloudStorageService cloudStorageService;
|
||||
|
||||
@GetMapping("/public/{fileId}")
|
||||
public ResponseEntity<Resource> getPublicFile(@PathVariable String fileId) {
|
||||
public ResponseEntity<InputStreamResource> getPublicFile(@PathVariable String fileId) {
|
||||
try {
|
||||
FileEntity fileEntity = fileRepository.findById(fileId)
|
||||
.orElseThrow(() -> new FileNotFoundException("Not found: " + fileId));
|
||||
|
||||
File file = cloudStorageService.readFileFromStorageByPath(fileEntity.getFilePath());
|
||||
|
||||
if (file == null || !file.exists()) {
|
||||
log.error("File not found in storage: {}", fileEntity.getFilePath());
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
InputStream inputStream = cloudStorageService.readFileFromStorage(fileEntity.getFilePath());
|
||||
|
||||
MediaType mediaType = getMediaType(fileEntity);
|
||||
|
||||
@@ -76,19 +68,17 @@ public class FileController {
|
||||
.filename(fileEntity.getOriginalFileName(), StandardCharsets.UTF_8)
|
||||
.build();
|
||||
|
||||
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(mediaType)
|
||||
.contentLength(file.length())
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString())
|
||||
.header(HttpHeaders.CACHE_CONTROL, "public, max-age=3600")
|
||||
.body(resource);
|
||||
.body(new InputStreamResource(inputStream));
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
log.warn("Not on disk: {}", fileId);
|
||||
log.warn("File not found in DB: {}", fileId);
|
||||
return ResponseEntity.notFound().build();
|
||||
} catch (IOException e) {
|
||||
log.error("Read file exception: {}", fileId, e);
|
||||
} catch (Exception e) {
|
||||
log.error("Error reading file: {}", fileId, e);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user