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
1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 5 additions & 40 deletions src/main/java/cc/wordview/api/controller/ImageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,29 @@

import cc.wordview.api.Application;
import cc.wordview.api.exception.ImageNotFoundException;
import cc.wordview.api.util.WordViewResourceResolver;
import cc.wordview.api.runtime.ImageCache;
import jakarta.annotation.PostConstruct;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

@SuppressWarnings("RedundantThrows")
@RestController
@CrossOrigin(origins = Application.CORS_ORIGIN)
@RequestMapping(path = Application.API_PATH + "/image")
public class ImageController {
private static final Logger logger = LoggerFactory.getLogger(ImageController.class);

@Autowired
private WordViewResourceResolver resourceResolver;

private final Map<String, byte[]> images = new HashMap<>();
private ImageCache cache;

@GetMapping(produces = MediaType.IMAGE_PNG_VALUE)
public @ResponseBody byte[] getImage(@RequestParam String parent) throws ImageNotFoundException {
byte[] image = images.get(parent);

if (image == null) {
throw new ImageNotFoundException("Unable to find a image with this parent");
} else return image;
return cache.get(parent);
}

@PostConstruct
private void preloadImages() throws IOException {
String imagesPath = resourceResolver.getImagesPath();

try (Stream<Path> paths = Files.walk(Path.of(imagesPath))) {
paths.filter(Files::isRegularFile)
.forEach(file -> {
try (InputStream stream = new FileInputStream(file.toString())) {
String[] parts = file.toString().split("/");
String imageName = parts[parts.length - 1].replace(".png", "");

logger.info("Loading image \"{}.png\"", imageName);

images.put(imageName, IOUtils.toByteArray(stream));
} catch (Exception e) {
logger.error("Failed to load image", e);
}
});
} catch (IOException e) {
logger.error("Failed to walk through directory: {}", imagesPath, e);
}
cache.init();
}
}
31 changes: 31 additions & 0 deletions src/main/java/cc/wordview/api/runtime/ArrayCacheManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2025 Arthur Araujo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cc.wordview.api.runtime;

import java.io.IOException;
import java.util.ArrayList;

abstract public class ArrayCacheManager<T> {
protected final ArrayList<T> array = new ArrayList<>();

/**
* Populates the array with the values that should be cached.
* Ideally should be run in a @PostConstruct
*/
abstract public void init() throws IOException;
}
41 changes: 41 additions & 0 deletions src/main/java/cc/wordview/api/runtime/HashMapCacheManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2025 Arthur Araujo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cc.wordview.api.runtime;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

abstract public class HashMapCacheManager<T> {
protected final Map<String, T> map = new HashMap<>();

/**
* Populates the map with the values that should be cached.
* Ideally should be run in a @PostConstruct
*/
abstract public void init() throws IOException;

/**
* Retrieves the value from the map using the key
*
* @param key the key to the value
*/
public T get(String key) {
return map.get(key);
}
}
76 changes: 76 additions & 0 deletions src/main/java/cc/wordview/api/runtime/ImageCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2025 Arthur Araujo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cc.wordview.api.runtime;

import cc.wordview.api.exception.ImageNotFoundException;
import cc.wordview.api.util.WordViewResourceResolver;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

@Component
public class ImageCache extends HashMapCacheManager<byte[]> {
private static final Logger logger = LoggerFactory.getLogger(ImageCache.class);

@Autowired
private WordViewResourceResolver resourceResolver;

@Override
public void init() throws IOException {
String imagesPath = resourceResolver.getImagesPath();
Path filepath = Path.of(imagesPath);

try (Stream<Path> paths = Files.walk(filepath)) {
paths.filter(Files::isRegularFile)
.forEach(file -> {
try (InputStream stream = new FileInputStream(file.toString())) {
String[] parts = file.toString().split("/");
String imageName = parts[parts.length - 1].replace(".png", "");

map.put(imageName, IOUtils.toByteArray(stream));
} catch (Exception e) {
logger.error("Failed to load image", e);
}
});

logger.info("Preloaded {} images", map.size());
} catch (IOException e) {
logger.error("Failed to walk through directory: {}", imagesPath, e);
}
}

@SneakyThrows(ImageNotFoundException.class)
@Override
public byte[] get(String key) {
byte[] img = super.get(key);

if (img == null) {
throw new ImageNotFoundException("Unable to find a image with this parent");
} else return img;
}
}
84 changes: 84 additions & 0 deletions src/main/java/cc/wordview/api/runtime/LyricsCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2025 Arthur Araujo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cc.wordview.api.runtime;

import cc.wordview.api.database.entity.VideoLyrics;
import cc.wordview.api.service.specification.VideoLyricsServiceInterface;
import cc.wordview.api.util.WordViewResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LyricsCache extends HashMapCacheManager<String> {
private static final Logger logger = LoggerFactory.getLogger(LyricsCache.class);

@Autowired
private WordViewResourceResolver resourceResolver;

@Autowired
private VideoLyricsServiceInterface videoLyricsService;

@Override
public void init() throws IOException {
String lyricsPath = resourceResolver.getLyricsPath();
Path filepath = Path.of(lyricsPath);

Map<String, String> fileToContent = new HashMap<>();

try {
Files.walk(filepath)
.filter(Files::isRegularFile)
.forEach(file -> {
try {
fileToContent.put(file.getFileName().toString(), Files.readString(file));
} catch (IOException e) {
logger.error("Failed to read lyrics file, ignoring this file", e);
}
});


List<VideoLyrics> videoLyrics = videoLyricsService.getAll();

for (var videoLyric : videoLyrics) {
String content = fileToContent.get(videoLyric.getLyricsFile() + ".vtt");
map.put(videoLyric.getVideoId(), content);
}

logger.info("Preloaded {} lyrics", map.size());
} catch (IOException e) {
logger.error("Failed to read the lyrics path", e);
}
}

public void put(String id, String lyrics) {
if (map.containsKey(id))
return;

logger.info("Adding {} to cache, there are now {} lyrics", id, map.size() + 1);
map.put(id, lyrics);
}
}
Loading
Loading