Merge pull request #4 from FeiZhaixiage/master

[API] Add a feature to get user photos.
pull/6/head
Azalea 2023-12-08 05:12:09 +09:00 committed by GitHub
commit 722d415e75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -1,9 +1,12 @@
package icu.samnyan.aqua.api.controller.sega.game.maimai2;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import icu.samnyan.aqua.api.model.MessageResponse;
import icu.samnyan.aqua.api.model.ReducedPageResponse;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.ProfileResp;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.PhotoResp;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.external.ExternalUserData;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.external.Maimai2DataExport;
import icu.samnyan.aqua.api.model.resp.sega.maimai2.external.Maimai2DataImport;
@ -21,9 +24,12 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.nio.file.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author samnyan (privateamusement@protonmail.com)
@ -89,6 +95,38 @@ public class ApiMaimai2PlayerDataController {
return divMaxLength;
}
@GetMapping("userPhoto")
public PhotoResp getUserPhoto(@RequestParam long aimeId,
@RequestParam(required = false, defaultValue = "0") int imageIndex) {
List<String> matchedFiles = new ArrayList<>();
PhotoResp Photo = new PhotoResp();
try (Stream<Path> paths = Files.walk(Paths.get("data"))) {
matchedFiles = paths
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().endsWith(".jpg"))
.filter(path -> {
String fileName = path.getFileName().toString();
String[] parts = fileName.split("-");
return parts.length > 0 && parts[0].equals(String.valueOf(aimeId));
})
.map(Path::getFileName)
.map(Path::toString)
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
Photo.setTotalImage(matchedFiles.size());
Photo.setImageIndex(imageIndex);
if(matchedFiles.size() > imageIndex){
byte[] targetImageContent = Files.readAllBytes(Paths.get("data/" + matchedFiles.get(imageIndex)));
String divData = Base64.getEncoder().encodeToString(targetImageContent);
Photo.setDivData(divData);
Photo.setFileName(matchedFiles.get(imageIndex));
}
}
catch (Exception e) {
}
return Photo;
}
@GetMapping("profile")
public ProfileResp getProfile(@RequestParam long aimeId) {
return mapper.convert(userDataRepository.findByCard_ExtId(aimeId).orElseThrow(), new TypeReference<>() {

View File

@ -0,0 +1,15 @@
package icu.samnyan.aqua.api.model.resp.sega.maimai2;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PhotoResp {
private int imageIndex;
private int totalImage;
private String fileName;
private String divData;
}