From ad2492f62f6c99b14801f45f2a7ca997fe515a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20H=C3=A4berling?= Date: Sun, 13 Aug 2023 11:11:11 -0700 Subject: [PATCH] Implement API version `0.2.9` by adding `FetchMediaImageRefs`. --- appengine/build.gradle | 2 +- .../retrostore/data/app/AppManagement.java | 2 +- .../rpc/api/FetchMediaImageRefsApiCall.java | 94 +++++++++++++++++++ .../rpc/api/FetchMediaImagesApiCall.java | 3 +- .../retrostore/rpc/internal/ApiRequest.java | 2 + 5 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 appengine/src/main/java/org/retrostore/rpc/api/FetchMediaImageRefsApiCall.java diff --git a/appengine/build.gradle b/appengine/build.gradle index 86c69f4..049172e 100644 --- a/appengine/build.gradle +++ b/appengine/build.gradle @@ -28,7 +28,7 @@ dependencies { // Specific Retrostore AppEngine dependencies.. implementation 'com.google.protobuf:protobuf-lite:3.0.0' - implementation 'org.retrostore:retrostore-client:0.2.7' + implementation 'org.retrostore:retrostore-client:0.2.9' implementation "com.google.guava:guava:20.0" implementation "com.googlecode.objectify:objectify:5.1.22" implementation "com.google.code.gson:gson:2.8.0" diff --git a/appengine/src/main/java/org/retrostore/data/app/AppManagement.java b/appengine/src/main/java/org/retrostore/data/app/AppManagement.java index 00b2ccc..73eb5bc 100644 --- a/appengine/src/main/java/org/retrostore/data/app/AppManagement.java +++ b/appengine/src/main/java/org/retrostore/data/app/AppManagement.java @@ -77,7 +77,7 @@ public interface AppManagement { Map getMediaImagesForApp(String appId); /** - * Deletes the media imafge with the given ID. + * Deletes the media image with the given ID. * * @param mediaId the id of the media image. */ diff --git a/appengine/src/main/java/org/retrostore/rpc/api/FetchMediaImageRefsApiCall.java b/appengine/src/main/java/org/retrostore/rpc/api/FetchMediaImageRefsApiCall.java new file mode 100644 index 0000000..2d9ecb1 --- /dev/null +++ b/appengine/src/main/java/org/retrostore/rpc/api/FetchMediaImageRefsApiCall.java @@ -0,0 +1,94 @@ +/* + * Copyright 2017, Sascha Häberling + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.retrostore.rpc.api; + +import com.google.protobuf.InvalidProtocolBufferException; +import org.retrostore.client.common.proto.ApiResponseMediaImageRefs; +import org.retrostore.client.common.proto.ApiResponseMediaImages; +import org.retrostore.client.common.proto.FetchMediaImageRefsParams; +import org.retrostore.client.common.proto.MediaImage; +import org.retrostore.client.common.proto.MediaImageRef; +import org.retrostore.data.app.AppManagement; +import org.retrostore.request.RequestData; +import org.retrostore.request.Response; +import org.retrostore.rpc.internal.ApiCall; + +import java.util.HashSet; +import java.util.logging.Logger; + +public class FetchMediaImageRefsApiCall implements ApiCall { + private static final Logger LOG = Logger.getLogger("FetchMediaImageRefs"); + + private final FetchMediaImagesApiCall mediaImageCall; + + public FetchMediaImageRefsApiCall(AppManagement appManagement) { + mediaImageCall = new FetchMediaImagesApiCall(appManagement); + } + + @Override + public String getName() { + return "fetchMediaImageRefs"; + } + + @Override + public Response call(RequestData data) { + FetchMediaImagesApiCall.Params params = parseParams(data.getRawBody()); + final ApiResponseMediaImageRefs response = callInternal(params); + return responder -> responder.respondProto(response); + } + + private FetchMediaImagesApiCall.Params parseParams(byte[] data) { + FetchMediaImageRefsParams params; + try { + params = FetchMediaImageRefsParams.parseFrom(data); + return new FetchMediaImagesApiCall.Params(params.getAppId(), new HashSet<>(params.getMediaTypeList())); + } catch (InvalidProtocolBufferException e) { + LOG.severe("Cannot parse parameters."); + } + return null; + } + + private ApiResponseMediaImageRefs callInternal(FetchMediaImagesApiCall.Params params) { + ApiResponseMediaImageRefs.Builder response = ApiResponseMediaImageRefs.newBuilder(); + + // Piggy back on top of the original media image fetch call, to avoid code duplication. + ApiResponseMediaImages mediaImages = mediaImageCall.callInternal(params); + + // Return error if fetching the media images failed. + if (!mediaImages.getSuccess()) { + return response.setSuccess(false) + .setMessage(mediaImages.getMessage()) + .build(); + } + + // Convert all media images to references. + for (MediaImage image : mediaImages.getMediaImageList()) { + // Skip empty/UNKNOWN entries. + if (image.getData().size() == 0) continue; + + String ref = params.appId + "/" + image.getFilename(); + response.addMediaImageRef(MediaImageRef.newBuilder() + .setType(image.getType()) + .setFilename(image.getFilename()) + .setUploadTime(image.getUploadTime()) + .setDescription(image.getDescription()) + .setDataRef(ref)); + } + + return response.setSuccess(true).setMessage("All good :-)").build(); + } +} diff --git a/appengine/src/main/java/org/retrostore/rpc/api/FetchMediaImagesApiCall.java b/appengine/src/main/java/org/retrostore/rpc/api/FetchMediaImagesApiCall.java index 4d3b757..b54e90a 100644 --- a/appengine/src/main/java/org/retrostore/rpc/api/FetchMediaImagesApiCall.java +++ b/appengine/src/main/java/org/retrostore/rpc/api/FetchMediaImagesApiCall.java @@ -88,7 +88,8 @@ private Params parseParams(byte[] data) { return null; } - private ApiResponseMediaImages callInternal(Params params) { + // Called from FetchMediaImageRefsApiCall, to avoid duplication. + ApiResponseMediaImages callInternal(Params params) { ApiResponseMediaImages.Builder response = ApiResponseMediaImages.newBuilder(); if (Strings.isNullOrEmpty(params.appId)) { return response.setSuccess(false).setMessage("No appId given.").build(); diff --git a/appengine/src/main/java/org/retrostore/rpc/internal/ApiRequest.java b/appengine/src/main/java/org/retrostore/rpc/internal/ApiRequest.java index 54c4bc9..ac1af19 100644 --- a/appengine/src/main/java/org/retrostore/rpc/internal/ApiRequest.java +++ b/appengine/src/main/java/org/retrostore/rpc/internal/ApiRequest.java @@ -28,6 +28,7 @@ import org.retrostore.resources.ImageServiceWrapper; import org.retrostore.rpc.api.DownloadStateApiCall; import org.retrostore.rpc.api.DownloadStateMemoryRegionApiCall; +import org.retrostore.rpc.api.FetchMediaImageRefsApiCall; import org.retrostore.rpc.api.FetchMediaImagesApiCall; import org.retrostore.rpc.api.GetAppApiCall; import org.retrostore.rpc.api.ListAppsApiCall; @@ -55,6 +56,7 @@ public ApiRequest(AppManagement appManagement, ImageServiceWrapper imageService, new ListAppsApiCall(appManagement, imageService), new ListAppsNanoApiCall(appManagement, imageService), new FetchMediaImagesApiCall(appManagement), + new FetchMediaImageRefsApiCall(appManagement), new UploadStateApiCall(stateManagement), new DownloadStateApiCall(stateManagement), new DownloadStateMemoryRegionApiCall(stateManagement));