0

So I got this gradle script that let's me upload apks to Nexus. The issue is that those files end up without file extension on the server which is a problem if you want to download an app from there.

It seems that I'm passing the proper mime type but even with that I'm still getting a file without extension.

Here is the code:

def uploadToRepository(File file, 
    String folder, 
    String url, 
    String userName, 
    String password){

    HTTPBuilder http = new HTTPBuilder(url)
    String basicAuthString = "Basic " + "${userName}:${password}".bytes.encodeBase64().toString()

    http.client.addRequestInterceptor(new HttpRequestInterceptor() {
        void process(HttpRequest httpRequest, HttpContext httpContext) {
            httpRequest.addHeader('Authorization', basicAuthString)
        }
    })
    try {
        http.request(Method.POST, "application/vnd.android.package-archive") { req ->
            uri.path = "/content/repositories/releases/${folder}"

            MultipartEntity entity = new MultipartEntity()
            def fileBody = new FileBody(file, "application/vnd.android.package-archive")
            entity.addPart("file", fileBody)
            req.entity = entity

            response.success = { resp, reader ->
                if(resp.status == 201) {
                    println "File ${file.name} uploaded"
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace()
    }
}
Painy James
  • 805
  • 2
  • 13
  • 26
  • 1
    Have you tried `new FileBody(file, ContentType.create("application/vnd.android.package-archive"), file.name)` – tim_yates May 01 '16 at 13:38
  • it was all fine, the issue was that Nexus needs to have the file name as part of the url or else is not going to work.. Basically I just changed `uri.path = "/content/repositories/releases/${folder}"` to be `uri.path = "/content/repositories/releases/${folder}/nameofapp.apk"` – Painy James May 01 '16 at 20:52

0 Answers0