mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-11-04 10:09:42 +00:00
calculate file size for team image upload in web service if not provided in Content-Length header field
This commit is contained in:
1 parent
8295e89113
commit
8eb67e4882
3 files changed
+13
-10
No files matched your search
+11
-4
@@ -1,5 +1,7 @@
|
||||
package com.sap.sailing.server.gateway.jaxrs.api;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
@@ -18,6 +20,7 @@ import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.apache.commons.fileupload.util.Streams;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.domain.base.Competitor;
|
||||
@@ -113,8 +116,7 @@ public class CompetitorsResource extends AbstractSailingServerResource {
|
||||
@Path("{competitor-id}/team/image")
|
||||
@Produces("application/json;charset=UTF-8")
|
||||
public String setTeamImage(@PathParam("competitor-id") String competitorId, InputStream uploadedInputStream,
|
||||
@HeaderParam("Content-Type") String fileType, @HeaderParam("Content-Length") long sizeInBytes) {
|
||||
|
||||
@HeaderParam("Content-Type") String fileType, @HeaderParam("Content-Length") long sizeInBytes) throws IOException {
|
||||
RacingEventService service = getService();
|
||||
CompetitorStore store = service.getCompetitorStore();
|
||||
Competitor competitor = store.getExistingCompetitorByIdAsString(competitorId);
|
||||
@@ -123,14 +125,19 @@ public class CompetitorsResource extends AbstractSailingServerResource {
|
||||
throw new WebApplicationException(Response.status(Status.BAD_REQUEST)
|
||||
.entity("Could not find competitor with id " + competitorId).type(MediaType.TEXT_PLAIN).build());
|
||||
}
|
||||
|
||||
String fileExtension = "";
|
||||
if (fileType.equals("image/jpeg")) {
|
||||
fileExtension = ".jpg";
|
||||
} else if (fileType.equals("image/png")) {
|
||||
fileExtension = ".png";
|
||||
}
|
||||
|
||||
if (sizeInBytes <= 0) {
|
||||
// size not provided; read the stream and determine length locally:
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
Streams.copy(uploadedInputStream, bos, /* close output stream */ true);
|
||||
sizeInBytes = bos.size();
|
||||
uploadedInputStream = new ByteArrayInputStream(bos.toByteArray());
|
||||
}
|
||||
URI imageUri;
|
||||
try {
|
||||
if (sizeInBytes > 1024 * 1024 * MAX_SIZE_IN_MB) {
|
||||
|
||||
+1
-5
@@ -208,7 +208,6 @@ public class RegattaActivity extends BaseActivity {
|
||||
*/
|
||||
public void updateLeaderboardPictureChosenByUser(final Bitmap bitmap) {
|
||||
storeImageAndSendToServer(bitmap, getLeaderboardImageFileName(leaderboard.name), true);
|
||||
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -235,7 +234,6 @@ public class RegattaActivity extends BaseActivity {
|
||||
FileOutputStream fos = new FileOutputStream(pictureFile);
|
||||
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
|
||||
fos.close();
|
||||
|
||||
if (sendToServer) {
|
||||
sendTeamImageToServer(pictureFile);
|
||||
}
|
||||
@@ -331,13 +329,11 @@ public class RegattaActivity extends BaseActivity {
|
||||
|
||||
protected String doInBackground(String... urls) {
|
||||
uploadUrl = urls[0];
|
||||
|
||||
try {
|
||||
if (imageFile != null) {
|
||||
HttpClient httpclient = new DefaultHttpClient();
|
||||
HttpPost httppost = new HttpPost(uploadUrl);
|
||||
|
||||
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(imageFile), -1);
|
||||
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(imageFile), imageFile.length());
|
||||
reqEntity.setContentType("image/jpeg");
|
||||
reqEntity.setChunked(true); // Send in multiple parts if needed
|
||||
httppost.setEntity(reqEntity);
|
||||
|
||||
+1
-1
@@ -156,7 +156,7 @@ public class DatabaseHelper {
|
||||
competitor.countryCode = cursor.getString(cursor.getColumnIndex(Competitor.COMPETITOR_COUNTRY_CODE));
|
||||
competitor.sailId = cursor.getString(cursor.getColumnIndex(Competitor.COMPETITOR_SAIL_ID));
|
||||
competitor.rowId = cursor.getInt(cursor.getColumnIndex(BaseColumns._ID));
|
||||
competitor.id = cursor.getString(cursor.getColumnIndex(Competitor.COMPETITOR_CHECKIN_DIGEST));
|
||||
competitor.id = cursor.getString(cursor.getColumnIndex(Competitor.COMPETITOR_ID));
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
|
||||
Reference in new issue
Block a user