Skip to content

Commit

Permalink
I18N-1323 Update Mojito CLI to use OpenAPI spec for rest calls
Browse files Browse the repository at this point in the history
Fixed images WS
  • Loading branch information
DarKhaos committed Nov 27, 2024
1 parent 90285cd commit 20c063b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public RepositoryWsApi repositoryWsApi() {
public CommitWsApi commitWsApi() {
return new CommitWsApi(this.apiClient);
}

@Bean
public ImageWsApi imageWsApi() {
return new ImageWsApi(this.apiClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,7 +90,7 @@ public class ScreenshotCommand extends Command {

CommandDirectories commandDirectories;

@Autowired ImageClient imageClient;
@Autowired ImageWsApi imageClient;

@Autowired ScreenshotClient screenshotClient;

Expand Down Expand Up @@ -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);
}
}
Expand Down
39 changes: 8 additions & 31 deletions webapp/src/main/java/com/box/l10n/mojito/rest/images/ImageWS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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<byte[]> getImage(@PathVariable String imageName) throws IOException {
Optional<Image> image = imageService.getImage(imageName);

return image
Expand All @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit 20c063b

Please sign in to comment.