timols / java-gitlab-api

A wrapper for the Gitlab API written in Java
Apache License 2.0
387 stars 317 forks source link

images corrupt after upload #305

Open powlwoq opened 6 years ago

powlwoq commented 6 years ago

Hi, first of all: thanks for this great library! I use it to let users of our web-application create new issues directly in our repository. This works fantastic but if i try to upload an image and add it to the issue the file gets corrupted. If i add a text-file everything works fine. I've tested it with .png, .jpg and *.gif. The size of the uploaded file increases by about 50 % also. If i upload the same files manually via the gui everything works fine. Any ideas? Thanks in advance.

aram535 commented 6 years ago

Please post the code and a test case.

powlwoq commented 6 years ago

I use the lib in a grails application. I've created a grails-service with following method:

boolean createNewGitIssue(Contact contactInstance, SecUser secUserInstance) {
        boolean GIT_ACCESS = grailsApplication.config.eers.issuetracker ? true : false
        boolean ret = false
        if (GIT_ACCESS) {
            Integer assigneeId
            String labels = contactInstance.topic.name + ",User-Request"
            String mailtolink = "mailto:${secUserInstance.email}?subject=${URLEncoder.encode(contactInstance.subject, "UTF-8").replaceAll("\\+", "%20")}"
            String description = "Request from: <a href=${mailtolink}>${secUserInstance.lastName}, ${secUserInstance.firstName}</a> (${secUserInstance.documentingCentre.name}} / User-Roles: " + springSecurityService.principal.authorities*.authority.join(", ") +
                    "<br>==================================================<br>Original message:<br>" +
                    contactInstance.message + "<br>==================================================<br>" +
                    contactInstance.browserInfo

            try {
                // initiate GitlabAPI
                GitlabAPI gitlabAPI = GitlabAPI.connect(gitlabApiBaseUrl, assigneePrivateToken);

                // get EERS project
                GitlabProject gitlabProject = gitlabAPI.getProject("ESID", "EERS")

                // add markdown for attachment to description if exists
                if (!contactInstance.attachment.empty) {
                    File tempFile = new File(File.TempDirectory.location().getAbsolutePath() + File.separator + contactInstance.attachment.fileItem.name)
                    contactInstance.attachment.fileItem.write(tempFile)

                    GitlabUpload gitlabUpload = gitlabAPI.uploadFile(gitlabProject, tempFile)
                    description += "<br>" + gitlabUpload.markdown
                }

                log.debug("description: " + description)

                // get assigneeId by username
                assigneeId = getGitlabProjectMemberIdByUsername(gitlabAPI, gitlabProject, assigneeUserName)
                if (assigneeId) {
                    // create new issue
                    gitlabAPI.createIssue(gitlabProject.id,
                            assigneeId,
                            null,
                            labels,
                            description,
                            contactInstance.subject)

                    log.debug("created new issue in git")
                    ret = true
                } else {
                    log.error("assigneeId for \"${assigneeUserName}\" (gitlabUserName) not found")
                }
            } catch (ConnectException e) {
                log.error("ConnectException thrown")
                e.printStackTrace()
            } catch (Exception ex) {
                log.error("Exception thrown")
                ex.printStackTrace()
            }
        }else{
            log.warn("No git-access configured in config")
        }
        return ret
    }

The file is send to the gitlab server and the markdown is added to the issue but the file is corrupt. Any ideas? Thank you.

aram535 commented 6 years ago

Hi, when you're having issues like this you need to narrow it down in a unit test with as few lines as possible so you can test the function only and not your logic. The included junit test that tests upload does work.

vladimirov commented 5 years ago

Any updates? I'm also can't get how to upload images to the project

ghost commented 5 years ago

Hi, I had the very same problem...

Here is how i fixed the problem:

File: org/gitlab/api/http/GitlabHTTPRequestor.java Method: private void submitAttachments(HttpURLConnection connection)

Changed:

writer.append(CRLF).flush();
try (Reader fileReader = new FileReader(binaryFile)) {
    IOUtils.copy(fileReader, output);
}
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

To:

writer.append(CRLF).flush();
FileUtils.copyFile(binaryFile, output);
output.flush(); // Important before continuing with writer!
writer.append(CRLF); // additional crlf
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

Also need to import: org.apache.commons.io.FileUtils But maybe it was just a missing CRLF after the file content...

powlwoq commented 5 years ago

Thank you for your comment! I've done it like you have described it. Just adding an additional CRLF was not sufficient. But with FileUtils now it works. Maybe you should add a pull request?!