Skip to content

Commit

Permalink
upload uses multipart/form-data now
Browse files Browse the repository at this point in the history
  • Loading branch information
mguenther committed Oct 11, 2015
1 parent e630e85 commit 1000321
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.mgu.photoalbum.service;

import java.io.InputStream;
import java.util.List;

public interface PhotoCommandService {

String uploadPhoto(String ownerId, String albumId, String originalFilename, String base64EncodedImage);
//String uploadPhoto(String ownerId, String albumId, String originalFilename, String base64EncodedImage);
String uploadPhoto(String ownerId, String albumId, String originalFilename, InputStream fileInputStream);

void deletePhoto(String photoId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -61,7 +62,7 @@ public String uploadPhoto(
final String ownerId,
final String albumId,
final String originalFilename,
final String base64EncodedImage) {
final InputStream fileInputStream) {

final String photoId = generateUniquePhotoId();

Expand All @@ -70,7 +71,7 @@ public String uploadPhoto(
Path thumbnailPath = pathScheme.constructPathToThumbnail(ownerId, albumId, photoId);

pathAdapter.createDirectory(photoFolderPath);
pathAdapter.copy(inputStreamAdapter.getBase64DecodingInputStream(base64EncodedImage), originalPath);
pathAdapter.copy(fileInputStream, originalPath);
pathAdapter.copyImage(generateThumbnail(originalPath), thumbnailPath);

if (LOGGER.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.mgu.photoalbum;

import com.hubspot.dropwizard.guice.GuiceBundle;
import com.mgu.photoalbum.config.CrossOriginConfig;
import com.mgu.photoalbum.config.ServiceConfig;
import com.mgu.photoalbum.config.ServiceModule;
import com.mgu.photoalbum.security.Principal;
import io.dropwizard.Application;
import io.dropwizard.auth.AuthFactory;
import io.dropwizard.auth.basic.BasicAuthFactory;
import io.dropwizard.forms.MultiPartBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.eclipse.jetty.servlets.CrossOriginFilter;
Expand All @@ -31,6 +31,7 @@ public void initialize(final Bootstrap<ServiceConfig> bootstrap) {
.enableAutoConfig(getClass().getPackage().getName())
.build();
bootstrap.addBundle(guiceBundle);
bootstrap.addBundle(new MultiPartBundle());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
import com.mgu.photoalbum.service.PhotoCommandService;
import com.mgu.photoalbum.service.PhotoQueryService;
import io.dropwizard.auth.Auth;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -79,7 +82,7 @@ public Response listAlbum(
@PathParam("albumId") String albumId,
@QueryParam("offset") Optional<Integer> optionalOffset,
@QueryParam("pageSize") Optional<Integer> optionalPageSize,
@QueryParam("tags") Optional<String> optionalTags) {
@QueryParam("tags") Optional<String> optionalTags) {

final Album album = albumQueryService.albumById(albumId);

Expand Down Expand Up @@ -107,25 +110,29 @@ private Optional<List<String>> parseTags(Optional<String> optionalTags) {
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Consumes("multipart/form-data")
@Produces(MediaType.APPLICATION_JSON)
@Timed
public Response uploadPhoto(
public Response upload(
@Auth Principal principal,
@PathParam("albumId") String albumId,
UploadPhotoRepr uploadPhotoRepr) {
@FormDataParam("image") InputStream fileInputStream,
@FormDataParam("image") FormDataContentDisposition contentDisposition) {

final Album album = albumQueryService.albumById(albumId);
if (fileInputStream == null || contentDisposition == null) {
return Response.status(422).build(); // throw exception!
}

final Album album = albumQueryService.albumById(albumId);
if (!authorization.isAuthorized(principal, album)) {
throw new UserIsNotAuthorizedException(principal);
}

final String photoId = photoCommandService.uploadPhoto(
principal.getUserId(),
albumId,
uploadPhotoRepr.getOriginalFilename(),
uploadPhotoRepr.getBase64EncodedImage());
contentDisposition.getFileName(),
fileInputStream);

return Response.created(linkScheme.toPhoto(albumId, photoId)).build();
}
Expand Down

0 comments on commit 1000321

Please sign in to comment.