|
|
@ -1,24 +1,30 @@ |
|
|
|
package app.gameimage; |
|
|
|
|
|
|
|
import app.data.entity.Game; |
|
|
|
import com.vaadin.flow.component.html.Image; |
|
|
|
import com.vaadin.flow.server.StreamResource; |
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
public class GameImageUtils { |
|
|
|
public static final String IMAGE_PATH_CLIENT = "images/games/"; |
|
|
|
public static final String EXTENSION = ".png"; |
|
|
|
static final String EXTENSION = ".png"; |
|
|
|
|
|
|
|
static final String IMAGE_SOURCE_PATH_SERVER = "src/main/resources/META-INF/resources/images/chess-sources/"; |
|
|
|
static final String IMAGE_DEST_PATH_SERVER = "src/main/resources/META-INF/resources/images/games/"; |
|
|
|
static final String IMAGE_SOURCE_DIR = "src/main/resources/META-INF/resources/images/chess-sources/"; |
|
|
|
static final String IMAGE_DEST_DIR = "src/main/resources/META-INF/resources/images/games/"; |
|
|
|
private static final String EXTENSION_WITHOUT_DOT = "png"; |
|
|
|
|
|
|
|
private GameImageUtils() {} |
|
|
|
private GameImageUtils() { |
|
|
|
} |
|
|
|
|
|
|
|
public static BufferedImage readImage(String fileNameWithoutExtension) { |
|
|
|
BufferedImage img = null; |
|
|
|
try { |
|
|
|
String path = IMAGE_SOURCE_PATH_SERVER + fileNameWithoutExtension + EXTENSION; |
|
|
|
String path = IMAGE_SOURCE_DIR + fileNameWithoutExtension + EXTENSION; |
|
|
|
img = ImageIO.read(new File(path)); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
@ -28,11 +34,33 @@ public class GameImageUtils { |
|
|
|
|
|
|
|
public static void writeImage(BufferedImage img, String fileNameWithoutExtension) { |
|
|
|
try { |
|
|
|
String path = IMAGE_DEST_PATH_SERVER + fileNameWithoutExtension + EXTENSION; |
|
|
|
String path = IMAGE_DEST_DIR + fileNameWithoutExtension + EXTENSION; |
|
|
|
File outputFile = new File(path); |
|
|
|
ImageIO.write(img, EXTENSION_WITHOUT_DOT, outputFile); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static String getImagePath(Game game) { |
|
|
|
return IMAGE_DEST_DIR + getImageFilename(game); |
|
|
|
} |
|
|
|
|
|
|
|
static String getImageFilename(Game game) { |
|
|
|
return getGameId(game) + EXTENSION; |
|
|
|
} |
|
|
|
|
|
|
|
static String getGameId(Game game) { |
|
|
|
String[] urlParts = game.getGameInfo().getChessComId().split("/"); |
|
|
|
return urlParts[urlParts.length - 1]; |
|
|
|
} |
|
|
|
|
|
|
|
static byte[] getImageBytes(Game game) { |
|
|
|
try { |
|
|
|
return FileUtils.readFileToByteArray(new File(getImagePath(game))); |
|
|
|
} catch (IOException e) { |
|
|
|
return new byte[0]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |