Search in sources :

Example 1 with UploadFileMetadata

use of com.epam.pipeline.controller.vo.UploadFileMetadata in project cloud-pipeline by epam.

the class GitManagerTest method shouldRenameFile.

@Test
public void shouldRenameFile() throws GitClientException, UnsupportedEncodingException {
    final Pipeline pipeline = testingPipeline();
    final UploadFileMetadata file = new UploadFileMetadata();
    final byte[] readmeContent = "Some inconsiderable content".getBytes("UTF-8");
    file.setFileName(README_FILE);
    file.setFileSize(readmeContent.length / 1024 + " Kb");
    file.setFileType("text/markdown; charset=UTF-8");
    file.setBytes(readmeContent);
    final GitCommitEntry expectedCommit = new GitCommitEntry();
    expectedCommit.setMessage("Rename the file");
    expectedCommit.setCreatedAt("2017-07-25T13:13:11Z");
    givenThat(post(urlPathEqualTo(api(REPOSITORY_COMMITS))).willReturn(okJson(with(expectedCommit))));
    final GitFile gitFile = new GitFile();
    gitFile.setContent(Base64.getEncoder().encodeToString(FILE_CONTENT.getBytes()));
    givenThat(get(urlPathEqualTo(api(REPOSITORY_FILES))).withQueryParam(FILE_PATH, equalTo(DOCS + "/" + README_FILE)).withQueryParam(REF, equalTo(GIT_MASTER_REPOSITORY)).willReturn(okJson(with(gitFile))));
    final GitCommitEntry resultingCommit = gitManager.uploadFiles(pipeline, DOCS, singletonList(file), pipeline.getCurrentVersion().getCommitId(), "Rename the file");
    assertThat(resultingCommit, is(expectedCommit));
}
Also used : UploadFileMetadata(com.epam.pipeline.controller.vo.UploadFileMetadata) GitFile(com.epam.pipeline.entity.git.GitFile) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) GitCommitEntry(com.epam.pipeline.entity.git.GitCommitEntry) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test)

Example 2 with UploadFileMetadata

use of com.epam.pipeline.controller.vo.UploadFileMetadata in project cloud-pipeline by epam.

the class PipelineController method uploadFile.

@RequestMapping(value = "/pipeline/{id}/file/upload", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "Uploads a file.", notes = "Uploads a file.", produces = MediaType.APPLICATION_JSON_VALUE)
public List<UploadFileMetadata> uploadFile(@PathVariable(value = ID) Long id, @RequestParam(value = PATH) final String folder, HttpServletRequest request) throws GitClientException, FileUploadException {
    MultipartFile file = consumeMultipartFile(request);
    List<UploadFileMetadata> uploadedFiles = new LinkedList<>();
    UploadFileMetadata fileMeta = new UploadFileMetadata();
    fileMeta.setFileName(FilenameUtils.getName(file.getOriginalFilename()).replaceAll("[ ]", "_"));
    fileMeta.setFileSize(file.getSize() / BYTES_IN_KB + " Kb");
    fileMeta.setFileType(file.getContentType());
    try {
        fileMeta.setBytes(file.getBytes());
        uploadedFiles.add(fileMeta);
    } catch (IOException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    pipelineApiService.uploadFiles(id, folder, uploadedFiles);
    uploadedFiles.forEach(f -> f.setBytes(null));
    return uploadedFiles;
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) UploadFileMetadata(com.epam.pipeline.controller.vo.UploadFileMetadata) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with UploadFileMetadata

use of com.epam.pipeline.controller.vo.UploadFileMetadata in project cloud-pipeline by epam.

the class GitManager method uploadFiles.

public GitCommitEntry uploadFiles(Pipeline pipeline, String folder, List<UploadFileMetadata> files, String lastCommitId, String commitMessage) throws GitClientException {
    Assert.isTrue(lastCommitId.equals(pipeline.getCurrentVersion().getCommitId()), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_WAS_UPDATED));
    GitlabClient gitlabClient = this.getGitlabClientForPipeline(pipeline);
    GitPushCommitEntry gitPushCommitEntry = new GitPushCommitEntry();
    for (UploadFileMetadata file : files) {
        String filePath = folder + PATH_DELIMITER + file.getFileName();
        Arrays.stream(filePath.split(PATH_DELIMITER)).forEach(pathPart -> Assert.isTrue(GitUtils.checkGitNaming(pathPart), messageHelper.getMessage(MessageConstants.ERROR_INVALID_PIPELINE_FILE_NAME, filePath)));
        boolean fileExists = false;
        try {
            fileExists = gitlabClient.getFileContents(filePath, GIT_MASTER_REPOSITORY) != null;
        } catch (HttpClientErrorException exception) {
            LOGGER.debug(exception.getMessage(), exception);
        }
        GitPushCommitActionEntry gitPushCommitActionEntry = new GitPushCommitActionEntry();
        commitMessage = getCommitMessage(commitMessage, filePath, fileExists, gitPushCommitActionEntry);
        gitPushCommitActionEntry.setFilePath(filePath);
        gitPushCommitActionEntry.setContent(Base64.getEncoder().encodeToString(file.getBytes()));
        gitPushCommitActionEntry.setEncoding(BASE64_ENCODING);
        gitPushCommitEntry.getActions().add(gitPushCommitActionEntry);
    }
    gitPushCommitEntry.setCommitMessage(commitMessage);
    return gitlabClient.commit(gitPushCommitEntry);
}
Also used : GitPushCommitActionEntry(com.epam.pipeline.entity.git.GitPushCommitActionEntry) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) UploadFileMetadata(com.epam.pipeline.controller.vo.UploadFileMetadata) GitPushCommitEntry(com.epam.pipeline.entity.git.GitPushCommitEntry)

