5 changed files with 152 additions and 0 deletions
@ -0,0 +1,85 @@ |
|||
package app.controllers; |
|||
|
|||
import app.annotations.enums.CollectStages; |
|||
import app.annotations.interfaces.*; |
|||
import app.entities.SearchFilter; |
|||
import app.entities.db.DbFile; |
|||
import app.repositories.FilePSRepository; |
|||
import app.repositories.FileRepository; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.core.io.InputStreamResource; |
|||
import org.springframework.data.domain.Page; |
|||
import org.springframework.data.domain.Pageable; |
|||
import org.springframework.http.HttpHeaders; |
|||
import org.springframework.http.MediaType; |
|||
import org.springframework.http.ResponseEntity; |
|||
import org.springframework.web.bind.annotation.*; |
|||
import org.springframework.web.multipart.MultipartFile; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.IOException; |
|||
import java.io.UnsupportedEncodingException; |
|||
import java.net.URLEncoder; |
|||
import java.nio.charset.StandardCharsets; |
|||
import java.sql.Timestamp; |
|||
import java.time.Instant; |
|||
import java.util.UUID; |
|||
|
|||
@RestController |
|||
@RequestMapping("api/files") |
|||
public class FileController { |
|||
|
|||
@Autowired |
|||
private FileRepository fileRepository; |
|||
|
|||
@Autowired |
|||
private FilePSRepository filePSRepository; |
|||
|
|||
@PostMapping |
|||
@CheckWebAccess |
|||
@CheckPermitionFlag(flag = "z") |
|||
@CollectStatistic(stage = CollectStages.COMBINED) |
|||
public ResponseEntity upload(@CookieValue(value = "steam64") String steam64, |
|||
@RequestParam("file") MultipartFile multipartFile) throws IOException { |
|||
if (multipartFile.isEmpty() || multipartFile.getSize() == 0L) return ResponseEntity.noContent().build(); |
|||
|
|||
UUID uuid = UUID.randomUUID(); |
|||
Timestamp timestamp = Timestamp.from(Instant.now()); |
|||
|
|||
DbFile dbFile = new DbFile(); |
|||
dbFile.setUploader(steam64); |
|||
dbFile.setFilename(multipartFile.getOriginalFilename()); |
|||
dbFile.setFilesize(multipartFile.getSize()); |
|||
dbFile.setData(multipartFile.getBytes()); |
|||
dbFile.setTimestamp(timestamp); |
|||
dbFile.setId(uuid); |
|||
fileRepository.save(dbFile); |
|||
return ResponseEntity.ok(uuid.toString()); |
|||
} |
|||
|
|||
@GetMapping("/{uuid}") |
|||
public ResponseEntity<InputStreamResource> download(@PathVariable String uuid) throws UnsupportedEncodingException { |
|||
DbFile dbFile = fileRepository.findById(UUID.fromString(uuid)).orElse(null); |
|||
if (dbFile == null) return ResponseEntity.notFound().build(); |
|||
|
|||
HttpHeaders headers = new HttpHeaders(); |
|||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + encodeFileName(dbFile.getFilename())); |
|||
return ResponseEntity.ok() |
|||
.headers(headers) |
|||
.contentLength(dbFile.getFilesize()) |
|||
.contentType(MediaType.APPLICATION_OCTET_STREAM) |
|||
.body(new InputStreamResource(new ByteArrayInputStream(dbFile.getData()))); |
|||
} |
|||
|
|||
@PostMapping("/search") |
|||
@CheckWebAccess |
|||
@CheckPermitionFlag(flag = "z") |
|||
@CollectStatistic(stage = CollectStages.COMBINED) |
|||
public Page<DbFile> getFiles(Pageable pageable, @RequestBody(required = false) SearchFilter searchFilter) { |
|||
return filePSRepository.getFiles(pageable); |
|||
} |
|||
|
|||
private static String encodeFileName(String fileName) throws UnsupportedEncodingException { |
|||
return URLEncoder.encode(fileName, StandardCharsets.UTF_8).replace("+", "%20"); |
|||
} |
|||
} |
@ -0,0 +1,39 @@ |
|||
package app.entities.db; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|||
import jakarta.persistence.*; |
|||
import lombok.Data; |
|||
|
|||
import java.sql.Timestamp; |
|||
import java.util.UUID; |
|||
|
|||
@Entity |
|||
@Table(schema = "tf2_facti13", name = "files") |
|||
@Data |
|||
public class DbFile { |
|||
@Id |
|||
@Column(name = "id") |
|||
private UUID id; |
|||
|
|||
@Column(name = "uploader") |
|||
private String uploader; |
|||
|
|||
@Column(name = "filename", length = 255) |
|||
private String filename; |
|||
|
|||
@Column(name = "filesize") |
|||
private Long filesize; |
|||
|
|||
@Column(name = "extrainfo") |
|||
private String extraInfo; |
|||
|
|||
@Column(name = "data", columnDefinition = "bytea") |
|||
@JsonIgnore |
|||
private byte[] data; |
|||
|
|||
@Column(name = "ts") |
|||
private Timestamp timestamp; |
|||
|
|||
@Column(name = "deleted") |
|||
private Boolean deleted; |
|||
} |
@ -0,0 +1,15 @@ |
|||
package app.repositories; |
|||
|
|||
import app.entities.db.DbFile; |
|||
import org.springframework.data.domain.Page; |
|||
import org.springframework.data.domain.Pageable; |
|||
import org.springframework.data.jpa.repository.Query; |
|||
import org.springframework.data.repository.PagingAndSortingRepository; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
public interface FilePSRepository extends PagingAndSortingRepository<DbFile, UUID> { |
|||
|
|||
@Query("select f from DbFile f where f.deleted = false") |
|||
Page<DbFile> getFiles(Pageable pageable); |
|||
} |
@ -0,0 +1,9 @@ |
|||
package app.repositories; |
|||
|
|||
import app.entities.db.DbFile; |
|||
import org.springframework.data.repository.CrudRepository; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
public interface FileRepository extends CrudRepository<DbFile, UUID> { |
|||
} |
Loading…
Reference in new issue