diff --git a/cli/pom.xml b/cli/pom.xml index 77c07f5131..4a075c92db 100644 --- a/cli/pom.xml +++ b/cli/pom.xml @@ -273,9 +273,9 @@ java8 - com.box.l10n.mojito.cli.apiclient - com.box.l10n.mojito.cli.model - com.box.l10n.mojito.cli.apiclient + ${parent.groupId}.cli.apiclient + ${parent.groupId}.cli.model + ${parent.groupId}.cli.apiclient diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/apiclient/WsApiConfig.java b/cli/src/main/java/com/box/l10n/mojito/cli/apiclient/WsApiConfig.java index 752e6f7ec6..b76deb837a 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/apiclient/WsApiConfig.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/apiclient/WsApiConfig.java @@ -37,4 +37,9 @@ public RepositoryWsApi repositoryWsApi() { public CommitWsApi commitWsApi() { return new CommitWsApi(this.apiClient); } + + @Bean + public ImageWsApi imageWsApi() { + return new ImageWsApi(this.apiClient); + } } diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/ScreenshotCommand.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/ScreenshotCommand.java index a573e7847b..52130f0da4 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/ScreenshotCommand.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/ScreenshotCommand.java @@ -3,9 +3,10 @@ import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.box.l10n.mojito.LocaleMappingHelper; +import com.box.l10n.mojito.cli.apiclient.ApiException; +import com.box.l10n.mojito.cli.apiclient.ImageWsApi; import com.box.l10n.mojito.cli.command.param.Param; import com.box.l10n.mojito.cli.console.ConsoleWriter; -import com.box.l10n.mojito.rest.client.ImageClient; import com.box.l10n.mojito.rest.client.ScreenshotClient; import com.box.l10n.mojito.rest.entity.Locale; import com.box.l10n.mojito.rest.entity.Repository; @@ -89,7 +90,7 @@ public class ScreenshotCommand extends Command { CommandDirectories commandDirectories; - @Autowired ImageClient imageClient; + @Autowired ImageWsApi imageClient; @Autowired ScreenshotClient screenshotClient; @@ -205,8 +206,8 @@ void uploadImage(Path image, String uploadPath) throws CommandException { logger.debug("Upload image: {} to path: {}", image.toString(), uploadPath); try { byte[] content = Files.readAllBytes(image); - imageClient.uploadImage(uploadPath, content); - } catch (IOException ex) { + imageClient.uploadImage(content, uploadPath); + } catch (IOException | ApiException ex) { throw new CommandException("Failed to upload image: " + image.toString(), ex); } } diff --git a/webapp/src/main/java/com/box/l10n/mojito/rest/images/ImageWS.java b/webapp/src/main/java/com/box/l10n/mojito/rest/images/ImageWS.java index 4358019135..43feb1b43e 100644 --- a/webapp/src/main/java/com/box/l10n/mojito/rest/images/ImageWS.java +++ b/webapp/src/main/java/com/box/l10n/mojito/rest/images/ImageWS.java @@ -5,21 +5,19 @@ import com.box.l10n.mojito.entity.Image; import com.box.l10n.mojito.service.image.ImageService; import io.swagger.v3.oas.annotations.Operation; -import jakarta.servlet.http.HttpServletRequest; import java.io.IOException; -import java.net.URLDecoder; import java.util.Optional; import org.apache.commons.io.FilenameUtils; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.servlet.HandlerMapping; /** * Simple WS to uploadImage and serve images. @@ -32,13 +30,11 @@ public class ImageWS { /** logger */ static Logger logger = getLogger(ImageWS.class); - static String PATH_PREFIX = "/api/images/"; - @Autowired ImageService imageService; - @Operation(summary = "Get an image by its name (receive the image name as a path variable)") + @Operation(summary = "Get an image by its name") @RequestMapping( - value = "/api/images/**", + value = "/api/images/{*imageName}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE, @@ -47,10 +43,7 @@ public class ImageWS { MediaType.IMAGE_GIF_VALUE }) @ResponseBody - public ResponseEntity getImage(HttpServletRequest httpServletRequest) throws IOException { - - String imageName = getImageNameFromRequest(httpServletRequest); - + public ResponseEntity getImage(@PathVariable String imageName) throws IOException { Optional image = imageService.getImage(imageName); return image @@ -62,9 +55,9 @@ public ResponseEntity getImage(HttpServletRequest httpServletRequest) throws IOE .orElseGet(() -> ResponseEntity.notFound().build()); } - @Operation(summary = "Upload an image (receive the image name as a path variable)") + @Operation(summary = "Upload an image") @RequestMapping( - value = "/api/images/**", + value = "/api/images/{*imageName}", method = RequestMethod.PUT, consumes = { MediaType.APPLICATION_OCTET_STREAM_VALUE, @@ -73,28 +66,12 @@ public ResponseEntity getImage(HttpServletRequest httpServletRequest) throws IOE MediaType.IMAGE_GIF_VALUE }) @ResponseBody - public void uploadImage(@RequestBody byte[] imageContent, HttpServletRequest httpServletRequest) { - String imageName = getImageNameFromRequest(httpServletRequest); + public void uploadImage(@RequestBody byte[] imageContent, @PathVariable String imageName) + throws IOException { logger.debug("Uploading image: {}", imageName); imageService.uploadImage(imageName, imageContent); } - /** - * Get the image name/path from the request. - * - * @param httpServletRequest - * @return - */ - String getImageNameFromRequest(HttpServletRequest httpServletRequest) { - String path = - (String) - httpServletRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); - - String imageName = path.substring(PATH_PREFIX.length()); - imageName = URLDecoder.decode(imageName); - return imageName; - } - /** * Get the media type of an image based on its extension. Supported types are JPEG, PNG and GIF. * If there is no match based on the extension, MediaType.APPLICATION_OCTET_STREAM is returned.