Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use @Slf4j annotation #294

Merged
merged 1 commit into from
Jan 30, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* Component to apply command against an image prior to display. Useful for things like cropping or
* adjusting image in some way via imageMagick or similar
*/
@Slf4j
@Component
public class ImageTransformComponent {
private static final Logger logger = LoggerFactory.getLogger(ImageTransformComponent.class);

@Value("${transform-preview-commmand}")
private String previewCommand;

Expand Down Expand Up @@ -49,14 +47,14 @@ private File transformItem(File file, String command) {
while ((line = reader.readLine()) != null) {
out.append(line).append("\n");
}
logger.info("command output: " + out);
log.info("command output: " + out);
}
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new IOException("error exit status from command: " + exitCode);
}
} catch (IOException | InterruptedException e) {
logger.warn("error running command", e);
log.warn("error running command", e);
}
return tmpFile.exists() ? tmpFile : file;
}
Expand Down
56 changes: 27 additions & 29 deletions src/main/java/com/bigboxer23/meural_control/MeuralComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/** */
@Slf4j
@Component
public class MeuralComponent {
private static final Logger logger = LoggerFactory.getLogger(MeuralComponent.class);

private static final String apiUrl = "https://api.meural.com/v0/";

@Value("${meural-account}")
Expand Down Expand Up @@ -57,7 +55,7 @@ public MeuralComponent(GooglePhotosComponent gPhotos, ImageTransformComponent tr

protected String getToken() {
if (token == null) {
logger.info("fetching service meural token");
log.info("fetching service meural token");
try (Response response = OkHttpUtil.postSynchronous(
apiUrl + "authenticate",
new FormBody.Builder()
Expand All @@ -67,7 +65,7 @@ protected String getToken() {
null)) {
token = OkHttpUtil.getNonEmptyBody(response, Token.class).getToken();
} catch (IOException e) {
logger.error("getToken", e);
log.error("getToken", e);
}
}
return token;
Expand All @@ -81,12 +79,12 @@ protected Device getDevice() throws IOException {
if (meuralDevice != null) {
return meuralDevice;
}
logger.info("fetching device info from meural service");
log.info("fetching device info from meural service");
try (Response response =
OkHttpUtil.getSynchronous(apiUrl + "user/devices?count=10&page=1", getAuthCallback())) {
Devices devices = OkHttpUtil.getNonEmptyBody(response, Devices.class);
if (devices == null || devices.getData() == null || devices.getData().length == 0) {
logger.warn("cannot get device from body ");
log.warn("cannot get device from body ");
throw new IOException("cannot get device from body ");
}
meuralDevice = devices.getData()[0];
Expand All @@ -95,7 +93,7 @@ protected Device getDevice() throws IOException {
}

private MeuralStringResponse addItemToPlaylistAndDisplay(SourceItem sourceItem) throws IOException {
logger.info("starting add new file to playlist: \"" + sourceItem.getName() + "\"");
log.info("starting add new file to playlist: \"" + sourceItem.getName() + "\"");
MeuralItem item = uploadItemToMeural(sourceItem);
MeuralPlaylist playlist = getOrCreatePlaylist();
// Check if we already had this item in the playlist. If we did, no need to do anything
Expand All @@ -104,14 +102,14 @@ private MeuralStringResponse addItemToPlaylistAndDisplay(SourceItem sourceItem)
deleteItemsFromPlaylist(playlist);
addPlaylistToDevice(getDevice().getId(), playlist.getId());
}
logger.info("completed adding new file to playlist: \"" + sourceItem.getName() + "\"");
log.info("completed adding new file to playlist: \"" + sourceItem.getName() + "\"");
MeuralStringResponse response = new MeuralStringResponse();
response.setStatus("pass");
return response;
}

private void addPlaylistToDevice(String deviceId, String playlistId) throws IOException {
logger.info("Adding playlist to Meural " + deviceId + ":" + playlistId);
log.info("Adding playlist to Meural " + deviceId + ":" + playlistId);
RetryingCommand.execute(
() -> {
try (Response response = OkHttpUtil.postSynchronous(
Expand All @@ -127,14 +125,14 @@ private void addPlaylistToDevice(String deviceId, String playlistId) throws IOEx
}

private void deleteItemsFromPlaylist(MeuralPlaylist playlist) throws IOException {
logger.info("deleting items from playlist " + playlist.getId());
log.info("deleting items from playlist " + playlist.getId());
for (Integer item : playlist.getItemIds()) {
deleteItem(item);
}
}

private void deleteItem(Integer itemId) throws IOException {
logger.info("deleting item: " + itemId);
log.info("deleting item: " + itemId);
RetryingCommand.execute(
() -> {
try (Response response =
Expand All @@ -150,7 +148,7 @@ private void deleteItem(Integer itemId) throws IOException {
}

private void addItemToPlaylist(String playlistId, String itemId) throws IOException {
logger.info("adding item to playlist " + playlistId + ":" + itemId);
log.info("adding item to playlist " + playlistId + ":" + itemId);
RetryingCommand.execute(
() -> {
try (Response response = OkHttpUtil.postSynchronous(
Expand All @@ -174,7 +172,7 @@ private String getMeuralName(SourceItem sourceItem) {

private MeuralItem uploadItemToMeural(SourceItem sourceItem) throws IOException {
sourceItem.setTempFile(transformComponent.transformItem(sourceItem.getTempFile()));
logger.info("uploading file to Meural service \"" + sourceItem.getName() + "\"");
log.info("uploading file to Meural service \"" + sourceItem.getName() + "\"");
try (Response response = OkHttpUtil.postSynchronous(
apiUrl + "items",
new MultipartBody.Builder()
Expand All @@ -189,20 +187,20 @@ private MeuralItem uploadItemToMeural(SourceItem sourceItem) throws IOException
try {
MeuralItemResponse itemResponse = OkHttpUtil.getNonEmptyBody(response, MeuralItemResponse.class);
if (itemResponse == null || itemResponse.getData() == null) {
logger.warn("cannot get item from body ");
log.warn("cannot get item from body ");
reset();
throw new IOException("cannot get item from body ");
}
return itemResponse.getData();
} catch (JsonEncodingException e) {
logger.warn("uploadItemToMeural exception: ", e);
log.warn("uploadItemToMeural exception: ", e);
throw e;
}
}
}

protected MeuralPlaylist getOrCreatePlaylist() throws IOException {
logger.info("get playlist info for \"" + playlistName + "\"");
log.info("get playlist info for \"" + playlistName + "\"");
return RetryingCommand.execute(
() -> {
try (Response response =
Expand All @@ -211,7 +209,7 @@ protected MeuralPlaylist getOrCreatePlaylist() throws IOException {
if (meuralPlaylists == null
|| meuralPlaylists.getData() == null
|| meuralPlaylists.getData().length == 0) {
logger.warn("cannot get playlists from body ");
log.warn("cannot get playlists from body ");
throw new IOException("cannot get playlists from body ");
}
MeuralPlaylist playlist = Arrays.stream(meuralPlaylists.getData())
Expand All @@ -221,7 +219,7 @@ protected MeuralPlaylist getOrCreatePlaylist() throws IOException {
try {
return createPlaylist(playlistName);
} catch (IOException e) {
logger.warn("orElseGet", e);
log.warn("orElseGet", e);
return null;
}
});
Expand All @@ -236,7 +234,7 @@ protected MeuralPlaylist getOrCreatePlaylist() throws IOException {
}

protected MeuralPlaylist createPlaylist(String name) throws IOException {
logger.info("Creating playlist for " + name);
log.info("Creating playlist for " + name);
return RetryingCommand.execute(
() -> {
try (Response response = OkHttpUtil.postSynchronous(
Expand All @@ -249,7 +247,7 @@ protected MeuralPlaylist createPlaylist(String name) throws IOException {
MeuralPlaylistResponse playlistResponse =
OkHttpUtil.getNonEmptyBody(response, MeuralPlaylistResponse.class);
if (playlistResponse == null || playlistResponse.getData() == null) {
logger.warn("cannot create playlist from body ");
log.warn("cannot create playlist from body ");
throw new IOException("cannot create playlist from body ");
}
return playlistResponse.getData();
Expand All @@ -259,13 +257,13 @@ protected MeuralPlaylist createPlaylist(String name) throws IOException {
}

protected void deletePlaylist(String playlistId) throws IOException {
logger.info("deleting playlist for " + playlistId);
log.info("deleting playlist for " + playlistId);
RetryingCommand.execute(
() -> {
try (Response response =
OkHttpUtil.deleteSynchronous(apiUrl + "galleries/" + playlistId, getAuthCallback())) {
if (!response.isSuccessful()) {
logger.warn("cannot delete playlist "
log.warn("cannot delete playlist "
+ playlistId
+ ", body: "
+ response.body().string());
Expand Down Expand Up @@ -296,7 +294,7 @@ private MediaType getMediaType(File file) {

/** Cause a re-fetch of all meural device information on next request */
public void reset() {
logger.warn("resetting api");
log.warn("resetting api");
token = null;
meuralDevice = null;
}
Expand All @@ -310,7 +308,7 @@ private MeuralStringResponse executeAfterFetchCommand(SourceItem item, Command<M
if (item.getAlbumToSaveTo() != null && item.getAlbumToSaveTo().length() > 0) {
gPhotos.uploadItemToAlbum(item);
}
logger.info("removing temp file: \"" + item.getName() + "\"");
log.info("removing temp file: \"" + item.getName() + "\"");
item.getTempFile().delete();
}
}
Expand All @@ -319,10 +317,10 @@ private MeuralStringResponse executeAfterFetchCommand(SourceItem item, Command<M
public MeuralStringResponse fetchItem(SourceItem item, Command<MeuralStringResponse> command) throws IOException {
// If temp file is set and exists, don't fetch it again.
if (item.getTempFile() != null && item.getTempFile().exists()) {
logger.info("item exists, not re-downloading \"" + item.getName() + "\"");
log.info("item exists, not re-downloading \"" + item.getName() + "\"");
return executeAfterFetchCommand(item, command);
}
logger.info("downloading item for \"" + item.getName() + "\"");
log.info("downloading item for \"" + item.getName() + "\"");
String extension = FilenameUtils.getExtension(
item.getName() != null
? item.getName()
Expand Down Expand Up @@ -366,7 +364,7 @@ public MeuralStringResponse changePictureWithPreview(SourceItem item, boolean tr
tmpFile = transformComponent.transformPreviewItem(tmpFile);
}
File file = tmpFile;
logger.info("previewing directly on meural \"" + item.getName() + "\"");
log.info("previewing directly on meural \"" + item.getName() + "\"");
return RetryingCommand.execute(
() -> {
try (Response response = OkHttpUtil.postSynchronous(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -25,6 +24,7 @@
import org.springframework.web.bind.annotation.RestController;

/** */
@Slf4j
@RestController
@Tag(
name = "Meural Control Service",
Expand All @@ -38,8 +38,6 @@ public class MeuralController {

private final GooglePhotosComponent gPhotosAPI;

private static final Logger logger = LoggerFactory.getLogger(MeuralController.class);

public MeuralController(
MeuralComponent api,
SchedulerComponent scheduler,
Expand All @@ -59,7 +57,7 @@ private <T extends MeuralResponse> T handleResponse(HttpServletResponse servletR
}
return (T) response;
} catch (IOException theE) {
logger.warn("handleResponse", theE);
log.warn("handleResponse", theE);
api.reset();
servletResponse.setStatus(HttpStatus.BAD_REQUEST.value());
return (T) new MeuralResponse();
Expand All @@ -86,7 +84,7 @@ private <T extends MeuralResponse> T handleResponse(HttpServletResponse servletR
"https://res.cloudinary.com/dk-find-out/image/upload/q_80,w_1440,f_auto/DCTM_Penguin_UK_DK_AL316928_wsfijk.jpg")
})
public MeuralStringResponse displayContentFromUrl(String url, HttpServletResponse servletResponse) {
logger.warn("change display to: " + url);
log.warn("change display to: " + url);
return handleResponse(servletResponse, () -> api.changePicture(new SourceItem(null, new URL(url))));
}

Expand All @@ -113,7 +111,7 @@ public MeuralStringResponse displayContentFromUrl(String url, HttpServletRespons
"https://res.cloudinary.com/dk-find-out/image/upload/q_80,w_1440,f_auto/DCTM_Penguin_UK_DK_AL316928_wsfijk.jpg")
})
public MeuralStringResponse previewContentFromUrl(String url, HttpServletResponse servletResponse) {
logger.warn("previewing: " + url);
log.warn("previewing: " + url);
return handleResponse(servletResponse, () -> api.previewItem(new SourceItem(null, new URL(url)), true));
}

Expand Down
Loading