-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Формы. Загрузка файла на сервер. [#504855]
- Loading branch information
1 parent
57b6809
commit f31efb6
Showing
15 changed files
with
376 additions
and
36 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/ru/mch/dreamjob/controller/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ru.mch.dreamjob.controller; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import ru.mch.dreamjob.service.FileService; | ||
|
||
@RestController | ||
@RequestMapping("/files") | ||
public class FileController { | ||
|
||
private final FileService fileService; | ||
|
||
public FileController(FileService fileService) { | ||
this.fileService = fileService; | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<?> getById(@PathVariable int id) { | ||
var contentOptional = fileService.getFileById(id); | ||
if (contentOptional.isEmpty()) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
return ResponseEntity.ok(contentOptional.get().getContent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ru.mch.dreamjob.dto; | ||
|
||
public class FileDto { | ||
private String name; | ||
|
||
private byte[] content; | ||
|
||
public FileDto(String name, byte[] content) { | ||
this.name = name; | ||
this.content = content; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public byte[] getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(byte[] content) { | ||
this.content = content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package ru.mch.dreamjob.entity; | ||
|
||
import java.util.Objects; | ||
|
||
public class File { | ||
|
||
private int id; | ||
|
||
private String name; | ||
|
||
private String path; | ||
|
||
public File(String name, String path) { | ||
this.name = name; | ||
this.path = path; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public void setPath(String path) { | ||
this.path = path; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
File file = (File) o; | ||
return id == file.id && Objects.equals(path, file.path); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/ru/mch/dreamjob/repository/FileRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ru.mch.dreamjob.repository; | ||
|
||
import ru.mch.dreamjob.entity.File; | ||
|
||
import java.util.Optional; | ||
|
||
public interface FileRepository { | ||
|
||
File save(File file); | ||
|
||
Optional<File> findById(int id); | ||
|
||
boolean deleteById(int id); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/ru/mch/dreamjob/repository/MemoryFileRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ru.mch.dreamjob.repository; | ||
|
||
import org.springframework.stereotype.Repository; | ||
import ru.mch.dreamjob.entity.File; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@Repository | ||
public class MemoryFileRepository implements FileRepository { | ||
|
||
private final AtomicInteger nextId = new AtomicInteger(0); | ||
|
||
private final Map<Integer, File> files = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public File save(File file) { | ||
file.setId(nextId.incrementAndGet()); | ||
files.put(file.getId(), file); | ||
return file; | ||
} | ||
|
||
@Override | ||
public Optional<File> findById(int id) { | ||
return Optional.ofNullable(files.get(id)); | ||
} | ||
|
||
@Override | ||
public boolean deleteById(int id) { | ||
return files.remove(id) != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ru.mch.dreamjob.service; | ||
|
||
import ru.mch.dreamjob.dto.FileDto; | ||
import ru.mch.dreamjob.entity.File; | ||
|
||
import java.util.Optional; | ||
|
||
public interface FileService { | ||
|
||
File save(FileDto fileDto); | ||
|
||
Optional<FileDto> getFileById(int id); | ||
|
||
boolean deleteById(int id); | ||
} |
92 changes: 92 additions & 0 deletions
92
src/main/java/ru/mch/dreamjob/service/SimpleFileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package ru.mch.dreamjob.service; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import ru.mch.dreamjob.dto.FileDto; | ||
import ru.mch.dreamjob.entity.File; | ||
import ru.mch.dreamjob.repository.FileRepository; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Service | ||
public class SimpleFileService implements FileService { | ||
|
||
private final FileRepository fileRepository; | ||
|
||
private final String storageDirectory; | ||
|
||
public SimpleFileService(FileRepository fileRepository, | ||
@Value("${file.directory}") String storageDirectory) { | ||
this.fileRepository = fileRepository; | ||
this.storageDirectory = storageDirectory; | ||
createStorageDirectory(storageDirectory); | ||
} | ||
|
||
private void createStorageDirectory(String path) { | ||
try { | ||
Files.createDirectories(Path.of(path)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public File save(FileDto fileDto) { | ||
var path = getNewFilePath(fileDto.getName()); | ||
writeFileBytes(path, fileDto.getContent()); | ||
return fileRepository.save(new File(fileDto.getName(), path)); | ||
} | ||
|
||
private String getNewFilePath(String sourceName) { | ||
return storageDirectory + java.io.File.separator + UUID.randomUUID() + sourceName; | ||
} | ||
|
||
private void writeFileBytes(String path, byte[] content) { | ||
try { | ||
Files.write(Path.of(path), content); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<FileDto> getFileById(int id) { | ||
var fileOptional = fileRepository.findById(id); | ||
if (fileOptional.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
var content = readFileAsBytes(fileOptional.get().getPath()); | ||
return Optional.of(new FileDto(fileOptional.get().getName(), content)); | ||
} | ||
|
||
private byte[] readFileAsBytes(String path) { | ||
try { | ||
return Files.readAllBytes(Path.of(path)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean deleteById(int id) { | ||
var fileOptional = fileRepository.findById(id); | ||
if (fileOptional.isEmpty()) { | ||
return false; | ||
} | ||
deleteFile(fileOptional.get().getPath()); | ||
return fileRepository.deleteById(id); | ||
} | ||
|
||
private void deleteFile(String path) { | ||
try { | ||
Files.deleteIfExists(Path.of(path)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.