java-gitlab-api icon indicating copy to clipboard operation
java-gitlab-api copied to clipboard

images corrupt after upload

Open powlwoq opened this issue 7 years ago • 6 comments

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.

powlwoq avatar Jun 26 '18 12:06 powlwoq

Please post the code and a test case.

aram535 avatar Jul 29 '18 15:07 aram535

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.

powlwoq avatar Aug 16 '18 08:08 powlwoq

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.

aram535 avatar Aug 16 '18 15:08 aram535

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

vladimirov avatar Nov 28 '18 15:11 vladimirov

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...

ghost avatar Mar 21 '19 13:03 ghost

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?!

powlwoq avatar Mar 22 '19 08:03 powlwoq