Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 14 additions & 67 deletions src/main/java/cc/wordview/api/controller/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,35 @@
package cc.wordview.api.controller;

import cc.wordview.api.Application;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import cc.wordview.api.util.WordViewResourceResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static cc.wordview.api.controller.response.ExceptionHandler.response;
import static cc.wordview.api.controller.response.Response.ok;

@RestController
@CrossOrigin(origins = Application.CORS_ORIGIN)
@RequestMapping(path = Application.API_PATH + "/home")
public class HomeController {
@Autowired
private WordViewResourceResolver resourceResolver;

@GetMapping(produces = "application/json;charset=utf-8")
public ResponseEntity<?> getEditorsPick() {
public ResponseEntity<?> getHome() {
return response(() -> {
JsonArray editorsPickArray = new JsonArray();
InputStream homeFile = new FileInputStream(resourceResolver.getOthersPath() + "/home.json");

editorsPickArray.add(
new VideoEntry(
"6gluNoLVKiQ",
"Eleanor Rigby (Remastered 2015)",
"The Beatles",
"https://i.ytimg.com/vi_webp/6gluNoLVKiQ/maxresdefault.webp"
).toJsonObject()
);
editorsPickArray.add(
new VideoEntry(
"ZnUEeXpxBJ0",
"Aquarela",
"Toquinho",
"https://i.ytimg.com/vi_webp/ZnUEeXpxBJ0/maxresdefault.webp"
).toJsonObject()
);
editorsPickArray.add(
new VideoEntry(
"ZpT9VCUS54s",
"Suisei no parade",
"majiko",
"https://i.ytimg.com/vi_webp/ZpT9VCUS54s/maxresdefault.webp"
).toJsonObject()
);
editorsPickArray.add(
new VideoEntry(
"HCTunqv1Xt4",
"When I'm Sixty Four",
"The Beatles",
"https://i.ytimg.com/vi_webp/HCTunqv1Xt4/maxresdefault.webp"
).toJsonObject()
);
editorsPickArray.add(
new VideoEntry(
"9NPv4q57on8",
"Ano yume wo nazotte",
"YOASOBI",
"https://i.ytimg.com/vi_webp/9NPv4q57on8/maxresdefault.webp"
).toJsonObject()
);
String text = new String(homeFile.readAllBytes(), StandardCharsets.UTF_8)
.replace("\n", "");

JsonObject responseObject = new JsonObject();
responseObject.add("editors-pick", editorsPickArray);

return ok(responseObject.toString());
return ok(text.trim());
});
}
}

@Getter
@Setter
@AllArgsConstructor
class VideoEntry {
private String id;
private String title;
private String artist;
private String cover;

public JsonObject toJsonObject() {
String jsonString = new Gson().toJson(this);
return JsonParser.parseString(jsonString).getAsJsonObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class WordViewResourceResolver {
@Value("${wordview.translations_path}")
private String translationsPath;

@Value("${wordview.others_path}")
private String othersPath;

@PostConstruct
public void debugShowPaths() {
logger.info("""
Expand All @@ -58,8 +61,9 @@ public void debugShowPaths() {
Lyrics Path: {}
Phrases Path: {}
Translations Path: {}
Others Path: {}
""",
dictionariesPath, imagesPath, lyricsPath, phrasesPath, translationsPath);
dictionariesPath, imagesPath, lyricsPath, phrasesPath, translationsPath, othersPath);
}

public String getDictionariesPath() throws IOException {
Expand All @@ -82,6 +86,10 @@ public String getTranslationsPath() throws IOException {
return resolvePathOrClasspath(translationsPath);
}

public String getOthersPath() throws IOException {
return resolvePathOrClasspath(othersPath);
}

/**
* Checks if the path is at the classpath, and if so return the full URI
* if it's a simple path it returns the path itself
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ wordview.images_path=classpath:/images/
wordview.lyrics_path=classpath:/lyrics/
wordview.phrases_path=classpath:/phrases/
wordview.translations_path=classpath:/translations/
wordview.others_path=classpath:/others/

server.compression.enabled=true
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
server.compression.min-response-size=2048
39 changes: 39 additions & 0 deletions src/main/resources/others/home.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"categories": [
{
"id": "editors-pick",
"entries": [
{
"id": "6gluNoLVKiQ",
"title": "Eleanor Rigby (Remastered 2015)",
"artist": "The Beatles",
"cover": "https://i.ytimg.com/vi_webp/6gluNoLVKiQ/maxresdefault.webp"
},
{
"id": "ZnUEeXpxBJ0",
"title": "Aquarela",
"artist": "Toquinho",
"cover": "https://i.ytimg.com/vi_webp/ZnUEeXpxBJ0/maxresdefault.webp"
},
{
"id": "ZpT9VCUS54s",
"title": "Suisei no parade",
"artist": "majiko",
"cover": "https://i.ytimg.com/vi_webp/ZpT9VCUS54s/maxresdefault.webp"
},
{
"id": "HCTunqv1Xt4",
"title": "When I'm Sixty Four",
"artist": "The Beatles",
"cover": "https://i.ytimg.com/vi_webp/HCTunqv1Xt4/maxresdefault.webp"
},
{
"id": "9NPv4q57on8",
"title": "Ano yume wo nazotte",
"artist": "YOASOBI",
"cover": "https://i.ytimg.com/vi_webp/9NPv4q57on8/maxresdefault.webp"
}
]
}
]
}