Skip to content

Commit

Permalink
Implement API version 0.2.9 by adding FetchMediaImageRefs.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaeberling committed Aug 13, 2023
1 parent 21aa22b commit ad2492f
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
2 changes: 1 addition & 1 deletion appengine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public interface AppManagement {
Map<Long, MediaImage> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit ad2492f

Please sign in to comment.