mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-25 06:58:39 +00:00
Merge remote-tracking branch 'origin/jan-team-image' into ftes-file-storage
Conflicts: java/com.sap.sailing.server.gateway/src/com/sap/sailing/server/gateway/jaxrs/api/CompetitorsResource.java
This commit is contained in:
+2
-5
@@ -32,7 +32,6 @@ import com.sap.sse.filestorage.FileStorageService;
|
||||
import com.sap.sse.filestorage.InvalidPropertiesException;
|
||||
import com.sap.sse.filestorage.OperationFailedException;
|
||||
import com.sap.sse.filestorage.testsupport.AmazonS3TestSupport;
|
||||
import com.sun.jersey.core.header.FormDataContentDisposition;
|
||||
|
||||
public class TeamImageTest extends AbstractJaxRsApiTest {
|
||||
private static final String name = "Heiko KRÖGER";
|
||||
@@ -63,12 +62,10 @@ public class TeamImageTest extends AbstractJaxRsApiTest {
|
||||
CompetitorsResource r = spyResource(new CompetitorsResource());
|
||||
URL fileUrl = getClass().getResource("/" + teamImageFile);
|
||||
URI fileUri = new URI(fileUrl.toString());
|
||||
String fileExtension = teamImageFile.substring(teamImageFile.lastIndexOf("."));
|
||||
long length = new File(fileUri).length();
|
||||
InputStream stream = getClass().getResourceAsStream("/" + teamImageFile);
|
||||
|
||||
FormDataContentDisposition fileDetails = FormDataContentDisposition.name("file").size(length)
|
||||
.fileName(teamImageFile).build();
|
||||
String jsonString = r.setTeamImage(id, stream, fileDetails);
|
||||
String jsonString = r.setTeamImage(id, stream, fileExtension, length);
|
||||
|
||||
//now download and compare
|
||||
JSONObject json = (JSONObject) JSONValue.parseWithException(jsonString);
|
||||
|
||||
+12
-11
@@ -8,6 +8,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
@@ -30,8 +31,6 @@ import com.sap.sailing.server.gateway.serialization.impl.PersonJsonSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.TeamJsonSerializer;
|
||||
import com.sap.sse.filestorage.InvalidPropertiesException;
|
||||
import com.sap.sse.filestorage.OperationFailedException;
|
||||
import com.sun.jersey.core.header.FormDataContentDisposition;
|
||||
import com.sun.jersey.multipart.FormDataParam;
|
||||
|
||||
@Path("/v1/competitors")
|
||||
public class CompetitorsResource extends AbstractSailingServerResource {
|
||||
@@ -98,12 +97,10 @@ public class CompetitorsResource extends AbstractSailingServerResource {
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
||||
@Produces("application/json;charset=UTF-8")
|
||||
@Consumes({ "image/jpeg", "image/png" })
|
||||
@Path("{competitor-id}/image")
|
||||
public String setTeamImage(@PathParam("competitor-id") String competitorId,
|
||||
@FormDataParam("file") InputStream uploadedInputStream,
|
||||
@FormDataParam("file") FormDataContentDisposition fileDetails) {
|
||||
public String setTeamImage(@PathParam("competitor-id") String competitorId, InputStream uploadedInputStream,
|
||||
@HeaderParam("Content-Type") String fileType, @HeaderParam("Content-Length") long sizeInBytes) {
|
||||
|
||||
RacingEventService service = getService();
|
||||
CompetitorStore store = service.getCompetitorStore();
|
||||
@@ -114,17 +111,21 @@ public class CompetitorsResource extends AbstractSailingServerResource {
|
||||
.entity("Could not find competitor with id " + competitorId).type(MediaType.TEXT_PLAIN).build());
|
||||
}
|
||||
|
||||
String fileExtension = null;
|
||||
if (fileType.equals("image/jpeg")) {
|
||||
fileExtension += ".jpg";
|
||||
} else {
|
||||
fileExtension += ".png";
|
||||
}
|
||||
|
||||
URI imageUri;
|
||||
try {
|
||||
String fileName = fileDetails.getFileName();
|
||||
long sizeInBytes = fileDetails.getSize();
|
||||
|
||||
if (sizeInBytes > 1024 * 1024 * MAX_SIZE_IN_MB) {
|
||||
throw new WebApplicationException(Response.status(Status.BAD_REQUEST)
|
||||
.entity("Image is larger than " + MAX_SIZE_IN_MB + "MB").build());
|
||||
}
|
||||
|
||||
imageUri = getService().getActiveFileStorageService().storeFile(uploadedInputStream, fileName, sizeInBytes);
|
||||
imageUri = getService().getActiveFileStorageService().storeFile(uploadedInputStream, fileExtension, sizeInBytes);
|
||||
} catch (IOException | OperationFailedException | InvalidPropertiesException e) {
|
||||
logger.log(Level.WARNING, "Could not store competitor image", e);
|
||||
throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR)
|
||||
|
||||
@@ -22,10 +22,10 @@ import com.sap.sse.common.TypeBasedServiceFinder;
|
||||
*/
|
||||
public interface FileStorageService {
|
||||
/**
|
||||
* @param originalFileName
|
||||
* @param originalFileExtension
|
||||
* may be {@code null}
|
||||
*/
|
||||
URI storeFile(InputStream is, String originalFileName, long lengthInBytes) throws IOException,
|
||||
URI storeFile(InputStream is, String fileExtension, long lengthInBytes) throws IOException,
|
||||
OperationFailedException, InvalidPropertiesException;
|
||||
|
||||
/**
|
||||
|
||||
+5
-9
@@ -80,13 +80,9 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
|
||||
return new AmazonS3Client(creds);
|
||||
}
|
||||
|
||||
private static String getKey(String originalFileName) {
|
||||
private static String getKey(String fileExtension) {
|
||||
String key = UUID.randomUUID().toString();
|
||||
if (originalFileName != null) {
|
||||
String ending = originalFileName.substring(originalFileName.lastIndexOf("."));
|
||||
key += ending;
|
||||
// key += "/" + originalFileName;
|
||||
}
|
||||
key += fileExtension;
|
||||
return key;
|
||||
}
|
||||
|
||||
@@ -102,18 +98,18 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI storeFile(InputStream is, String originalFileName, long lengthInBytes)
|
||||
public URI storeFile(InputStream is, String fileExtension, long lengthInBytes)
|
||||
throws InvalidPropertiesException, OperationFailedException {
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentLength(lengthInBytes);
|
||||
String key = getKey(originalFileName);
|
||||
String key = getKey(fileExtension);
|
||||
PutObjectRequest request = new PutObjectRequest(bucketName.getValue(), key, is, metadata)
|
||||
.withCannedAcl(CannedAccessControlList.PublicRead);
|
||||
AmazonS3Client s3Client = createS3Client();
|
||||
try {
|
||||
s3Client.putObject(request);
|
||||
} catch (AmazonClientException e) {
|
||||
throw new OperationFailedException("Could not store file " + originalFileName, e);
|
||||
throw new OperationFailedException("Could not store file", e);
|
||||
}
|
||||
URI uri = getUri(key);
|
||||
logger.info("Stored file " + uri);
|
||||
|
||||
+4
-8
@@ -23,9 +23,9 @@ public class LocalFileStorageServiceImpl implements FileStorageService {
|
||||
private static final String retrievalProtocol = "http";
|
||||
|
||||
@Override
|
||||
public URI storeFile(InputStream is, String originalFileName, long lengthInBytes) throws IOException {
|
||||
public URI storeFile(InputStream is, String fileExtension, long lengthInBytes) throws IOException {
|
||||
OutputStream outputStream = null;
|
||||
String pathToFile = path + "/" + getKey(originalFileName);
|
||||
String pathToFile = path + "/" + getKey(fileExtension);
|
||||
|
||||
outputStream = new FileOutputStream(new File(pathToFile));
|
||||
|
||||
@@ -50,13 +50,9 @@ public class LocalFileStorageServiceImpl implements FileStorageService {
|
||||
return getUri(pathToFile);
|
||||
}
|
||||
|
||||
private static String getKey(String originalFileName) {
|
||||
private static String getKey(String fileEnding) {
|
||||
String key = UUID.randomUUID().toString();
|
||||
if (originalFileName != null) {
|
||||
String ending = originalFileName.substring(originalFileName.lastIndexOf("."));
|
||||
key += ending;
|
||||
// key += "/" + originalFileName;
|
||||
}
|
||||
key += fileEnding;
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user