Checking attachments in the request

96 views Asked by At

I am developing file upload web app in Scalatra. In my use case i want to check if the submitted request contains attachment or not.

Below is my code :

val (profilePicByteStream: Array[Byte], contentType:String, fileName) = fileParams.get("profilePic") match {
      case Some(file) => (file.get(), file.contentType.getOrElse("application/octet-stream"), file.name)
      case None => (Array(), "", null)
    }

It is working fine when there is an attachment in the submitted request. How to hanlde invalid request.

In case of non attachments, it is throwing error :

([Ljava.lang.Object;@16837c0b,) (of class scala.Tuple2)
scala.MatchError: ([Ljava.lang.Object;@16837c0b,) (of class scala.Tuple2)

In both cases with attachments and without attachments, it is going to Some(file) case.

Can anyone suggest how to handle invalid case?

1

There are 1 answers

0
Boguste Hameyie On BEST ANSWER

Have you considered doing a "map" instead of a match?

fileParams.get("profilePic").map(file => (file.get(), file.contentType.getOrElse("application/octet-stream"), file.name))

This will work in the cases when you do have a file uploaded, and will return None in the cases where nothing is present