How to deal with upload image by Lagom

499 views
Skip to first unread message

Bond Chan

unread,
Aug 31, 2016, 4:29:54 AM8/31/16
to Lagom Framework Users
Hi,


Anyone have an example of dealing with uploading Image with Lagom services ?



Regards,
Bond

James Roper

unread,
Aug 31, 2016, 9:52:36 PM8/31/16
to Bond Chan, Lagom Framework Users
Hi Bond,

Currently it's not supported with the service API, however you can implement it by dropping down to the Play API in your service implementation:

import play.mvc.*;
import play.libs.streams.Accumulator;
import akka.stream.javadsl.*;

...

@Inject
private ExecutionContext ec;

public PlayServiceCall myServiceCall() {
  return wrapCall -> EssentialAction.of(rh -> {
    // Now you need to define an Akka streams sink to handle the image stream -
    // what that looks like depends on where you want to store the image.
    // I'm going to use one that pass in an output stream, but this uses blocking IO,
    // you should prefer something that's non blocking
    Sink<ByteString, CompletionStage<IOResult>> sink = StreamConverters.fromOutputStream(() -> {
      // Create output stream to write image to here
      return ...;
    });

    return Accumulator.fromSink(sink).map(ioResult -> {
      // Now you can create a Play result, eg:
      return Results.ok("Uploaded!");
    }, ec);
  });
}

Regards,

James

--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/CALecwfqwjYx5_COQ0Q-78pJa9wWaZHvhu4AvHXQNpOA8Y3Lcdw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
James Roper
Software Engineer

Lightbend – Build reactive apps!
Twitter: @jroper

Prateek Singhal

unread,
Oct 4, 2017, 11:51:10 AM10/4/17
to Lagom Framework Users
Hi James,

I am also looking for lagom service API to upload image. Please let me know if  the same is supported by lagom or not.

Thanks,
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framewo...@googlegroups.com.
To post to this group, send email to lagom-f...@googlegroups.com.

Ignasi Marimon-Clos i Sunyol

unread,
Oct 4, 2017, 12:51:45 PM10/4/17
to Prateek Singhal, Lagom Framework Users
Hi Patreek, 

the thread you mentioned is Lagom code.
Lagom uses Play and Akka under the covers but it doesn't expose all the features Play or Akka have in every Lagom API.

You have to keep in mind that Lagom is not a web framework. Instead, Lagom is a microservices framework that's designed to work with different RPC protocols and the default protocol is HTTP+JSON.

Uploading a file is not the best use case for lagom, even though it's not impossible. (see the code in the thread you mentioned)


Cheers



To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/0b3ecf18-d84c-43fa-836e-651748a22cf5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Ignasi Marimon-Clos
Software Developer @ Lagom


Koleman Nix

unread,
Oct 19, 2017, 4:04:48 PM10/19/17
to Lagom Framework Users
I just stumbled upon this thread and, until seeing James' code sample above, I had no idea about the `PlayServiceCall` trait. This is something that I think should be included in the Lagom documentation somewhere, even if it's only a few words and a small sample.

Ignasi Marimon-Clos i Sunyol

unread,
Oct 28, 2017, 11:10:02 AM10/28/17
to Koleman Nix, Lagom Framework Users
Thanks Koleman. 

I've added an issue to work on it https://github.com/lagom/lagom/issues/1078

To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/d95aab6d-e71d-4d1d-b7b4-2d4e101addb5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Ignasi Marimon-Clos i Sunyol

unread,
Feb 27, 2018, 5:46:28 AM2/27/18
to Lagom Framework Users
HI,

there is now a lagom-recipe demonstrating how to handle File Upload side-band in a Lagom service.

This was a rather common request. The solution introduced in the recipe is slightly different to what James Roper suggested on this thread to demonstrate an alternative.

Cheers,
Reply all
Reply to author
Forward
0 new messages