Browse Source

files uploader 2

master
gsd 4 weeks ago
parent
commit
c7924a3f78
  1. 54
      src/main/java/app/controllers/FileController.java
  2. 16
      src/main/java/app/controllers/other/ExternalVIPController.java
  3. 15
      src/main/java/app/entities/SearchFilter.java
  4. 3
      src/main/java/app/entities/db/DbFile.java
  5. 13
      src/main/java/app/repositories/FilePSRepository.java
  6. 4
      src/main/java/app/repositories/FileRepository.java

54
src/main/java/app/controllers/FileController.java

@ -6,6 +6,9 @@ import app.entities.SearchFilter;
import app.entities.db.DbFile;
import app.repositories.FilePSRepository;
import app.repositories.FileRepository;
import app.services.ProfileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.Page;
@ -26,21 +29,52 @@ import java.time.Instant;
import java.util.UUID;
@RestController
@RequestMapping("api/files")
@RequestMapping(FileController.endpoint)
public class FileController {
public static final String endpoint = "api/file";
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private FileRepository fileRepository;
@Autowired
private FilePSRepository filePSRepository;
@Autowired
private ProfileService profileService;
@GetMapping("/edit/{uuid}")
@CheckWebAccess
@CheckPermitionFlag(flag = "z")
@CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity<DbFile> getDbFile(@PathVariable String uuid) {
DbFile dbFile = fileRepository.findById(UUID.fromString(uuid)).orElse(null);
if (dbFile == null) return ResponseEntity.notFound().build();
return ResponseEntity.ok(dbFile);
}
@PostMapping("/edit")
@CheckWebAccess
@CheckPermitionFlag(flag = "z")
@CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity editDbFile(@RequestBody DbFile dbFileEdited) {
DbFile dbFile = fileRepository.findById(dbFileEdited.getId()).orElse(null);
if (dbFile == null) return ResponseEntity.notFound().build();
dbFile.setTags(dbFileEdited.getTags());
fileRepository.save(dbFile);
return ResponseEntity.ok().build();
}
@PostMapping
@CheckWebAccess
@CheckPermitionFlag(flag = "z")
@CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity upload(@CookieValue(value = "steam64") String steam64,
@RequestParam("file") MultipartFile multipartFile) throws IOException {
@RequestParam("file") MultipartFile multipartFile,
@RequestParam(value = "tags", required = false) String tags) throws IOException {
if (multipartFile.isEmpty() || multipartFile.getSize() == 0L) return ResponseEntity.noContent().build();
UUID uuid = UUID.randomUUID();
@ -53,6 +87,7 @@ public class FileController {
dbFile.setData(multipartFile.getBytes());
dbFile.setTimestamp(timestamp);
dbFile.setId(uuid);
dbFile.setTags(tags);
fileRepository.save(dbFile);
return ResponseEntity.ok(uuid.toString());
}
@ -76,10 +111,23 @@ public class FileController {
@CheckPermitionFlag(flag = "z")
@CollectStatistic(stage = CollectStages.COMBINED)
public Page<DbFile> getFiles(Pageable pageable, @RequestBody(required = false) SearchFilter searchFilter) {
return filePSRepository.getFiles(pageable);
String steam64_ids = searchFilter.getAccountsSteam64(profileService);
return filePSRepository.getFiles(pageable,
steam64_ids.isEmpty(), steam64_ids,
searchFilter.getBeginUnixTime(),
searchFilter.getEndUnixTime());
}
private static String encodeFileName(String fileName) throws UnsupportedEncodingException {
return URLEncoder.encode(fileName, StandardCharsets.UTF_8).replace("+", "%20");
}
public String getUrl(String tags) {
logger.info("Search " + tags + " in files");
DbFile dbFile = fileRepository.getDbFilesByTag(tags);
if (dbFile == null)
throw new RuntimeException("Cannot find: " + tags);
return endpoint+"/"+dbFile.getId();
}
}

16
src/main/java/app/controllers/other/ExternalVIPController.java

@ -6,6 +6,7 @@ import app.annotations.interfaces.CheckPermitionFlag;
import app.annotations.interfaces.CheckWebAccess;
import app.annotations.interfaces.ClusterMethod;
import app.annotations.interfaces.CollectStatistic;
import app.controllers.FileController;
import app.entities.VipGiveMethod;
import app.entities.VipPrice;
import app.services.db.VIPService;
@ -31,20 +32,23 @@ public class ExternalVIPController {
List<VipPrice> prices;
private FileController fileController;
@Autowired
public ExternalVIPController(VIPService vipService) {
public ExternalVIPController(VIPService vipService, FileController fileController) {
this.vipService = vipService;
this.unique_set = new HashSet<>();
this.fileController = fileController;
setupPrices();
}
public void setupPrices() {
prices = new ArrayList<>();
prices.add(new VipPrice("1 Месяц", 200, "1 ключ", "site_content/images/vip/VIP_1_MOUNTH.jpg", "month", true, true, true, 10));
prices.add(new VipPrice("1 Неделя", 100, "40 рефов", "site_content/images/vip/VIP_7_DAYS.jpg", "week", true, true, true, 10));
prices.add(new VipPrice("1 День", 35, "10 рефов", "site_content/images/vip/VIP_1_DAY.jpg", "day", true, true, true, 10));
prices.add(new VipPrice("1 День", 0, "бесплатно", "site_content/images/vip/freevip.jpg", "free", true, true, true, 10));
prices.add(new VipPrice("Промокод", 0, "бесплатно", "site_content/images/vip/promocode.jpg", "promocode", true, true, true, 10));
prices.add(new VipPrice("1 Месяц", 200, "1 ключ", fileController.getUrl("vip,VIP_1_MOUNTH"), "month", true, true, true, 10));
prices.add(new VipPrice("1 Неделя", 100, "40 рефов", fileController.getUrl("vip,VIP_7_DAYS"), "week", true, true, true, 10));
prices.add(new VipPrice("1 День", 35, "10 рефов", fileController.getUrl("vip,VIP_1_DAY"), "day", true, true, true, 10));
prices.add(new VipPrice("1 День", 0, "бесплатно", fileController.getUrl("vip,freevip"), "free", true, true, true, 10));
prices.add(new VipPrice("Промокод", 0, "бесплатно", fileController.getUrl("vip,promocode"), "promocode", true, true, true, 10));
}
@PostMapping

15
src/main/java/app/entities/SearchFilter.java

@ -65,6 +65,21 @@ public class SearchFilter {
.toList());
}
/**
* Выдает список из ид 64 версии 76561198087598690
* @param profileService
* @return
*/
public String getAccountsSteam64(ProfileService profileService) {
if (accounts == null || accounts.isEmpty()) return "";
return String.join(",", accounts.stream()
.map(profileService::GetSteamIDFromAnyData)
.filter(Objects::nonNull)
.map(sId -> String.valueOf(sId.steam64))
.toList());
}
public Long getBeginUnixTime() {
return begin == null ? null : (Timestamp.valueOf(begin).getTime()/1000) + utc*60;
}

3
src/main/java/app/entities/db/DbFile.java

@ -36,4 +36,7 @@ public class DbFile {
@Column(name = "deleted")
private Boolean deleted;
@Column(name = "tags")
private String tags;
}

13
src/main/java/app/repositories/FilePSRepository.java

@ -5,11 +5,20 @@ 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 org.springframework.data.repository.query.Param;
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);
@Query("select f from DbFile f where f.deleted = false and " +
"(:steam64_non_exists = true or position(f.uploader in :steam64_ids) > 0) and " +
"(:begin_date is null or DATE_PART('EPOCH', f.timestamp) >= :begin_date) and " +
"(:end_date is null or :end_date >= DATE_PART('EPOCH', f.timestamp)) " +
"order by f.timestamp desc")
Page<DbFile> getFiles(Pageable pageable,
@Param(value = "steam64_non_exists") Boolean steam64_non_exists,
@Param(value = "steam64_ids") String steam64_ids,
@Param(value = "begin_date") Long begin_date,
@Param(value = "end_date") Long end_date);
}

4
src/main/java/app/repositories/FileRepository.java

@ -1,9 +1,13 @@
package app.repositories;
import app.entities.db.DbFile;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.util.UUID;
public interface FileRepository extends CrudRepository<DbFile, UUID> {
@Query("select f from DbFile f where position(:tags in f.tags) > 0 order by f.timestamp desc limit 1")
DbFile getDbFilesByTag(@Param(value = "tags") String tags);
}

Loading…
Cancel
Save