Example 4 with UploadFileMetadata

use of com.epam.pipeline.controller.vo.UploadFileMetadata in project cloud-pipeline by epam.

the class DataStorageController method uploadFile.

@RequestMapping(value = "/datastorage/{id}/list/upload", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "Uploads a file to data storage.", notes = "Uploads a file to data storage.", produces = MediaType.APPLICATION_JSON_VALUE)
public List<UploadFileMetadata> uploadFile(@PathVariable(value = ID) Long id, @RequestParam(value = PATH, required = false) final String folder, HttpServletRequest request) throws FileUploadException {
    MultipartFile file = consumeMultipartFile(request);
    LinkedList<UploadFileMetadata> uploadedFiles = new LinkedList<>();
    UploadFileMetadata fileMeta = new UploadFileMetadata();
    fileMeta.setFileName(FilenameUtils.getName(file.getOriginalFilename()));
    fileMeta.setFileSize(file.getSize() / BYTES_IN_KB + " Kb");
    fileMeta.setFileType(file.getContentType());
    try {
        fileMeta.setBytes(file.getBytes());
        uploadedFiles.add(fileMeta);
    } catch (IOException e) {
        throw new DataStorageException("Failed to upload file to datastorage.", e);
    }
    dataStorageApiService.createDataStorageFile(id, folder, fileMeta.getFileName(), fileMeta.getBytes());
    return uploadedFiles;
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) UploadFileMetadata(com.epam.pipeline.controller.vo.UploadFileMetadata) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

UploadFileMetadata (com.epam.pipeline.controller.vo.UploadFileMetadata)4 ApiOperation (io.swagger.annotations.ApiOperation)2 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 DataStorageException (com.epam.pipeline.entity.datastorage.DataStorageException)1 GitCommitEntry (com.epam.pipeline.entity.git.GitCommitEntry)1 GitFile (com.epam.pipeline.entity.git.GitFile)1 GitPushCommitActionEntry (com.epam.pipeline.entity.git.GitPushCommitActionEntry)1 GitPushCommitEntry (com.epam.pipeline.entity.git.GitPushCommitEntry)1 Pipeline (com.epam.pipeline.entity.pipeline.Pipeline)1 AbstractManagerTest (com.epam.pipeline.manager.AbstractManagerTest)1 Test (org.junit.Test)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1